-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathcore.py
299 lines (239 loc) · 9.47 KB
/
core.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
from inspect import signature
import copy
from collections import namedtuple, defaultdict
import time
import numpy as np
import pandas as pd
from functools import singledispatch
#####################
# utils
#####################
class Timer():
def __init__(self, synch=None):
self.synch = synch or (lambda: None)
self.synch()
self.times = [time.perf_counter()]
self.total_time = 0.0
def __call__(self, include_in_total=True):
self.synch()
self.times.append(time.perf_counter())
delta_t = self.times[-1] - self.times[-2]
if include_in_total:
self.total_time += delta_t
return delta_t
localtime = lambda: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
default_table_formats = {float: '{:{w}.4f}', str: '{:>{w}s}', 'default': '{:{w}}', 'title': '{:>{w}s}'}
def table_formatter(val, is_title=False, col_width=12, formats=None):
formats = formats or default_table_formats
type_ = lambda val: float if isinstance(val, (float, np.float)) else type(val)
return (formats['title'] if is_title else formats.get(type_(val), formats['default'])).format(val, w=col_width)
def every(n, col):
return lambda data: data[col] % n == 0
class Table():
def __init__(self, keys=None, report=(lambda data: True), formatter=table_formatter):
self.keys, self.report, self.formatter = keys, report, formatter
self.log = []
def append(self, data):
self.log.append(data)
data = {' '.join(p): v for p,v in path_iter(data)}
self.keys = self.keys or data.keys()
if len(self.log) is 1:
print(*(self.formatter(k, True) for k in self.keys))
if self.report(data):
print(*(self.formatter(data[k]) for k in self.keys))
def df(self):
return pd.DataFrame([{'_'.join(p): v for p,v in path_iter(row)} for row in self.log])
#####################
## data preprocessing
#####################
def preprocess(dataset, transforms):
dataset = copy.copy(dataset) #shallow copy
for transform in transforms:
dataset['data'] = transform(dataset['data'])
return dataset
@singledispatch
def normalise(x, mean, std):
return (x - mean) / std
@normalise.register(np.ndarray)
def _(x, mean, std):
#faster inplace for numpy arrays
x = np.array(x, np.float32)
x -= mean
x *= 1.0/std
return x
unnormalise = lambda x, mean, std: x*std + mean
@singledispatch
def pad(x, border):
raise NotImplementedError
@pad.register(np.ndarray)
def _(x, border):
return np.pad(x, [(0, 0), (border, border), (border, border), (0, 0)], mode='reflect')
@singledispatch
def transpose(x, source, target):
raise NotImplementedError
@transpose.register(np.ndarray)
def _(x, source, target):
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, shape):
*_, H, W = shape
return [{'x0': x0, 'y0': y0} for x0 in range(W+1-self.w) for y0 in range(H+1-self.h)]
def output_shape(self, shape):
*_, H, W = shape
return (*_, self.h, self.w)
@singledispatch
def flip_lr(x):
raise NotImplementedError
@flip_lr.register(np.ndarray)
def _(x):
return x[..., ::-1].copy()
class FlipLR(namedtuple('FlipLR', ())):
def __call__(self, x, choice):
return flip_lr(x) if choice else x
def options(self, shape):
return [{'choice': b} for b in [True, False]]
class Cutout(namedtuple('Cutout', ('h', 'w'))):
def __call__(self, x, x0, y0):
x[..., y0:y0+self.h, x0:x0+self.w] = 0.0
return x
def options(self, shape):
*_, H, W = shape
return [{'x0': x0, 'y0': y0} for x0 in range(W+1-self.w) for y0 in 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]
data = data.copy()
for choices, f in zip(self.choices, self.transforms):
data = f(data, **choices[index])
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:
self.choices.append(np.random.choice(t.options(x_shape), N))
x_shape = t.output_shape(x_shape) if hasattr(t, 'output_shape') else x_shape
#####################
## 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)
def map_nested(func, nested_dict):
return {k: map_nested(func, v) if isinstance(v, dict) else func(v) for k,v in nested_dict.items()}
def group_by_key(items):
res = defaultdict(list)
for k, v in items:
res[k].append(v)
return res
#####################
## graph building
#####################
sep = '/'
def split(path):
i = path.rfind(sep) + 1
return path[:i].rstrip(sep), path[i:]
def normpath(path):
#simplified os.path.normpath
parts = []
for p in path.split(sep):
if p == '..': parts.pop()
elif p.startswith(sep): parts = [p]
else: parts.append(p)
return sep.join(parts)
has_inputs = lambda node: type(node) is tuple
def pipeline(net):
return [(sep.join(path), (node if has_inputs(node) else (node, [-1]))) for (path, node) in path_iter(net)]
def build_graph(net):
flattened = pipeline(net)
resolve_input = lambda rel_path, path, idx: normpath(sep.join((path, '..', rel_path))) if isinstance(rel_path, str) else flattened[idx+rel_path][0]
return {path: (node[0], [resolve_input(rel_path, path, idx) for rel_path in node[1]]) for idx, (path, node) in enumerate(flattened)}
#####################
## training utils
#####################
@singledispatch
def cat(*xs):
raise NotImplementedError
@singledispatch
def to_numpy(x):
raise NotImplementedError
class PiecewiseLinear(namedtuple('PiecewiseLinear', ('knots', 'vals'))):
def __call__(self, t):
return np.interp([t], self.knots, self.vals)[0]
class Const(namedtuple('Const', ['val'])):
def __call__(self, x):
return self.val
#####################
## network visualisation (requires pydot)
#####################
class ColorMap(dict):
palette = ['#'+x for x in (
'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 _repr_html_(self):
css = (
'.pill {'
'margin:2px; border-width:1px; border-radius:9px; border-style:solid;'
'display:inline-block; width:100px; height:15px; line-height:15px;'
'}'
'.pill_text {'
'width:90%; margin:auto; font-size:9px; text-align:center; overflow:hidden;'
'}'
)
s = '<div class=pill style="background-color:{}"><div class=pill_text>{}</div></div>'
return '<style>'+css+'</style>'+''.join((s.format(color, text) for text, color in self.items()))
def make_dot_graph(nodes, edges, direction='LR', **kwargs):
from pydot import Dot, Cluster, Node, Edge
class Subgraphs(dict):
def __missing__(self, path):
parent, label = split(path)
subgraph = Cluster(path, label=label, style='rounded, filled', fillcolor='#77777744')
self[parent].add_subgraph(subgraph)
return subgraph
g = Dot(rankdir=direction, directed=True, **kwargs)
g.set_node_defaults(
shape='box', style='rounded, filled', fillcolor='#ffffff')
subgraphs = Subgraphs({'': g})
for path, attr in nodes:
parent, label = split(path)
subgraphs[parent].add_node(
Node(name=path, label=label, **attr))
for src, dst, attr in edges:
g.add_edge(Edge(src, dst, **attr))
return g
class DotGraph():
def __init__(self, graph, size=15, direction='LR'):
self.nodes = [(k, v) for k, (v,_) in graph.items()]
self.edges = [(src, dst, {}) for dst, (_, inputs) in graph.items() for src in inputs]
self.size, self.direction = size, direction
def dot_graph(self, **kwargs):
return make_dot_graph(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
_repr_svg_ = svg
except ImportError:
def __repr__(self): return 'pydot is needed for network visualisation'
walk = lambda dct, key: walk(dct, dct[key]) if key in dct else key
def remove_by_type(net, node_type):
#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, node_type)}
return {k: (v, [walk(remap, x) for x in i]) for k, (v,i) in graph.items() if not isinstance(v, node_type)}