-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
163 lines (117 loc) · 4.42 KB
/
helpers.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
from collections import OrderedDict
import numpy as np
import torch
def train(model, optimizer, criterion, train_data_loader, n_epochs,
lr_scheduler=None, val_data_loader=None, metrics=[],
verbose=True, gpu=False, callbacks=[]):
if gpu:
device = torch.device('cuda')
else:
device = torch.device('cpu')
history = []
model.train()
model.to(device)
for callback in callbacks:
callback.set_model(model)
callback.on_train_start()
for i in range(n_epochs):
for callback in callbacks:
callback.on_epoch_start(i)
if lr_scheduler:
lr_scheduler.step()
train_metrics = OrderedDict(
[('loss', 0)]
+ [(metric.__name__, 0) for metric in metrics]
)
n_samples = 0
for inputs, targets in train_data_loader:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
for callback in callbacks:
callback.on_batch_start(inputs, targets)
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_metrics['loss'] += loss.data * inputs.size(0)
for metric in metrics:
res = metric(outputs, targets)
train_metrics[metric.__name__] += res * inputs.size(0)
n_samples += inputs.size(0)
for callback in callbacks:
callback.on_batch_end(inputs, targets, outputs, loss)
history.append(OrderedDict())
for m in train_metrics:
history[-1][m] = (train_metrics[m] / n_samples).item()
if val_data_loader:
with torch.no_grad():
model.eval()
val_metrics = dict(
[('loss', 0)]
+ [(metric.__name__, 0) for metric in metrics]
)
n_val_samples = 0
for inputs, targets in val_data_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
val_metrics['loss'] += loss.data * inputs.size(0)
for metric in metrics:
res = metric(outputs, targets)
val_metrics[metric.__name__] += res * inputs.size(0)
n_val_samples += inputs.size(0)
for m in train_metrics:
history[-1]['val_' + m] = \
(val_metrics[m] / n_val_samples).item()
model.train()
if verbose:
print('epoch={:<5} {}'.format(
i+1,
' '.join(['{}={:<10.5f}'.format(*h)
for h in history[-1].items()])
))
for callback in callbacks:
callback.on_epoch_end(i, history)
for callback in callbacks:
callback.on_train_end(history)
model.eval()
history = dict(zip(history[0], zip(*[d.values() for d in history])))
return history
def predict(model, data_loader, final_activation=None, gpu=False,
force_model_eval_mode=True):
if gpu:
device = torch.device('cuda')
else:
device = torch.device('cpu')
model.to(device)
if force_model_eval_mode:
model.eval()
with torch.no_grad():
preds = []
for inputs, _ in data_loader:
inputs = inputs.to(device)
predictions = model(inputs)
if final_activation is not None:
predictions = final_activation(predictions)
preds.append(predictions.cpu().numpy())
preds = np.vstack(preds)
return preds
def _check_is_probability(x):
if x.size(1) == 1:
return (x >= 0).all() and (x <= 1).all()
else:
return ((x >= 0).all() and (x <= 1).all()
and (x.sum(axis=-1) == 1.).all())
def binary_accuracy(outputs, targets):
if _check_is_probability(outputs):
preds = outputs >= .5
else:
preds = outputs >= 0
return (preds == targets.byte()).float().mean()
def multiclass_accuracy(outputs, targets):
if outputs.size(1) == 1:
return binary_accuracy(outputs, targets)
else:
preds = torch.max(outputs, dim=1)[1]
s = (preds == targets).float().sum(0)
return s / outputs.size(0)