-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
384 lines (318 loc) · 12.3 KB
/
functions.py
File metadata and controls
384 lines (318 loc) · 12.3 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import numpy as np
import itertools
import os
import torch
from torch.nn.functional import softmax
from sklearn import metrics
from IPython.display import clear_output
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import torch
import torch.nn as nn
from torchvision import models
import torch.nn.functional as F
#Model Init ----------------------------------------------------------------------------------
class music_net(nn.Module):
def __init__(self):
# """Intitalize neural net layers"""
super(music_net, self).__init__()
# self.conv1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, stride=1, padding=0)
# self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=3, stride=1, padding=0)
# self.conv3 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=0)
# self.conv4 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=0)
# self.conv5 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=0)
# self.fc1 = nn.Linear(in_features=9856, out_features=10)
# self.batchnorm1 = nn.BatchNorm2d(num_features=8)
# self.batchnorm2 = nn.BatchNorm2d(num_features=16)
# self.batchnorm3 = nn.BatchNorm2d(num_features=32)
# self.batchnorm4 = nn.BatchNorm2d(num_features=64)
# self.batchnorm5 = nn.BatchNorm2d(num_features=128)
# self.dropout = nn.Dropout(p=0.3, inplace=False)
# Densenet init
self.densenet = models.densenet121(pretrained=True)
#self.densenet = models.densenet121()
#for param in self.densenet.parameters():
# param.requires_grad = False
self.densenet.eval()
num_ftrs = self.densenet.classifier.in_features
self.densenet.classifier = nn.Linear(num_ftrs, 10)
def forward(self, x):
# # Conv layer 1.
# x = self.conv1(x)
# x = self.batchnorm1(x)
# x = F.relu(x)
# x = F.max_pool2d(x, kernel_size=2)
# # Conv layer 2.
# x = self.conv2(x)
# x = self.batchnorm2(x)
# x = F.relu(x)
# x = F.max_pool2d(x, kernel_size=2)
# # Conv layer 3.
# x = self.conv3(x)
# x = self.batchnorm3(x)
# x = F.relu(x)
# x = F.max_pool2d(x, kernel_size=2)
# # Conv layer 4.
# x = self.conv4(x)
# x = self.batchnorm4(x)
# x = F.relu(x)
# x = F.max_pool2d(x, kernel_size=2)
# # Conv layer 5.
# x = self.conv5(x)
# x = self.batchnorm5(x)
# x = F.relu(x)
# x = F.max_pool2d(x, kernel_size=2)
# # Fully connected layer 1.
# x = torch.flatten(x, 1)
# x = self.dropout(x)
# x = self.fc1(x)
# x = F.softmax(x, dim=1)
# Densenet layers
x = self.densenet(x)
return x
#Plot Graphs ----------------------------------------------------------------------------------
def plot_loss_accuracy(validation_acc, validation_loss, validation_auroc, train_acc, train_loss, train_auroc):
clear_output(wait=True)
epochs = len(train_acc)
_, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15.5, 5.5))
ax1.plot(list(range(epochs)), train_loss, label='Training Loss')
ax1.plot(list(range(epochs)), validation_loss, label='Validation Loss')
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Loss')
ax1.set_title('Epoch vs Loss')
ax1.legend()
ax2.plot(list(range(epochs)), train_acc, label='Training Accuracy')
ax2.plot(list(range(epochs)), validation_acc, label='Validation Accuracy')
ax2.set_xlabel('Epochs')
ax2.set_ylabel('Accuracy')
ax2.set_title('Epoch vs Accuracy')
ax2.legend()
ax3.plot(list(range(epochs)), train_auroc, label='Training AUROC')
ax3.plot(list(range(epochs)), validation_auroc, label='Validation AUROC')
ax3.set_xlabel('Epochs')
ax3.set_ylabel('AUROC')
ax3.set_title('Epoch vs AUROC')
ax3.legend()
plt.show()
#Adaptive training ----------------------------------------------------------------------------------
#Adapted from 445 Project
def save_checkpoint(model, epoch, checkpoint_dir, stats):
state = {
"epoch": epoch,
"state_dict": model.state_dict(),
"stats": stats,
}
filename = os.path.join(checkpoint_dir, "epoch={}.checkpoint.pth.tar".format(epoch))
torch.save(state, filename)
#Adapted from 445 Project
def restore_checkpoint(model, checkpoint_dir):
try:
cp_files = [
file_
for file_ in os.listdir(checkpoint_dir)
if file_.startswith("epoch=") and file_.endswith(".checkpoint.pth.tar")
]
except FileNotFoundError:
cp_files = None
os.makedirs(checkpoint_dir)
if not cp_files:
print("No saved model parameters found")
return model, 0, []
# Find latest epoch
for i in itertools.count(1):
if "epoch={}.checkpoint.pth.tar".format(i) in cp_files:
epoch = i
else:
break
print(
"Which epoch to load from? Choose in range [0, {}].".format(epoch),
"Enter 0 to train from scratch.",
)
print(">> ", end="")
inp_epoch = int(input())
if inp_epoch not in range(epoch + 1):
raise Exception("Invalid epoch number")
if inp_epoch == 0:
print("Checkpoint not loaded")
clear_checkpoint(checkpoint_dir)
return model, 0, []
filename = os.path.join(
checkpoint_dir, "epoch={}.checkpoint.pth.tar".format(inp_epoch)
)
print("Loading from checkpoint {}?".format(filename))
checkpoint = torch.load(filename)
try:
stats = checkpoint["stats"]
model.load_state_dict(checkpoint["state_dict"])
print(
"=> Successfully restored checkpoint (trained for {} epochs)".format(
checkpoint["epoch"]
)
)
except:
print("=> Checkpoint not successfully restored")
raise
return model, inp_epoch, stats
#Adapted from 445 Project
def clear_checkpoint(checkpoint_dir):
filelist = [f for f in os.listdir(checkpoint_dir) if f.endswith(".pth.tar")]
for f in filelist:
os.remove(os.path.join(checkpoint_dir, f))
print("Checkpoint successfully removed")
#Adapted from 445 Project
def early_stopping(stats, curr_count_to_patience, global_min_loss):
curr_loss = stats[-1][1]
if curr_loss >= global_min_loss:
curr_count_to_patience += 1
else:
global_min_loss = curr_loss
curr_count_to_patience = 0
return curr_count_to_patience, global_min_loss
#Getting Metrics ----------------------------------------------------------------------------------
def evaluate_epoch(
tr_loader,
val_loader,
te_loader,
model,
criterion,
epoch,
stats,
include_test=False,
):
#Test training and validation sets
def _get_metrics(loader):
y_true, y_pred, y_score = [], [], []
correct, total = 0, 0
running_loss = []
for X, y in loader:
X, y = X.to("cuda"), y.to("cuda")
with torch.no_grad():
output = model(X)
predicted = predictions(output)
y_true.append(y)
y_pred.append(predicted)
y_score.append(softmax(output, dim=1))
total += y.size(0)
correct += (predicted == y).sum().item()
running_loss.append(criterion(output, y).item())
y_true = torch.cat(y_true)
y_pred = torch.cat(y_pred)
y_score = torch.cat(y_score)
loss = np.mean(running_loss)
acc = correct / total
auroc = metrics.roc_auc_score(y_true.cpu(), y_score.cpu(), multi_class="ovo")
return acc, loss, auroc
train_acc, train_loss, train_auc = _get_metrics(tr_loader)
val_acc, val_loss, val_auc = _get_metrics(val_loader)
stats_at_epoch = [
val_acc,
val_loss,
val_auc,
train_acc,
train_loss,
train_auc,
]
if include_test:
stats_at_epoch += list(_get_metrics(te_loader))
stats.append(stats_at_epoch)
log_training(epoch, stats)
else:
stats.append(stats_at_epoch)
def predictions(logits):
return logits.max(1)[1]
def save_cnn_training_plot():
plt.savefig("cnn_training_plot.png", dpi=200)
def log_training(epoch, stats):
splits = ["Validation", "Train", "Test"]
metrics = ["Accuracy", "Loss", "AUROC"]
print("Epoch {}".format(epoch))
for j, split in enumerate(splits):
for i, metric in enumerate(metrics):
idx = len(metrics) * j + i
if idx >= len(stats[-1]):
continue
print(f"\t{split} {metric}:{round(stats[-1][idx],4)}")
#Confusion Matrix Generation ----------------------------------------------------------------------------------
#Adapted from 445 Project
def gen_labels(loader, model):
#returns true and predicted values
y_true, y_pred = [], []
for X, y in loader:
with torch.no_grad():
output = model(X)
predicted = predictions(output.data)
y_true = np.append(y_true, y.numpy())
y_pred = np.append(y_pred, predicted.numpy())
return y_true, y_pred
#Adapted from 445 Project
def plot_conf(loader, model, sem_labels, png_name):
#Draw confusion matrix
y_true, y_pred = gen_labels(loader, model)
cm = confusion_matrix(y_true, y_pred)
fig, ax = plt.subplots()
cax = ax.matshow(cm, cmap=plt.cm.Blues, interpolation="nearest")
cbar = fig.colorbar(cax, fraction=0.046, pad=0.04)
cbar.set_label("Frequency", rotation=270, labelpad=10)
for (i, j), z in np.ndenumerate(cm):
ax.text(j, i, z, ha="center", va="center")
plt.gcf().text(0.02, 0.4, sem_labels, fontsize=9)
plt.subplots_adjust(left=0.5)
ax.set_xlabel("Predictions")
ax.xaxis.set_label_position("top")
ax.set_ylabel("True Labels")
plt.savefig(png_name)
#Helper Functions ----------------------------------------------------------------------------------
def count_parameters(model):
#Some interesting info
return sum(p.numel() for p in model.parameters() if p.requires_grad)
#Train Function ----------------------------------------------------------------------------------
def train(model, device, tr_loader, va_loader, te_loader):
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0005)
stats = []
#Set desired patience level
patience = 20
curr_count_to_patience = 0
epoch = 0
print("Number of float-valued parameters:", count_parameters(model))
evaluate_epoch(
tr_loader, va_loader, te_loader, model, criterion, epoch, stats
)
global_min_loss = stats[0][1]
values = np.array(stats)
plot_loss_accuracy(values[:,0], values[:,1], values[:,2], values[:,3], values[:,4], values[:,5])
while curr_count_to_patience < patience:
train_epoch(tr_loader, model, criterion, optimizer, device)
evaluate_epoch(
tr_loader,
va_loader,
te_loader,
model,
criterion,
epoch + 1,
stats,
)
save_checkpoint(model, epoch + 1, './checkpoints/', stats)
curr_count_to_patience, global_min_loss = early_stopping(
stats, curr_count_to_patience, global_min_loss
)
epoch += 1
values = np.array(stats)
plot_loss_accuracy(values[:,0], values[:,1], values[:,2], values[:,3], values[:,4], values[:,5])
#return np.array(stats)[:,1].argmin()+1
print("Finished Training")
def train_epoch(loader, model, criterion, optimizer, device):
for data, target in loader:
data, target = data.to(device), target.to(device)
output = model(data)
optimizer.zero_grad()
loss = criterion(output, target)
loss.backward()
optimizer.step()
def set_seed(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False