-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
380 lines (307 loc) · 12.8 KB
/
utils.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from inspect import signature
from collections import namedtuple
import time
import torch
from torch import nn
import numpy as np
import torchvision
import os
import yaml
import pprint
import csv
torch.backends.cudnn.benchmark = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#####################
# utils
#####################
class Timer():
def __init__(self):
self.times = [time.time()]
self.total_time = 0.0
def __call__(self, include_in_total=True):
self.times.append(time.time())
dt = self.times[-1] - self.times[-2]
if include_in_total:
self.total_time += dt
return dt
localtime = lambda: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
def warmup_cudnn(model, batch_size):
#run forward and backward pass of the model on a batch of random inputs
#to allow benchmarking of cudnn kernels
batch = {
'input': torch.Tensor(np.random.rand(batch_size,3,32,32)).cuda().half(),
'target': torch.LongTensor(np.random.randint(0,10,batch_size)).cuda()
}
model.train(True)
o = model(batch)
o['loss'].backward()
model.zero_grad()
torch.cuda.synchronize()
class TableLogger():
def append(self, output):
if not hasattr(self, 'keys'):
self.keys = output.keys()
print(*(f'{k:>12s}' for k in self.keys))
filtered = [output[k] for k in self.keys]
print(*(f'{v:12.4f}' if isinstance(v, np.float) else f'{v:12}' for v in filtered))
#####################
## data preprocessing
#####################
cifar10_mean = (0.4914, 0.4822, 0.4465) # equals np.mean(train_set.train_data, axis=(0,1,2))/255
cifar10_std = (0.2471, 0.2435, 0.2616) # equals np.std(train_set.train_data, axis=(0,1,2))/255
def normalise(x, mean=cifar10_mean, std=cifar10_std):
x, mean, std = [np.array(a, np.float32) for a in (x, mean, std)]
x -= mean*255
x *= 1.0/(255*std)
return x
def pad(x, border=4):
return np.pad(x, [(0, 0), (border, border), (border, border), (0, 0)], mode='reflect')
def transpose(x, source='NHWC', target='NCHW'):
return x.transpose([source.index(d) for d in target])
#####################
## data augmentation
#####################
class Crop(namedtuple('Crop', ('h', 'w'))):
def __call__(self, x, x0, y0):
return x[:,y0:y0+self.h,x0:x0+self.w]
def options(self, x_shape):
C, H, W = x_shape
return {'x0': range(W+1-self.w), 'y0': range(H+1-self.h)}
def output_shape(self, x_shape):
C, H, W = x_shape
return (C, self.h, self.w)
class FlipLR(namedtuple('FlipLR', ())):
def __call__(self, x, choice):
return x[:, :, ::-1].copy() if choice else x
def options(self, x_shape):
return {'choice': [True, False]}
class Cutout(namedtuple('Cutout', ('h', 'w'))):
def __call__(self, x, x0, y0):
x = x.copy()
x[:,y0:y0+self.h,x0:x0+self.w].fill(0.0)
return x
def options(self, x_shape):
C, H, W = x_shape
return {'x0': range(W+1-self.w), 'y0': range(H+1-self.h)}
class Transform():
def __init__(self, dataset, transforms):
self.dataset, self.transforms = dataset, transforms
self.choices = None
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
data, labels = self.dataset[index]
for choices, f in zip(self.choices, self.transforms):
args = {k: v[index] for (k,v) in choices.items()}
data = f(data, **args)
return data, labels
def set_random_choices(self):
self.choices = []
x_shape = self.dataset[0][0].shape
N = len(self)
for t in self.transforms:
options = t.options(x_shape)
x_shape = t.output_shape(x_shape) if hasattr(t, 'output_shape') else x_shape
self.choices.append({k:np.random.choice(v, size=N) for (k,v) in options.items()})
#####################
## data loading
#####################
class Batches():
def __init__(self, dataset, batch_size, shuffle, num_workers=0, drop_last=False):
self.dataset = dataset
self.dataloader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, num_workers=num_workers, pin_memory=True, shuffle=shuffle, drop_last=drop_last
)
def __iter__(self):
return ({'input': x.to(device).half(), 'target': y.to(device).long()} for (x,y) in self.dataloader)
def __len__(self):
return len(self.dataloader)
#####################
## torch stuff
#####################
class Identity(nn.Module):
def forward(self, x): return x
class Mul(nn.Module):
def __init__(self, weight):
super().__init__()
self.weight = weight
def __call__(self, x):
return x*self.weight
class Flatten(nn.Module):
def forward(self, x): return x.view(x.size(0), x.size(1))
class Add(nn.Module):
def forward(self, x, y): return x + y
class Concat(nn.Module):
def forward(self, *xs): return torch.cat(xs, 1)
class Correct(nn.Module):
def forward(self, classifier, target):
return classifier.max(dim = 1)[1] == target
def batch_norm(num_channels, bn_bias_init=None, bn_bias_freeze=False, bn_weight_init=None, bn_weight_freeze=False, use_bn=True):
m = nn.BatchNorm2d(num_channels)
if bn_bias_init is not None:
m.bias.data.fill_(bn_bias_init)
if bn_bias_freeze:
m.bias.requires_grad = False
if bn_weight_init is not None:
m.weight.data.fill_(bn_weight_init)
if bn_weight_freeze:
m.weight.requires_grad = False
return m
def to_numpy(x):
return x.detach().cpu().numpy()
#####################
## dict utils
#####################
union = lambda *dicts: {k: v for d in dicts for (k, v) in d.items()}
def path_iter(nested_dict, pfx=()):
for name, val in nested_dict.items():
if isinstance(val, dict): yield from path_iter(val, (*pfx, name))
else: yield ((*pfx, name), val)
#####################
## graph building
#####################
sep='_'
RelativePath = namedtuple('RelativePath', ('parts'))
rel_path = lambda *parts: RelativePath(parts)
def build_graph(net):
net = dict(path_iter(net))
default_inputs = [[('input',)]]+[[k] for k in net.keys()]
with_default_inputs = lambda vals: (val if isinstance(val, tuple) else (val, default_inputs[idx]) for idx,val in enumerate(vals))
parts = lambda path, pfx: tuple(pfx) + path.parts if isinstance(path, RelativePath) else (path,) if isinstance(path, str) else path
return {sep.join((*pfx, name)): (val, [sep.join(parts(x, pfx)) for x in inputs]) for (*pfx, name), (val, inputs) in zip(net.keys(), with_default_inputs(net.values()))}
class TorchGraph(nn.Module):
def __init__(self, net):
self.graph = build_graph(net)
super().__init__()
for n, (v, _) in self.graph.items():
setattr(self, n, v)
def forward(self, inputs):
self.cache = dict(inputs)
for n, (_, i) in self.graph.items():
self.cache[n] = getattr(self, n)(*[self.cache[x] for x in i])
return self.cache
def half(self):
for module in self.children():
if type(module) is not nn.BatchNorm2d:
module.half()
return self
#####################
## training utils
#####################
class PiecewiseLinear(namedtuple('PiecewiseLinear', ('knots', 'vals'))):
def __call__(self, t):
return np.interp([t], self.knots, self.vals)[0]
trainable_params = lambda model:filter(lambda p: p.requires_grad, model.parameters())
def nesterov(params, momentum, weight_decay=None):
return torch.optim.SGD(params, lr=0.0, momentum=momentum, weight_decay=weight_decay, nesterov=True)
concat = lambda xs: np.array(xs) if xs[0].shape is () else np.concatenate(xs)
def set_opt_params(optimizer, params):
for k, v in params.items():
optimizer.param_groups[0][k] = v
return optimizer
def update(model, optimizer):
assert model.training
model.cache['loss'].backward()
optimizer.step()
model.zero_grad()
def collect(stats, output):
for k,v in stats.items():
v.append(to_numpy(output[k]))
def train_epoch(model, batches, optimizer, lrs, stats):
model.train(True)
for lr, batch in zip(lrs, batches):
collect(stats, model(batch))
update(model, set_opt_params(optimizer, {'lr': lr}))
return stats
def test_epoch(model, batches, stats):
model.train(False)
for batch in batches:
collect(stats, model(batch))
return stats
sum_ = lambda xs: np.sum(concat(xs), dtype=np.float)
def train(model, lr_schedule, optimizer, train_set, test_set, batch_size=512,
loggers=(), test_time_in_total=True, num_workers=0, drop_last=False, timer=None):
t = timer or Timer()
train_batches = Batches(train_set, batch_size, shuffle=True, num_workers=num_workers, drop_last=drop_last)
test_batches = Batches(test_set, batch_size, shuffle=False, num_workers=num_workers)
N_train, N_test = len(train_set), len(test_set)
if drop_last: N_train -= (N_train % batch_size)
for epoch in range(lr_schedule.knots[-1]):
train_batches.dataset.set_random_choices()
lrs = (lr_schedule(x)/batch_size for x in np.arange(epoch, epoch+1, 1/len(train_batches)))
train_stats, train_time = train_epoch(model, train_batches, optimizer, lrs, {'loss': [], 'correct': []}), t()
test_stats, test_time = test_epoch(model, test_batches, {'loss': [], 'correct': []}), t(test_time_in_total)
summary = {
'epoch': epoch+1,
'lr': lr_schedule(epoch+1),
'train time': train_time,
'train loss': sum_(train_stats['loss'])/N_train,
'train acc': sum_(train_stats['correct'])/N_train,
'test time': test_time,
'test loss': sum_(test_stats['loss'])/N_test,
'test acc': sum_(test_stats['correct'])/N_test,
'total time': t.total_time,
}
for logger in loggers:
logger.append(summary)
return summary
#####################
## network visualisation (requires pydot)
#####################
class ColorMap(dict):
palette = (
'bebada,ffffb3,fb8072,8dd3c7,80b1d3,fdb462,b3de69,fccde5,bc80bd,ccebc5,ffed6f,1f78b4,33a02c,e31a1c,ff7f00,'
'4dddf8,e66493,b07b87,4e90e3,dea05e,d0c281,f0e189,e9e8b1,e0eb71,bbd2a4,6ed641,57eb9c,3ca4d4,92d5e7,b15928'
).split(',')
def __missing__(self, key):
self[key] = self.palette[len(self) % len(self.palette)]
return self[key]
def make_pydot(nodes, edges, direction='LR', sep=sep, **kwargs):
import pydot
parent = lambda path: path[:-1]
stub = lambda path: path[-1]
class Subgraphs(dict):
def __missing__(self, path):
subgraph = pydot.Cluster(sep.join(path), label=stub(path), style='rounded, filled', fillcolor='#77777744')
self[parent(path)].add_subgraph(subgraph)
return subgraph
subgraphs = Subgraphs()
subgraphs[()] = g = pydot.Dot(rankdir=direction, directed=True, **kwargs)
g.set_node_defaults(
shape='box', style='rounded, filled', fillcolor='#ffffff')
for node, attr in nodes:
path = tuple(node.split(sep))
subgraphs[parent(path)].add_node(
pydot.Node(name=node, label=stub(path), **attr))
for src, dst, attr in edges:
g.add_edge(pydot.Edge(src, dst, **attr))
return g
get_params = lambda mod: {p.name: getattr(mod, p.name, '?') for p in signature(type(mod)).parameters.values()}
class DotGraph():
colors = ColorMap()
def __init__(self, net, size=15, direction='LR'):
graph = build_graph(net)
self.nodes = [(k, {
'tooltip': '%s %.1000r' % (type(n).__name__, get_params(n)),
'fillcolor': '#'+self.colors[type(n)],
}) for k, (n, i) in graph.items()]
self.edges = [(src, k, {}) for (k, (n, i)) in graph.items() for src in i]
self.size, self.direction = size, direction
def dot_graph(self, **kwargs):
return make_pydot(self.nodes, self.edges, size=self.size,
direction=self.direction, **kwargs)
def svg(self, **kwargs):
return self.dot_graph(**kwargs).create(format='svg').decode('utf-8')
try:
import pydot
def _repr_svg_(self):
return self.svg()
except ImportError:
def __repr__(self):
return 'pydot is needed for network visualisation'
walk = lambda dict_, key: walk(dict_, dict_[key]) if key in dict_ else key
def remove_identity_nodes(net):
#remove identity nodes for more compact visualisations
graph = build_graph(net)
remap = {k: i[0] for k,(v,i) in graph.items() if isinstance(v, Identity)}
return {k: (v, [walk(remap, x) for x in i]) for k, (v,i) in graph.items() if not isinstance(v, Identity)}