-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_runner.py
196 lines (159 loc) · 8.18 KB
/
net_runner.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
from datetime import datetime
import shutil
import time
import torch
from torch.utils.tensorboard import SummaryWriter
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import torchvision
import warnings
from utils import load_alexnet_model
class NetRunner:
def __init__(self, model_path, train_set, test_set, val_set, config, num_classes):
# Remove all files and subfolders in the 'runs' folder
shutil.rmtree('runs', ignore_errors=True)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.train_set = train_set
self.test_set = test_set
self.val_set = val_set
self.config = config
# Set the model_path attribute with the provided model path
self.model_path = model_path
self.model = self._load_model(model_path, num_classes).to(self.device)
self.criterion = torch.nn.CrossEntropyLoss()
self.optimizer = torch.optim.SGD(self.model.parameters(), lr=config['learning_rate'], momentum=config['momentum'])
timestamp = time.time()
date_time = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d_%H-%M-%S')
name = 'dog_exp_'
run_name = name + date_time
self.writer = SummaryWriter(f'runs/{run_name}')
# Add model graph to TensorBoard
dummy_input = torch.randn(1, 3, 224, 224).to(self.device)
self.writer.add_graph(self.model, dummy_input)
self.writer.flush()
def _load_model(self, model_path, num_classes):
self.model = load_alexnet_model(model_path, num_classes).to(self.device)
return self.model
def train(self):
best_acc = 0.0
early_stopping_counter = 0
for epoch in range(self.config['num_epochs']):
print(f'Epoch {epoch + 1}/{self.config["num_epochs"]}')
self.model.train()
running_loss = 0.0
running_corrects = 0
all_labels = []
all_preds = []
for inputs, labels in self.train_set:
# Visualize the first batch of training images
img_grid = torchvision.utils.make_grid(inputs)
self.writer.add_image('first_batch', img_grid, global_step=epoch)
inputs = inputs.to(self.device)
labels = labels.to(self.device)
self.optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = self.model(inputs)
_, preds = torch.max(outputs, 1)
loss = self.criterion(outputs, labels)
loss.backward()
self.optimizer.step()
all_labels.extend(labels.cpu().numpy())
all_preds.extend(preds.cpu().numpy())
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / len(self.train_set.dataset)
epoch_acc = running_corrects.double() / len(self.train_set.dataset)
print(f'Train Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
self.writer.add_scalar('Loss/train', epoch_loss, epoch)
self.writer.add_scalar('Accuracy/train', epoch_acc, epoch)
# Calculate and visualize confusion matrix
warnings.filterwarnings('ignore')
cm = confusion_matrix(all_labels, all_preds)
fig = plt.figure()
ax = fig.add_subplot(111)
cax=ax.matshow(cm)
fig.colorbar(cax)
ax.set_xticklabels([''] + self.train_set.dataset.classes)
ax.set_yticklabels([''] + self.train_set.dataset.classes)
plt.xlabel('Predicted')
plt.ylabel('True')
self.writer.add_figure('confusion_matrix_train', fig, epoch)
plt.close()
# Calcolo della confusion matrix per il validation set e della loss di validation per ogni epoca.
all_labels_valset=[]
all_preds_valset=[]
running_loss_valset=0.0
for inputs_valset, labels_valset in self.val_set:
inputs_valset=inputs_valset.to(self.device)
labels_valset=labels_valset.to(self.device)
with torch.set_grad_enabled(False):
outputs_valset=self.model(inputs_valset)
_, preds_valset=torch.max(outputs_valset,1)
loss_valset=self.criterion(outputs_valset,labels_valset)
all_labels_valset.extend(labels_valset.cpu().numpy())
all_preds_valset.extend(preds_valset.cpu().numpy())
running_loss_valset+=loss_valset.item()*inputs_valset.size(0)
epoch_loss_valset=running_loss_valset/len(self.val_set.dataset)
print(f'Validation Loss: {epoch_loss_valset:.4f}')
self.writer.add_scalar('Loss/validation', epoch_loss_valset, epoch)
cm_valset = confusion_matrix(all_labels_valset, all_preds_valset)
fig_valset_cm = plt.figure()
ax_valset_cm = fig_valset_cm.add_subplot(111)
cax_valset_cm = ax_valset_cm.matshow(cm_valset)
fig_valset_cm.colorbar(cax_valset_cm)
ax_valset_cm.set_xticklabels([''] + self.val_set.dataset.classes)
ax_valset_cm.set_yticklabels([''] + self.val_set.dataset.classes)
plt.xlabel('Predicted')
plt.ylabel('True')
self.writer.add_figure('confusion_matrix_validation', fig_valset_cm, epoch)
plt.close()
# Add embeddings to TensorBoard
features = []
labels = []
images = []
for inputs, label in self.val_set:
inputs=inputs.to(self.device)
with torch.set_grad_enabled(False):
output=self.model(inputs)
features.append(output)
labels.append(label)
images.append(inputs)
features=torch.cat(features).cpu().numpy()
labels=torch.cat(labels).cpu().numpy()
images=torch.cat(images).cpu()
class_names=self.val_set.dataset.classes
label_names=[class_names[i] for i in labels]
metadata=[f'{label}:{name}' for label,name in zip(labels,label_names)]
val_acc=self.evaluate(self.val_set)
if val_acc > best_acc:
best_acc=val_acc
best_model_wts=self.model.state_dict()
# Save the best model weights to the specified model path
save_path = self.model_path
torch.save(best_model_wts, save_path)
print(f'TRAINING - INFO - Saved best model weights to {save_path}')
early_stopping_counter=0
print(f'TRAINING - INFO - Best val Acc: {best_acc:.4f}')
else:
early_stopping_counter += 1
if early_stopping_counter >= self.config['early_stopping_patience']:
print(f'TRAINING - INFO - Early stopping after {early_stopping_counter} epochs with no improvement')
break
self.model.load_state_dict(best_model_wts)
self.writer.add_embedding(features,metadata=metadata,label_img=images,global_step=epoch)
print('DOG - TRAINING - INFO - Training finished')
def evaluate(self, dataset):
self.model.eval()
running_corrects=0
for inputs, labels in dataset:
inputs=inputs.to(self.device)
labels=labels.to(self.device)
with torch.set_grad_enabled(False):
outputs=self.model(inputs)
_, preds=torch.max(outputs, 1)
running_corrects+=torch.sum(preds==labels.data)
acc=running_corrects.double()/len(dataset.dataset)
return acc
def test(self):
acc=self.evaluate(self.test_set)
print(f'Test Acc: {acc:.4f}')