-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathconvnet_drawer.py
381 lines (291 loc) · 13.8 KB
/
convnet_drawer.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
381
import math
from abc import ABCMeta, abstractmethod
import config
class Line:
def __init__(self, x1, y1, x2, y2, color=(0, 0, 0), width=1, dasharray=None):
self.x1, self.y1 = x1, y1
self.x2, self.y2 = x2, y2
self.color = color
self.width = width
self.dasharray = dasharray
def get_svg_string(self):
stroke_dasharray = self.dasharray if self.dasharray else "none"
return '<line x1="{}" y1="{}" x2="{}" y2="{}" stroke-width="{}" stroke-dasharray="{}" stroke="rgb{}"/>\n'.format(
self.x1, self.y1, self.x2, self.y2, self.width, stroke_dasharray, self.color)
class Text:
def __init__(self, x, y, body, color=(0, 0, 0), size=20):
self.x = x
self.y = y
self.body = body
self.color = color
self.size = size
def get_svg_string(self):
return '<text x="{}" y="{}" font-family="arial" font-size="{}px" ' \
'text-anchor="middle" fill="rgb{}">{}</text>\n'.format(self.x, self.y, self.size, self.color, self.body)
class Model:
def __init__(self, input_shape):
self.layers = []
if len(input_shape) != 3:
raise ValueError("input_shape should be rank 3 but received {}".format(input_shape))
self.feature_maps = []
self.x = None
self.y = None
self.width = None
self.height = None
self.feature_maps.append(FeatureMap3D(*input_shape))
def add_feature_map(self, layer):
if isinstance(self.feature_maps[-1], FeatureMap3D):
h, w = self.feature_maps[-1].h, self.feature_maps[-1].w
filters = layer.filters if layer.filters else self.feature_maps[-1].c
if isinstance(layer, GlobalAveragePooling2D):
self.feature_maps.append(FeatureMap1D(filters))
elif isinstance(layer, Flatten):
self.feature_maps.append(FeatureMap1D(h * w * filters))
elif isinstance(layer, Deconv2D):
if layer.padding == "same":
new_h = h * layer.strides[0]
new_w = w * layer.strides[1]
else:
new_h = h * layer.strides[0] + max(layer.kernel_size[0] - layer.strides[0], 0)
new_w = w * layer.strides[1] + max(layer.kernel_size[1] - layer.strides[1], 0)
self.feature_maps.append(FeatureMap3D(new_h, new_w, filters))
else:
if layer.padding == "same":
new_h = math.ceil(h / layer.strides[0])
new_w = math.ceil(w / layer.strides[1])
else:
new_h = math.ceil((h - layer.kernel_size[0] + 1) / layer.strides[0])
new_w = math.ceil((w - layer.kernel_size[1] + 1) / layer.strides[1])
self.feature_maps.append(FeatureMap3D(new_h, new_w, filters))
else:
self.feature_maps.append(FeatureMap1D(layer.filters))
def add(self, layer):
self.add_feature_map(layer)
layer.prev_feature_map = self.feature_maps[-2]
layer.next_feature_map = self.feature_maps[-1]
self.layers.append(layer)
def build(self):
left = 0
for feature_map in self.feature_maps:
right = feature_map.set_objects(left)
left = right + config.inter_layer_margin
for i, layer in enumerate(self.layers):
layer.set_objects()
# get bounding box
self.x = - config.bounding_box_margin - 30
self.y = min([f.get_top() for f in self.feature_maps]) - config.text_margin - config.text_size \
- config.bounding_box_margin
self.width = self.feature_maps[-1].right + config.bounding_box_margin * 2 + 30 * 2
# TODO: automatically calculate the ad-hoc offset "30" from description length
self.height = - self.y * 2 + config.text_size
def save_fig(self, filename):
self.build()
string = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' \
'width= "{}" height="{}" '.format(self.width, self.height) + \
'viewBox="{} {} {} {}">\n'.format(self.x, self.y, self.width, self.height)
for feature_map in self.feature_maps:
string += feature_map.get_object_string()
for layer in self.layers:
string += layer.get_object_string()
string += '</svg>'
f = open(filename, 'w')
f.write(string)
f.close()
class FeatureMap:
__metaclass__ = ABCMeta
def __init__(self):
self.left = None
self.right = None
self.objects = None
@abstractmethod
def set_objects(self, left):
pass
def get_object_string(self):
return get_object_string(self.objects)
@abstractmethod
def get_top(self):
pass
@abstractmethod
def get_bottom(self):
pass
class FeatureMap3D(FeatureMap):
def __init__(self, h, w, c):
self.h = h
self.w = w
self.c = c
super(FeatureMap3D, self).__init__()
def set_objects(self, left):
self.left = left
c_ = math.pow(self.c, config.channel_scale)
self.right, self.objects = get_rectangular(self.h, self.w, c_, left, config.line_color_feature_map)
x = (left + self.right) / 2
y = self.get_top() - config.text_margin
self.objects.append(Text(x, y, "{}x{}x{}".format(self.h, self.w, self.c), color=config.text_color_feature_map,
size=config.text_size))
return self.right
def get_left_for_conv(self):
return self.left + self.w * config.ratio * math.cos(config.theta) / 2
def get_top(self):
return - self.h / 2 + self.w * config.ratio * math.sin(config.theta) / 2
def get_bottom(self):
return self.h / 2 - self.w * config.ratio * math.sin(config.theta) / 2
def get_right_for_conv(self):
x = self.left + self.w * config.ratio * math.cos(config.theta) / 4
y = - self.h / 4 + self.w * config.ratio * math.sin(config.theta) / 4
return x, y
class FeatureMap1D(FeatureMap):
def __init__(self, c):
self.c = c
super(FeatureMap1D, self).__init__()
def set_objects(self, left):
self.left = left
c_ = math.pow(self.c, config.channel_scale)
self.right = left + config.one_dim_width
# TODO: reflect text length to right
x1 = left
y1 = - c_ / 2
x2 = left + config.one_dim_width
y2 = c_ / 2
line_color = config.line_color_feature_map
self.objects = []
self.objects.append(Line(x1, y1, x1, y2, line_color))
self.objects.append(Line(x1, y2, x2, y2, line_color))
self.objects.append(Line(x2, y2, x2, y1, line_color))
self.objects.append(Line(x2, y1, x1, y1, line_color))
self.objects.append(Text(left + config.one_dim_width / 2, - c_ / 2 - config.text_margin, "{}".format(
self.c), color=config.text_color_feature_map, size=config.text_size))
return self.right
def get_top(self):
return - math.pow(self.c, config.channel_scale) / 2
def get_bottom(self):
return math.pow(self.c, config.channel_scale) / 2
class Layer:
__metaclass__ = ABCMeta
def __init__(self, filters=None, kernel_size=None, strides=(1, 1), padding="valid"):
self.filters = filters
self.kernel_size = kernel_size
self.strides = strides
self.padding = padding
self.objects = []
self.prev_feature_map = None
self.next_feature_map = None
self.description = None
@abstractmethod
def get_description(self):
return None
def set_objects(self):
c = math.pow(self.prev_feature_map.c, config.channel_scale)
left = self.prev_feature_map.get_left_for_conv()
start1 = (left + c,
-self.kernel_size[0] + self.kernel_size[1] * config.ratio * math.sin(config.theta) / 2
+ self.kernel_size[0] / 2)
start2 = (left + c + self.kernel_size[1] * config.ratio * math.cos(config.theta),
-self.kernel_size[1] * config.ratio * math.sin(config.theta) / 2 + self.kernel_size[0] / 2)
end = self.next_feature_map.get_right_for_conv()
line_color = config.line_color_layer
left, self.objects = get_rectangular(self.kernel_size[0], self.kernel_size[1], c, left, color=line_color)
self.objects.append(Line(start1[0], start1[1], end[0], end[1], color=line_color))
self.objects.append(Line(start2[0], start2[1], end[0], end[1], color=line_color))
x = (self.prev_feature_map.right + self.next_feature_map.left) / 2
y = max(self.prev_feature_map.get_bottom(), self.next_feature_map.get_bottom()) + config.text_margin \
+ config.text_size
for i, description in enumerate(self.get_description()):
self.objects.append(Text(x, y + i * config.text_size, "{}".format(description),
color=config.text_color_layer, size=config.text_size))
def get_object_string(self):
return get_object_string(self.objects)
class Conv2D(Layer):
def get_description(self):
return ["conv{}x{}, {}".format(self.kernel_size[0], self.kernel_size[1], self.filters),
"stride {}".format(self.strides)]
class Deconv2D(Layer):
def get_description(self):
return ["deconv{}x{}, {}".format(self.kernel_size[0], self.kernel_size[1], self.filters),
"stride {}".format(self.strides)]
class PoolingLayer(Layer):
def __init__(self, pool_size=(2, 2), strides=None, padding="valid"):
if not strides:
strides = pool_size
super(PoolingLayer, self).__init__(kernel_size=pool_size, strides=strides, padding=padding)
class AveragePooling2D(PoolingLayer):
def get_description(self):
return ["avepool{}x{}".format(self.kernel_size[0], self.kernel_size[1]),
"stride {}".format(self.strides)]
class MaxPooling2D(PoolingLayer):
def get_description(self):
return ["maxpool{}x{}".format(self.kernel_size[0], self.kernel_size[1]),
"stride {}".format(self.strides)]
class GlobalAveragePooling2D(Layer):
def __init__(self):
super(GlobalAveragePooling2D, self).__init__()
def get_description(self):
return ["global avepool"]
def set_objects(self):
x = (self.prev_feature_map.right + self.next_feature_map.left) / 2
y = max(self.prev_feature_map.get_bottom(), self.next_feature_map.get_bottom()) + config.text_margin \
+ config.text_size
for i, description in enumerate(self.get_description()):
self.objects.append(Text(x, y + i * config.text_size, "{}".format(description),
color=config.text_color_layer, size=config.text_size))
class Flatten(Layer):
def __init__(self):
super(Flatten, self).__init__()
def get_description(self):
return ["flatten"]
def set_objects(self):
x = (self.prev_feature_map.right + self.next_feature_map.left) / 2
y = max(self.prev_feature_map.get_bottom(), self.next_feature_map.get_bottom()) + config.text_margin \
+ config.text_size
for i, description in enumerate(self.get_description()):
self.objects.append(Text(x, y + i * config.text_size, "{}".format(description),
color=config.text_color_layer, size=config.text_size))
class Dense(Layer):
def __init__(self, units):
super(Dense, self).__init__(filters=units)
def get_description(self):
return ["dense"]
def set_objects(self):
x1 = self.prev_feature_map.right
y11 = - math.pow(self.prev_feature_map.c, config.channel_scale) / 2
y12 = math.pow(self.prev_feature_map.c, config.channel_scale) / 2
x2 = self.next_feature_map.left
y2 = - math.pow(self.next_feature_map.c, config.channel_scale) / 4
line_color = config.line_color_layer
self.objects.append(Line(x1, y11, x2, y2, color=line_color, dasharray=2))
self.objects.append(Line(x1, y12, x2, y2, color=line_color, dasharray=2))
x = (self.prev_feature_map.right + self.next_feature_map.left) / 2
y = max(self.prev_feature_map.get_bottom(), self.next_feature_map.get_bottom()) + config.text_margin \
+ config.text_size
for i, description in enumerate(self.get_description()):
self.objects.append(Text(x, y + i * config.text_size, "{}".format(description),
color=config.text_color_layer, size=config.text_size))
def get_rectangular(h, w, c, dx=0, color=(0, 0, 0)):
p = [[0, -h],
[w * config.ratio * math.cos(config.theta), -w * config.ratio * math.sin(config.theta)],
[c, 0]]
dy = w * config.ratio * math.sin(config.theta) / 2 + h / 2
right = dx + w * config.ratio * math.cos(config.theta) + c
lines = []
for i, [x1, y1] in enumerate(p):
for x2, y2 in [[0, 0], p[(i + 1) % 3]]:
for x3, y3 in [[0, 0], p[(i + 2) % 3]]:
lines.append(Line(x2 + x3 + dx, y2 + y3 + dy, x1 + x2 + x3 + dx, y1 + y2 + y3 + dy,
color=color))
for i in [1, 6, 8]:
lines[i].dasharray = 1
return right, lines
def get_object_string(objects):
return "".join([obj.get_svg_string() for obj in objects])
def main():
model = Model(input_shape=(128, 128, 3))
model.add(Conv2D(32, (11, 11), (2, 2), padding="same"))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (7, 7), padding="same"))
model.add(AveragePooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, (3, 3), padding="same"))
model.add(Conv2D(512, (3, 3), padding="same"))
model.save_fig("test.svg")
if __name__ == '__main__':
main()