-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask3_functions.py
173 lines (141 loc) · 5.62 KB
/
task3_functions.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
import copy
import time
import torch
import numpy as np
import matplotlib.pyplot as plt
from torchvision import transforms
from numpy.random import default_rng
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
# convenience functions
def view_grid(inp, title=None):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
if title is not None:
plt.title(title)
def train_model(device, dataloaders, dataset_sizes, model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(num_epochs):
print(f'Epoch {epoch}/{num_epochs - 1}')
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.
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'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# 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)
if phase == 'train':
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')
# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
print()
time_elapsed = time.time() - since
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
print(f'Best val Acc: {best_acc:4f}')
# load best model weights
model.load_state_dict(best_model_wts)
return model
def visualize_model(device, dataloaders, class_names, model, num_images=6):
was_training = model.training
model.eval()
images_so_far = 0
fig = plt.figure()
with torch.no_grad():
for i, (inputs, labels) in enumerate(dataloaders['val']):
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
for j in range(inputs.size()[0]):
images_so_far += 1
ax = plt.subplot(num_images//2, 2, images_so_far)
ax.axis('off')
ax.set_title(f'predicted: {class_names[preds[j]]}')
view_grid(inputs.cpu().data[j])
if images_so_far == num_images:
model.train(mode=was_training)
return
model.train(mode=was_training)
def explore_wrong_5x5_rgb(dataloader, model, device, class_labels=None, seed=None, replace=False):
model.eval()
rng = default_rng(seed)
all_wrong = torch.empty(0, dtype=torch.int64, device=device)
preds = torch.empty(0, dtype=torch.int64, device=device)
gtruths = torch.empty(0, dtype=torch.int64, device=device)
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X).argmax(1)
wrong = pred != y
wrong_ixs = torch.argwhere(wrong).flatten()
for ix in wrong_ixs:
all_wrong = torch.cat((all_wrong, X[ix, ...][None, ...]))
preds = torch.cat((preds, torch.tensor([pred[ix]]).to(device)))
gtruths = torch.cat((gtruths, torch.tensor([y[ix]]).to(device)))
example_ixs = rng.choice(range(len(gtruths)), 25, replace=replace)
fig, axes = plt.subplots(nrows=5, ncols=5, figsize=(14, 14))
fig.tight_layout()
for i, ix in enumerate(example_ixs):
X = all_wrong[ix]
y = gtruths[ix]
y_guess = preds[ix]
if class_labels:
true = class_labels[y]
guess = class_labels[y_guess]
else:
true = str(int(y))
guess = str(int(y_guess))
ax = axes.flatten()[i]
ax.set_title(f'True:{true}, Guess:{guess}')
im = X.squeeze().cpu().numpy()
im = np.moveaxis(im, 0, -1)
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
im = std * im + mean
im = np.clip(im, 0, 1)
ax.imshow(im)
model.train()