forked from jay-mahadeokar/pynetbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid.py
417 lines (346 loc) · 19.4 KB
/
hybrid.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
Copyright 2016 Yahoo Inc.
Licensed under the terms of the 2 clause BSD license.
Please see LICENSE file in the project root for terms.
"""
from base import BaseLego
from base import BaseLegoFunction
from caffe.proto import caffe_pb2
import google.protobuf as pb
from caffe import layers as L
from caffe import params as P
import caffe
from copy import deepcopy
class ConvBNReLULego(BaseLego):
type = 'ConvBNReLU'
def __init__(self, params):
# self._required = ['name', 'kernel_size', 'num_output', 'pad', 'stride', 'use_global_stats']
self._required = ['name', 'num_output', 'use_global_stats']
self._check_required_params(params)
self.convParams = deepcopy(params)
del self.convParams['use_global_stats']
self.convParams['name'] = 'conv_' + params['name']
self.batchNormParams = dict(use_global_stats=params['use_global_stats'],
name='bn_' + params['name'])
self.scaleParams = dict(name='scale_' + params['name'])
self.reluParams = dict(name='relu_' + params['name'])
def attach(self, netspec, bottom):
conv = BaseLegoFunction('Convolution', self.convParams).attach(netspec, bottom)
bn = BaseLegoFunction('BatchNorm', self.batchNormParams).attach(netspec, [conv])
scale = BaseLegoFunction('Scale', self.scaleParams).attach(netspec, [bn])
relu = BaseLegoFunction('ReLU', self.reluParams).attach(netspec, [scale])
return relu
class ConvReLULego(BaseLego):
def __init__(self, params):
self._required = ['name', 'kernel_size', 'num_output', 'pad', 'stride']
self._check_required_params(params)
self.convParams = deepcopy(params)
self.convParams['name'] = 'conv' + params['name']
self.reluParams = dict(name='relu' + params['name'])
def attach(self, netspec, bottom):
conv = BaseLegoFunction('Convolution', self.convParams).attach(netspec, bottom)
relu = BaseLegoFunction('ReLU', self.reluParams).attach(netspec, [conv])
return relu
class ConvBNLego(BaseLego):
type = 'ConvBN'
def __init__(self, params):
self._required = ['name', 'kernel_size', 'num_output', 'pad', 'stride', 'use_global_stats']
self._check_required_params(params)
self.convParams = deepcopy(params)
del self.convParams['use_global_stats']
self.convParams['name'] = 'conv_' + params['name']
self.batchNormParams = dict(use_global_stats=params['use_global_stats'],
name='bn_' + params['name'])
self.scaleParams = dict(name='scale_' + params['name'])
def attach(self, netspec, bottom):
conv = BaseLegoFunction('Convolution', self.convParams).attach(netspec, bottom)
bn = BaseLegoFunction('BatchNorm', self.batchNormParams).attach(netspec, [conv])
scale = BaseLegoFunction('Scale', self.scaleParams).attach(netspec, [bn])
return scale
class EltwiseReLULego(BaseLego):
type = 'EltwiseReLU'
def __init__(self, params):
self._required = ["name"]
self._check_required_params(params)
self.eltwiseParams = dict(name='eltwise_' + params['name'])
self.reluParams = dict(name='relu_' + params['name'])
def attach(self, netspec, bottom):
eltwise = BaseLegoFunction('Eltwise', self.eltwiseParams).attach(netspec, bottom)
relu = BaseLegoFunction('ReLU', self.reluParams).attach(netspec, [eltwise])
return relu
class FireLego(BaseLego):
type = 'Fire'
def __init__(self, params):
self._required = ['name', 'squeeze_num_output', 'use_global_stats']
self._check_required_params(params)
self.name = params['name']
self.squeeze_num_output = params['squeeze_num_output']
self.use_global_stats = params['use_global_stats']
self.downsample = True if 'downsample' in params else False
self.filter_mult = 4
if 'filter_mult' in params:
self.filter_mult = params['filter_mult']
def attach(self, netspec, bottom):
# Squeeze
name = self.name + '_' + 'squeeze_1by1'
sq_params = dict(name=name, num_output=self.squeeze_num_output,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
sq = ConvBNReLULego(sq_params).attach(netspec, bottom)
stride = 2 if self.downsample else 1
# expand
name = self.name + '_' + 'expand_1by1'
exp1_params = dict(name=name, num_output=self.squeeze_num_output * self.filter_mult,
kernel_size=1, pad=0, stride=stride,
use_global_stats=self.use_global_stats)
exp1 = ConvBNReLULego(exp1_params).attach(netspec, [sq])
name = self.name + '_' + 'expand_3by3'
exp2_params = dict(name=name, num_output=self.squeeze_num_output * self.filter_mult,
kernel_size=3, pad=1, stride=stride,
use_global_stats=self.use_global_stats)
exp2 = ConvBNReLULego(exp2_params).attach(netspec, [sq])
# concat
name = self.name + '_' + 'concat'
concat = BaseLegoFunction('Concat', dict(name=name)).attach(netspec, [exp1, exp2])
return concat
class InceptionV1Lego(BaseLego):
type = 'InceptionV1'
def __init__(self, params):
self._required = ['name', 'num_outputs', 'use_global_stats']
self._check_required_params(params)
self.name = params['name']
self.num_outputs = params['num_outputs']
self.use_global_stats = params['use_global_stats']
def attach(self, netspec, bottom):
# branch1by1
name = self.name + '_' + 'br1by1'
params = dict(name=name, num_output=self.num_outputs[0],
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br1by1 = ConvBNReLULego(params).attach(netspec, bottom)
# branch 3by3
name = self.name + '_' + 'br3by3_reduce'
params = dict(name=name, num_output=self.num_outputs[1],
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br3by3_reduce = ConvBNReLULego(params).attach(netspec, bottom)
name = self.name + '_' + 'br3by3_expand'
params = dict(name=name, num_output=self.num_outputs[2],
kernel_size=3, pad=1, stride=1,
use_global_stats=self.use_global_stats)
br3by3_expand = ConvBNReLULego(params).attach(netspec, [br3by3_reduce])
# branch 5by5
name = self.name + '_' + 'br5by5_reduce'
params = dict(name=name, num_output=self.num_outputs[3],
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br5by5_reduce = ConvBNReLULego(params).attach(netspec, bottom)
name = self.name + '_' + 'br5by5_expand'
params = dict(name=name, num_output=self.num_outputs[4],
kernel_size=5, pad=2, stride=1,
use_global_stats=self.use_global_stats)
br5by5_expand = ConvBNReLULego(params).attach(netspec, [br5by5_reduce])
# branch pool
name = self.name + '_' + 'pool_reduce'
params = dict(kernel_size=3, stride=1, pool=P.Pooling.MAX, name=name)
pool = BaseLegoFunction('Pooling', params).attach(netspec, bottom)
name = self.name + '_' + 'pool_expand'
params = dict(name=name, num_output=self.num_outputs[5],
kernel_size=1, pad=1, stride=1,
use_global_stats=self.use_global_stats)
pool_conv = ConvBNReLULego(params).attach(netspec, [pool])
# concat
name = self.name + '_' + 'concat'
concat = BaseLegoFunction('Concat', dict(name=name)).attach(netspec,
[br1by1, br3by3_expand, br5by5_expand, pool_conv ])
return concat
class InceptionLego(BaseLego):
def __init__(self, params):
self._required = ['name', 'num_output', 'use_global_stats', 'downsample']
self._check_required_params(params)
self.name = params['name']
self.num_output = params['num_output']
self.use_global_stats = params['use_global_stats']
self.downsample = params['downsample']
def attach(self, netspec, bottom):
stride = 2 if self.downsample else 1
# branch1by1
name = self.name + '_' + 'br1by1'
params = dict(name=name, num_output=self.num_output / 4,
kernel_size=1, pad=0, stride=stride,
use_global_stats=self.use_global_stats)
br1by1 = ConvBNReLULego(params).attach(netspec, bottom)
# branch 3by3
name = self.name + '_' + 'br3by3_reduce'
params = dict(name=name, num_output=self.num_output * 3 / 16,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br3by3_reduce = ConvBNReLULego(params).attach(netspec, bottom)
name = self.name + '_' + 'br3by3_expand'
params = dict(name=name, num_output=self.num_output / 4,
kernel_size=3, pad=1, stride=stride,
use_global_stats=self.use_global_stats)
br3by3_expand = ConvBNReLULego(params).attach(netspec, [br3by3_reduce])
# branch 2*3by3
name = self.name + '_' + 'br2_3by3_reduce'
params = dict(name=name, num_output=self.num_output * 3 / 16,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br2_3by3_reduce = ConvBNReLULego(params).attach(netspec, bottom)
name = self.name + '_' + 'br2_3by3_expand_1'
params = dict(name=name, num_output=self.num_output / 4,
kernel_size=3, pad=1, stride=1,
use_global_stats=self.use_global_stats)
br2_3by3_expand_1 = ConvBNReLULego(params).attach(netspec, [br2_3by3_reduce])
name = self.name + '_' + 'br2_3by3_expand_2'
params = dict(name=name, num_output=self.num_output / 4,
kernel_size=3, pad=1, stride=stride,
use_global_stats=self.use_global_stats)
br2_3by3_expand_2 = ConvBNReLULego(params).attach(netspec, [br2_3by3_expand_1])
# branch pool
name = self.name + '_' + 'pool'
pad = 0 if self.downsample else 1
params = dict(kernel_size=3, stride=stride, pool='max', name=name, pad=pad)
pool = BaseLegoFunction('Pooling', params).attach(netspec, bottom)
name = self.name + '_' + 'pool_expand'
params = dict(name=name, num_output=self.num_output / 4,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
pool_conv = ConvBNReLULego(params).attach(netspec, [pool])
# concat
name = self.name + '_' + 'concat'
concat = BaseLegoFunction('Concat', dict(name=name)).attach(netspec, [br1by1, br3by3_expand, br2_3by3_expand_2, pool_conv ])
# concat = ConcatLego(dict(name=name)).attach(netspec, [br1by1, br3by3_expand, br2_3by3_expand_2 ])
return concat
class ShortcutLego(BaseLego):
type = 'Shortcut'
def __init__(self, params):
self._required = ['name', 'shortcut', 'main_branch', 'stride', 'num_output', 'use_global_stats']
self._check_required_params(params)
self.name = params['name']
self.shortcut = params['shortcut']
self.stride = params['stride']
self.main_branch = params['main_branch']
self.num_output = params['num_output']
self.use_global_stats = params['use_global_stats']
def attach(self, netspec, bottom):
if self.shortcut == 'identity':
shortcut = bottom[0]
elif self.shortcut == 'projection':
name = self.name + '_proj_shortcut'
num_output = self.num_output
shortcut_params = dict(name=name , num_output=num_output,
kernel_size=1, pad=0, stride=self.stride,
use_global_stats=self.use_global_stats)
shortcut = ConvBNLego(shortcut_params).attach(netspec, bottom)
# Convolution(kernel_w=3,kernel_h=1,num_output=64,pad_w=1)+BatchNorm+Scale+ReLU+
# Convolution(kernel_w=1,kernel_h=3,num_output=64,pad_h=1)+BatchNorm+Scale+ReLU
if self.main_branch == 'inception_trick':
name = self.name + '_branch_3by1a'
num_output = self.num_output
br3by1a_params = dict(name=name, num_output=num_output,
kernel_w=3, kernel_h=1, pad_w=1, stride_w=self.stride, stride_h=1,
use_global_stats=self.use_global_stats)
br3by1a = ConvBNReLULego(br3by1a_params).attach(netspec, bottom)
name = self.name + '_branch_1by3a'
num_output = self.num_output
br1by3a_params = dict(name=name, num_output=num_output,
kernel_w=1, kernel_h=3, pad_h=1, stride_h=self.stride, stride_w=1,
use_global_stats=self.use_global_stats)
br1by3a = ConvBNReLULego(br1by3a_params).attach(netspec, [br3by1a])
name = self.name + '_branch_3by1b'
br3by1b_params = dict(name=name, num_output=num_output,
kernel_w=3, kernel_h=1, pad_w=1, stride=1,
use_global_stats=self.use_global_stats)
br3by1a = ConvBNReLULego(br3by1b_params).attach(netspec, [br1by3a])
name = self.name + '_branch_1by3b'
br1by3a_params = dict(name=name, num_output=num_output,
kernel_w=1, kernel_h=3, pad_h=1, stride=1,
use_global_stats=self.use_global_stats)
br2_out = ConvBNReLULego(br1by3a_params).attach(netspec, [br3by1a])
if self.main_branch == 'inception_trick_bottleneck':
name = self.name + '_branch2a'
num_output = self.num_output
br2a_params = dict(name=name, num_output=num_output / 4,
kernel_size=1, pad=0, stride=self.stride,
use_global_stats=self.use_global_stats)
br2a = ConvBNReLULego(br2a_params).attach(netspec, bottom)
name = self.name + '_branch_b_3by1'
num_output = self.num_output
br3by1a_params = dict(name=name, num_output=num_output / 4,
kernel_w=3, kernel_h=1, pad_w=1, stride=1,
use_global_stats=self.use_global_stats)
br3by1a = ConvBNReLULego(br3by1a_params).attach(netspec, [br2a])
name = self.name + '_branch_b_1by3'
num_output = self.num_output
br1by3a_params = dict(name=name, num_output=num_output / 4,
kernel_w=1, kernel_h=3, pad_h=1, stride=1,
use_global_stats=self.use_global_stats)
br1by3a = ConvBNReLULego(br1by3a_params).attach(netspec, [br3by1a])
name = self.name + '_branch2c'
br2c_params = dict(name=name, num_output=num_output,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br2_out = ConvBNLego(br2c_params).attach(netspec, [br1by3a])
if self.main_branch == 'bottleneck':
name = self.name + '_branch2a'
num_output = self.num_output
br2a_params = dict(name=name, num_output=num_output / 4,
kernel_size=1, pad=0, stride=self.stride,
use_global_stats=self.use_global_stats)
br2a = ConvBNReLULego(br2a_params).attach(netspec, bottom)
name = self.name + '_branch2b'
br2b_params = dict(name=name, num_output=num_output / 4,
kernel_size=3, pad=1, stride=1,
use_global_stats=self.use_global_stats)
br2b = ConvBNReLULego(br2b_params).attach(netspec, [br2a])
name = self.name + '_branch2c'
br2c_params = dict(name=name, num_output=num_output,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br2_out = ConvBNLego(br2c_params).attach(netspec, [br2b])
elif self.main_branch == 'inception':
name = self.name + '_inception'
inception_params = dict(name=name, num_output=self.num_output,
use_global_stats=self.use_global_stats)
inception_params['downsample'] = True if self.shortcut == 'projection' else False
br2_out = InceptionLego(inception_params).attach(netspec, bottom)
elif self.main_branch == '2inception':
name = self.name + '_inception_a'
inception_params_a = dict(name=name, num_output=self.num_output,
use_global_stats=self.use_global_stats,
downsample=False)
inception_a = InceptionLego(inception_params_a).attach(netspec, bottom)
name = self.name + '_inception_b'
inception_params_b = dict(name=name, num_output=self.num_output,
use_global_stats=self.use_global_stats)
inception_params_b['downsample'] = True if self.shortcut == 'projection' else False
br2_out = InceptionLego(inception_params_b).attach(netspec, [inception_a])
elif self.main_branch == 'normal':
name = self.name + '_branch2a'
num_output = self.num_output
br2a_params = dict(name=name, num_output=num_output,
kernel_size=3, pad=1, stride=self.stride,
use_global_stats=self.use_global_stats)
br2a = ConvBNReLULego(br2a_params).attach(netspec, bottom)
name = self.name + '_branch2b'
br2b_params = dict(name=name, num_output=num_output,
kernel_size=3, pad=1, stride=1,
use_global_stats=self.use_global_stats)
br2_out = ConvBNLego(br2b_params).attach(netspec, [br2a])
elif self.main_branch == '1by1_normal':
name = self.name + '_branch2a'
num_output = self.num_output
br2a_params = dict(name=name, num_output=num_output,
kernel_size=1, pad=0, stride=self.stride,
use_global_stats=self.use_global_stats)
br2a = ConvBNReLULego(br2a_params).attach(netspec, bottom)
name = self.name + '_branch2b'
br2b_params = dict(name=name, num_output=num_output,
kernel_size=1, pad=0, stride=1,
use_global_stats=self.use_global_stats)
br2_out = ConvBNLego(br2b_params).attach(netspec, [br2a])
# Combine the branches using EltwiseRelu lego
eltrelu_params = dict(name=self.name)
eltwiseRelu = EltwiseReLULego(eltrelu_params).attach(netspec, [shortcut, br2_out])
return eltwiseRelu