-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·236 lines (182 loc) · 7.05 KB
/
models.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os, sys, random, yaml
from itertools import product
from tqdm import tqdm
import numpy as np
import matplotlib as mpl
from sklearn.utils import shuffle
from inspect import signature
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch import optim
from utils import *
import IPython
""" Model that implements batchwise training with "compilation" and custom loss.
Exposed methods: predict_on_batch(), fit_on_batch(),
Overridable methods: loss(), forward().
"""
class AbstractModel(nn.Module):
def __init__(self):
super().__init__()
self.compiled = False
# Compile module and assign optimizer + params
def compile(self, optimizer=None, **kwargs):
if optimizer is not None:
self.optimizer_class = optimizer
self.optimizer_kwargs = kwargs
self.optimizer = self.optimizer_class(self.parameters(), **self.optimizer_kwargs)
else:
self.optimizer = None
self.compiled = True
self.to(DEVICE)
# Predict scores from a batch of data
def predict_on_batch(self, data):
self.eval()
with torch.no_grad():
return self.forward(data)
# Fit (make one optimizer step) on a batch of data
def fit_on_batch(self, data, target, loss_fn=None, train=True):
loss_fn = loss_fn or self.loss
self.zero_grad()
self.optimizer.zero_grad()
self.train(train)
self.zero_grad()
self.optimizer.zero_grad()
pred = self.forward(data)
if isinstance(target, list):
target = tuple(t.to(pred.device) for t in target)
else: target = target.to(pred.device)
if len(signature(loss_fn).parameters) > 2:
loss, metrics = loss_fn(pred, target, data.to(pred.device))
else:
loss, metrics = loss_fn(pred, target)
if train:
loss.backward()
self.optimizer.step()
self.zero_grad()
self.optimizer.zero_grad()
return pred, loss, metrics
# Make one optimizer step w.r.t a loss
def step(self, loss, train=True):
self.zero_grad()
self.optimizer.zero_grad()
self.train(train)
self.zero_grad()
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.zero_grad()
self.optimizer.zero_grad()
@classmethod
def load(cls, weights_file=None):
model = cls()
if weights_file is not None:
data = torch.load(weights_file)
# hack for models saved with optimizers
if "optimizer" in data: data = data["state_dict"]
model.load_state_dict(data)
return model
def load_weights(self, weights_file, backward_compatible=False):
data = torch.load(weights_file)
if backward_compatible:
data = {'parallel_apply.module.'+k:v for k,v in data.items()}
self.load_state_dict(data)
def save(self, weights_file):
torch.save(self.state_dict(), weights_file)
# Subclasses: override for custom loss + forward functions
def loss(self, pred, target):
raise NotImplementedError()
def forward(self, x):
raise NotImplementedError()
""" Model that implements training and prediction on generator objects, with
the ability to print train and validation metrics.
"""
class TrainableModel(AbstractModel):
def __init__(self):
super().__init__()
# Fit on generator for one epoch
def _process_data(self, datagen, loss_fn=None, train=True, logger=None):
self.train(train)
out = []
for data in datagen:
batch, y = data[0], data[1:]
if len(y) == 1: y = y[0]
y_pred, loss, metric_data = self.fit_on_batch(batch, y, loss_fn=loss_fn, train=train)
if logger is not None:
logger.update("loss", float(loss))
yield ((batch.detach(), y_pred.detach(), y, float(loss), metric_data))
def fit(self, datagen, loss_fn=None, logger=None):
for x in self._process_data(datagen, loss_fn=loss_fn, train=train, logger=logger):
pass
def fit_with_data(self, datagen, loss_fn=None, logger=None):
images, preds, targets, losses, metrics = zip(
*self._process_data(datagen, loss_fn=loss_fn, train=True, logger=logger)
)
images, preds, targets = torch.cat(images, dim=0), torch.cat(preds, dim=0), torch.cat(targets, dim=0)
metrics = zip(*metrics)
return images, preds, targets, losses, metrics
def fit_with_metrics(self, datagen, loss_fn=None, logger=None):
metrics = [
metrics
for _, _, _, _, metrics in self._process_data(
datagen, loss_fn=loss_fn, train=True, logger=logger
)
]
return list(zip(*metrics))
def predict_with_data(self, datagen, loss_fn=None, logger=None):
images, preds, targets, losses, metrics = zip(
*self._process_data(datagen, loss_fn=loss_fn, train=False, logger=logger)
)
images, preds, targets = torch.cat(images, dim=0), torch.cat(preds, dim=0), torch.cat(targets, dim=0)
images, preds, targets = images.cpu(), preds.cpu(), targets.cpu()
# preds = torch.cat(preds, dim=0)
metrics = zip(*metrics)
return images, preds, targets, losses, metrics
def predict_with_metrics(self, datagen, loss_fn=None, logger=None):
metrics = [
metrics
for _, _, _, _, metrics in self._process_data(
datagen, loss_fn=loss_fn, train=False, logger=logger
)
]
return list(zip(*metrics))
def predict(self, datagen):
preds = [self.predict_on_batch(x) for x in datagen]
preds = torch.cat(preds, dim=0)
return preds
class DataParallelModel(TrainableModel):
def __init__(self, *args, **kwargs):
super().__init__()
self.parallel_apply = nn.DataParallel(*args, **kwargs)
def forward(self, *args, **kwargs):
return self.parallel_apply(*args, **kwargs)
def loss(self, x, preds):
return self.parallel_apply.module.loss(x, preds)
@property
def module(self):
return self.parallel_apply.module
@classmethod
def load(cls, model=TrainableModel(), weights_file=None):
model = cls(model)
if weights_file is not None:
data = torch.load(weights_file, map_location=lambda storage, loc: storage)
# hack for models saved with optimizers
if "optimizer" in data: data = data["state_dict"]
model.load_state_dict(data)
return model
class WrapperModel(TrainableModel):
def __init__(self, model):
super().__init__()
self.model = model
def forward(self, *args, **kwargs):
return self.model(*args, **kwargs)
def loss(self, x, preds):
raise NotImplementedError()
def __getitem__(self, i):
return self.model[i]
@property
def module(self):
return self.model
if __name__ == "__main__":
IPython.embed()