-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrainer.py
95 lines (74 loc) · 3.55 KB
/
trainer.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
import time
import copy
import torch
def train_model(model, dataloaders, criterion, optimizer, num_epochs=25):
"""
Function to train a model for a number of epochs.
Args:
model: The neural network model to train
dataloaders: A dictionary of data loaders of the shape {'train': train_loader, 'val': val_loader}.
criterion: The loss function. Takes model outputs and labels as input and produces a loss value
optimizer: The optimizer object to use in order to train the model
num_epochs: The number of epochs to train the model for
Returns:
(model, val_acc_history): The trained model and the history of validation accuracy
"""
since = time.time()
val_acc_history = []
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(1, num_epochs+1):
print('Epoch {}/{}'.format(epoch, 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
all_preds = []
all_labels = []
# Iterate over data.
for inputs, labels in 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)
# Get model predictions
_, preds = torch.max(outputs, 1)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
all_preds.append(preds)
all_labels.append(labels)
epoch_loss = running_loss / len(dataloaders[phase].sampler.indices)
epoch_acc = running_corrects.double() / len(dataloaders[phase].sampler.indices)
all_labels = torch.cat(all_labels, 0)
all_preds = torch.cat(all_preds, 0)
epoch_weighted_acc = accuracy_score(all_labels.cpu().numpy(), all_preds.cpu().numpy(), sample_weight=compute_sample_weight(class_weights, all_labels.cpu().numpy()))
print('{} Loss: {:.4f} - Acc: {:.4f} - Weighted Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc, epoch_weighted_acc))
# deep copy the model
if phase == 'val' and epoch_weighted_acc > best_acc:
best_acc = epoch_weighted_acc
best_model_wts = copy.deepcopy(model.state_dict())
if phase == 'val':
val_acc_history.append(epoch_weighted_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, val_acc_history