-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
283 lines (228 loc) · 10.2 KB
/
utils.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
import torch.nn as nn
from torchvision import models
import matplotlib.pyplot as plt
from sklearn.metrics import (
auc,
confusion_matrix,
roc_curve,
classification_report
)
import numpy as np
import seaborn as sns
import numpy as np
import itertools
import torch
from torch.autograd.variable import Variable
from tqdm import tqdm
from pytorchtools import EarlyStopping
import torch.optim as optim
from torchvision.models import EfficientNet_V2_L_Weights, ResNet50_Weights
import copy
import mlflow
import mlflow.pytorch
import time
from datetime import date
import pandas as pd
today = str(date.today())
def test(test_loader, model_ft,DEVICE,modelName):
result_list = []
label_list = []
predicted_list = []
for input,target in tqdm(test_loader,total=len(test_loader)):
with torch.no_grad():
input = Variable(input).float().to(DEVICE)
if modelName == 'Inception':
output,_,_ = model_ft(input)
else:
output = model_ft(input)
soft_output = torch.softmax(output,dim=-1)
preds = soft_output.to('cpu').detach().numpy()
label = target.to('cpu').detach().numpy()
_,predicted = torch.max(soft_output.data, 1)
predicted = predicted.to('cpu').detach().numpy()
for i_batch in range(preds.shape[0]):
result_list.append(preds[i_batch,1])
label_list.append(label[i_batch])
predicted_list.append(predicted[i_batch])
return (label_list,predicted_list)
def log_scalar(name, value, step):
"""Log a scalar value to both MLflow and TensorBoard"""
mlflow.log_metric(name, value, step=step)
def training(model, num_epochs, train_dataloader, val_dataloader,DEVICE,use_auxiliary=False):
since = time.time()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.33)
criterion = nn.CrossEntropyLoss()
patience = 15
train_loss_array = []
train_acc_array = []
val_loss_array = []
val_acc_array = []
valid_losses = []
valid_loss = 0.0
early_stopping = EarlyStopping(patience=patience, verbose=True)
for epoch in range(num_epochs):
print('Epoch: {} | Learning rate: {}'.format(epoch + 1, scheduler.get_last_lr()))
epoch_loss = 0
epoch_correct_items = 0
epoch_items = 0
model.train()
print('Training')
for inputs, targets in tqdm(train_dataloader,total=len(train_dataloader)):
inputs = inputs.to(DEVICE)
targets = targets.to(DEVICE)
optimizer.zero_grad()
if use_auxiliary:
outputs, aux1, aux2 = model(inputs)
loss = criterion(outputs, targets) + 0.3 * criterion(aux1, targets) + 0.3 * criterion(aux2, targets)
else:
outputs = model(inputs)
loss = criterion(outputs, targets)
preds = outputs.argmax(dim=1)
correct_items = (preds == targets).float().sum()
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_correct_items += correct_items.item()
epoch_items += len(targets)
train_loss_array.append(epoch_loss / epoch_items)
train_acc_array.append(epoch_correct_items / epoch_items)
# display the accuracy each epoch
each_train_acc = 100* (epoch_correct_items / epoch_items)
each_train_loss = epoch_loss / epoch_items
scheduler.step()
model.eval()
print('Validation')
with torch.no_grad():
for inputs, targets in tqdm(val_dataloader,total=len(val_dataloader)):
inputs = inputs.to(DEVICE)
targets = targets.to(DEVICE)
if use_auxiliary:
outputs, _, _ = model(inputs)
else:
outputs = model(inputs)
loss = criterion(outputs, targets)
preds = outputs.argmax(dim=1)
correct_items = (preds == targets).float().sum()
epoch_loss += loss.item()
epoch_correct_items += correct_items.item()
epoch_items += len(targets)
valid_losses.append(loss.item())
# calculate average losses
valid_loss = np.average(valid_losses)
val_loss_array.append(epoch_loss / epoch_items)
val_acc_array.append(epoch_correct_items / epoch_items)
# display the accuracy each epoch
each_valid_acc = 100* (epoch_correct_items / epoch_items)
each_valid_loss = epoch_loss / epoch_items
log_scalar('training_loss', each_train_loss, epoch)
log_scalar('training_accuracy', float(each_train_acc), epoch)
log_scalar('val_loss', each_valid_loss, epoch)
log_scalar('val_accuracy', float(each_valid_acc), epoch)
print(f"Training loss: {each_train_loss:.3f}, training acc: {each_train_acc:.3f}")
print(f"Validation loss: {each_valid_loss:.3f}, validation acc: {each_valid_acc:.3f}")
print('-'*50)
early_stopping(valid_loss, model)
best_model_wts = copy.deepcopy(model.state_dict())
model.load_state_dict(best_model_wts)
time_elapsed = time.time() - since
if early_stopping.early_stop:
print("Early stopping")
best_model_wts = copy.deepcopy(model.state_dict())
model.load_state_dict(best_model_wts)
return best_model_wts, train_loss_array, train_acc_array, val_loss_array, val_acc_array
return best_model_wts, train_loss_array, train_acc_array, val_loss_array, val_acc_array
def visualize_training_results(train_loss_array,
val_loss_array,
train_acc_array,
val_acc_array,
num_epochs,
model_name,
batch_size):
fig, axs = plt.subplots(1, 2, figsize=(14,4))
fig.suptitle("{} training | Batch size: {}".format(model_name, batch_size), fontsize = 16)
axs[0].plot(list(range(1, len(train_loss_array)+1)), train_loss_array, label="train_loss")
axs[0].plot(list(range(1, len(val_loss_array)+1)), val_loss_array, label="val_loss")
axs[0].legend(loc='best')
axs[0].set(xlabel='epochs', ylabel='loss')
axs[1].plot(list(range(1, len(train_loss_array)+1)), train_acc_array, label="train_acc")
axs[1].plot(list(range(1, len(val_loss_array)+1)), val_acc_array, label="val_acc")
axs[1].legend(loc='best')
axs[1].set(xlabel='epochs', ylabel='accuracy')
# plt.savefig("Visualize Training Results")
fig.savefig('loss_n_accuracy.png', bbox_inches='tight')
# plt.show()
def set_parameter_requires_grad(model, feature_extracting):
# not fine tuning
if feature_extracting:
for param in model.parameters():
param.requires_grad = False
"""
FINE TUNING
for params in model.parameters():
params.requires_grad = True
"""
def plot_confusion_matrix(y_test,y_pred,
target_names,
title='Confusion matrix',
cmap=None,
normalize=True
):
"""
given a sklearn confusion matrix (cm), make a nice plot
Arguments
---------
cm: confusion matrix from sklearn.metrics.confusion_matrix
target_names: given classification classes such as [0, 1, 2]
the class names, for example: ['high', 'medium', 'low']
title: the text to display at the top of the matrix
cmap: the gradient of the values displayed from matplotlib.pyplot.cm
see http://matplotlib.org/examples/color/colormaps_reference.html
plt.get_cmap('jet') or plt.cm.Blues
normalize: If False, plot the raw numbers
If True, plot the proportions
Usage
-----
plot_confusion_matrix(cm = cm, # confusion matrix created by
# sklearn.metrics.confusion_matrix
normalize = True, # show proportions
target_names = y_labels_vals, # list of names of the classes
title = best_estimator_name) # title of graph
Citiation
---------
https://stackoverflow.com/questions/39033880/plot-confusion-matrix-sklearn-with-multiple-labels
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
cm = confusion_matrix(y_test,y_pred)
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('cividis')
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
if normalize:
cm2 = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm2.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.2f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.savefig("Confusion Matrix.png",bbox_inches = 'tight')
# plt.show()
report =classification_report(y_true=y_test,y_pred=y_pred,target_names=target_names,output_dict=True)
df = pd.DataFrame(report).transpose()
df.to_csv("Classification_Report.csv")