-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_evaluate.py
137 lines (113 loc) · 5.13 KB
/
train_evaluate.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
import torch
from tqdm import tqdm
import time
import copy
def train_model(model, dataloaders, criterion, optimizer, save_dir = None, save_all_epochs=False, num_epochs=25):
'''
model: The NN to train
dataloaders: A dictionary containing at least the keys
'train','val' that maps to Pytorch data loaders for the dataset
criterion: The Loss function
optimizer: The algorithm to update weights
(Variations on gradient descent)
num_epochs: How many epochs to train for
save_dir: Where to save the best model weights that are found,
as they are found. Will save to save_dir/weights_best.pth
Using None will not write anything to disk
save_all_epochs: Whether to save weights for ALL epochs, not just the best
validation error epoch. Will save to save_dir/weights_e{#}.pth
'''
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
since = time.time()
train_losses, val_losses = [], []
train_acc, val_acc = [], []
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
# TQDM has nice progress bars
for inputs, labels in tqdm(dataloaders[phase]):
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
# Get model outputs and calculate loss
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
if phase == 'train':
loss.backward()
optimizer.step()
# record loss and correct
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / len(dataloaders[phase].dataset)
epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
if phase == 'val':
val_losses.append(epoch_loss)
val_acc.append(epoch_acc)
if phase == "train":
train_losses.append(epoch_loss)
train_acc.append(epoch_acc)
print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
return model, train_losses, train_acc, val_losses, val_acc
def evaluate(model, dataloader, criterion, is_labelled = False, generate_labels = True, k = 5):
'''
Evaluation of the model on validation and test set only. (criteria: loss, top1 acc, top5 acc)
'''
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.eval()
running_loss = 0
running_top1_correct = 0
running_top5_correct = 0
predicted_labels = []
for inputs, labels in tqdm(dataloader):
inputs = inputs.to(device)
labels = labels.to(device)
tiled_labels = torch.stack([labels.data for i in range(k)], dim=1)
with torch.set_grad_enabled(False):
outputs = model(inputs)
if is_labelled:
loss = criterion(outputs, labels)
_, preds = torch.topk(outputs, k=5, dim=1)
if generate_labels:
nparr = preds.cpu().detach().numpy()
predicted_labels.extend([list(nparr[i]) for i in range(len(nparr))])
if is_labelled:
running_loss += loss.item() * inputs.size(0)
running_top1_correct += torch.sum(preds[:, 0] == labels.data)
running_top5_correct += torch.sum(preds == tiled_labels)
else:
pass
if is_labelled:
epoch_loss = float(running_loss / len(dataloader.dataset))
epoch_top1_acc = float(running_top1_correct.double() / len(dataloader.dataset))
epoch_top5_acc = float(running_top5_correct.double() / len(dataloader.dataset))
else:
epoch_loss = None
epoch_top1_acc = None
epoch_top5_acc = None
return epoch_loss, epoch_top1_acc, epoch_top5_acc, predicted_labels