forked from Alexander2116/graphenehackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser_dl_fnn.py
284 lines (238 loc) · 8.89 KB
/
laser_dl_fnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
import numpy as np
from torch.utils.data import Dataset, DataLoader
import glob
import random
import wandb
import torch.nn.functional as F
learning_rate = 0.001
num_epochs = 500
wandb.init(
# Set the project where this run will be logged
project="graphene_hackathon_fnn",
# Track hyperparameters and run metadata
config={
"learning_rate": learning_rate,
"epochs": num_epochs,
"conv_kernel": 3,
"conv_layers": 2,
"hidden_channels":8,
"output_channels":16,
"pool_kernel":2,
},
)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
class CustomDataset(Dataset):
def __init__(self, csv_file, transform=None):
self.data = pd.read_csv(csv_file)
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
sample = torch.tensor(self.data.iloc[idx, 1:].values, dtype=torch.float32)
if self.transform:
sample = self.transform(sample)
label = torch.tensor(self.data.iloc[idx, 0], dtype=torch.float32)
return sample, label
class MultiClassDataset(Dataset):
def __init__(self, file_paths, transform=None):
self.file_paths = file_paths
self.transform = transform
self.data, self.labels = self.load_data()
def load_data(self):
all_data = np.zeros((247))
all_labels = np.zeros((247))
#all_labels = np.zeros((1))
label = 0
for file_path in self.file_paths:
class_data = np.genfromtxt(file_path,delimiter=',')
mean_data = np.mean(class_data)
class_data -= mean_data
all_data = np.vstack((all_data,class_data))
filename = file_path.replace(working_directory,"")
letter = filename.replace('train/',"")[0]
if letter == 'c':
label = 0
else:
letter = filename.replace('train/',"")[6]
if letter == 'b':
label = 2
else:
label = 1
label = np.ones(247)*label
#label = np.array(label)
all_labels = np.vstack((all_labels,label)).astype(int)
#all_labels = np.vstack((all_labels,label)).astype(int)
all_data = all_data[1::]
all_labels = all_labels[1::]
return all_data, all_labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
#sample = torch.tensor(self.data.iloc[idx, 1:].values[0], dtype=torch.float32)
sample = self.data[idx]
if self.transform:
sample = self.transform(sample)
#label = torch.tensor(self.labels[idx][0],dtype=torch.long)
label = torch.tensor(self.labels[idx][0],dtype=torch.long)
return sample, label
"""
class MultiClassDataset(Dataset):
def __init__(self, file_paths, transform=None):
self.file_paths = file_paths
self.transform = transform
self.data, self.labels = self.load_data()
def load_data(self):
all_data = np.zeros((1,10,500))
all_labels = np.zeros((1))
for file_path in self.file_paths:
class_data = np.load(file_path)
class_data.shape = [1,10,500]
all_data = np.vstack((all_data,class_data))
filename = file_path.replace(working_directory,"")
if filename[1]=='r':
letter = filename.replace('train/',"")[0]
if letter == 'l':
label = 0
else:
label = 1
else:
letter = filename.replace('test/',"")[0]
if letter == 'l':
label = 0
else:
label = 1
all_labels = np.vstack((all_labels,label))
all_data = all_data[1::]
all_labels = all_labels[1::]
return all_data,all_labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
#sample = torch.tensor(self.data.iloc[idx, 1:].values[0], dtype=torch.float32)
sample = self.data[idx]
sample.shape = [1,10,500]
if self.transform:
sample = self.transform(sample)
#label = torch.tensor(int(self.data[idx, 0]), dtype=torch.float32).reshape(-1, 1) # Assuming class labels are integers
label = torch.tensor(self.labels[idx],dtype=torch.float32)
return sample, label
"""
# Define a simple transform to tensor
class ToTensor:
def __call__(self, sample):
return torch.tensor(sample, dtype=torch.float32)
class LaserRegression(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LaserRegression, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
#self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
#x = self.sigmoid(x)
return x
"""
class LaserCNN(nn.Module):
def __init__(self, input_channels, hidden_channels, output_channels, conv_kernel_size, pool_kernel_size, hidden_size, output_size):
super(LaserCNN, self).__init__()
self.conv1 = nn.Conv2d(input_channels, hidden_channels, conv_kernel_size)
self.relu = nn.ReLU()
self.batchnorm1 = nn.BatchNorm2d(hidden_channels)
self.conv2 = nn.Conv2d(hidden_channels,output_channels,conv_kernel_size)
self.batchnorm2 = nn.BatchNorm2d(output_channels)
self.maxpool = nn.MaxPool2d(pool_kernel_size,stride=2)
self.fc1 = nn.Linear(hidden_size, output_size)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.batchnorm1(x)
x = self.conv2(x)
x = self.relu(x)
x = self.batchnorm2(x)
x = self.maxpool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.sigmoid(x)
return x
"""
"""
# Define input, hidden, and output sizes
num_batch = 2
input_size = 500
input_channels = 1
hidden_channels = 8
output_channels = 16
conv_kernel_size = 3
pool_kernel_size = 2
hidden_size = 248*3*output_channels
output_size = 1
"""
input_size = 247
hidden_size = 128
output_size = 3
num_batch = 2
working_directory = 'Documents/graphene_hackathon/real_data/'
# Instantiate the model
model = LaserRegression(input_size, hidden_size, output_size)
#model = LaserCNN(input_channels,hidden_channels,output_channels,conv_kernel_size,pool_kernel_size,hidden_size,output_size)
model.to(device)
#csv_file_path = 'train_set.csv'
#train_dataset = CustomDataset(csv_file_path, transform=ToTensor())
train_file_paths = glob.glob(working_directory+'train/*Al*.csv')
random.shuffle(train_file_paths)
train_dataset = MultiClassDataset(train_file_paths, transform=ToTensor())
train_loader = DataLoader(train_dataset, batch_size=num_batch, shuffle=False)
test_file_paths = glob.glob(working_directory+'train/*Al*.csv')
random.shuffle(test_file_paths)
test_dataset = MultiClassDataset(test_file_paths, transform=ToTensor())
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)
loss = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
model.train()
print('Starting training')
for epoch in range(num_epochs):
model.train()
batch_loss_array = []
print('Epoch',epoch)
for batch_index, batch in enumerate(train_loader):
inputs, labels = batch
inputs = inputs.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(inputs)
# Compute the loss
batch_loss = loss(outputs, labels)
batch_loss_array.append(batch_loss.item())
# Backward pass and optimization
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
# Print training progress
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {sum(batch_loss_array)/batch_index}')
wandb.log({"loss": sum(batch_loss_array)/batch_index})
model.eval()
with torch.no_grad():
correct = 0
total = 0
for inputs, targets in test_loader:
# Forward pass
outputs = model(inputs)
probabilities = F.softmax(outputs, dim=1)
_, predicted_classes = probabilities.max(dim=1)
#predicted = torch.round(outputs) # Round to 0 or 1
# Accuracy calculation
total += targets.size(0)
correct += (predicted_classes == targets).sum().item()
accuracy = correct / total
print(f'Test Accuracy: {accuracy * 100:.2f}%')
wandb.log({"test_accuracy": accuracy})
torch.save(model.state_dict(), working_directory+'parameters/'+str(epoch)+".torch")
#print("Save model to:",working_directory+'parameters/'+str(epoch)+".torch")