forked from soeaver/caffe-model
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinception_resnet.py
567 lines (495 loc) · 38.5 KB
/
inception_resnet.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import caffe
from caffe import layers as L
from caffe import params as P
def stem_299x299(bottom):
"""
input:3x299x299
output:384x35x35
:param bottom: bottom layer
:return: layers
"""
conv1_3x3_s2 = L.Convolution(bottom, kernel_size=3, num_output=32, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 32x149x149
conv1_3x3_s2_bn = L.BatchNorm(conv1_3x3_s2, use_global_stats=False, in_place=True)
conv1_3x3_s2_scale = L.Scale(conv1_3x3_s2, scale_param=dict(bias_term=True), in_place=True)
conv1_3x3_s2_relu = L.ReLU(conv1_3x3_s2, in_place=True)
conv2_3x3_s1 = L.Convolution(conv1_3x3_s2, kernel_size=3, num_output=32, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 32x147x147
conv2_3x3_s1_bn = L.BatchNorm(conv2_3x3_s1, use_global_stats=False, in_place=True)
conv2_3x3_s1_scale = L.Scale(conv2_3x3_s1, scale_param=dict(bias_term=True), in_place=True)
conv2_3x3_s1_relu = L.ReLU(conv2_3x3_s1, in_place=True)
conv3_3x3_s1 = L.Convolution(conv2_3x3_s1, kernel_size=3, num_output=64, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 64x147x147
conv3_3x3_s1_bn = L.BatchNorm(conv3_3x3_s1, use_global_stats=False, in_place=True)
conv3_3x3_s1_scale = L.Scale(conv3_3x3_s1, scale_param=dict(bias_term=True), in_place=True)
conv3_3x3_s1_relu = L.ReLU(conv3_3x3_s1, in_place=True)
inception_stem1_3x3_s2 = L.Convolution(conv3_3x3_s1, kernel_size=3, num_output=96, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 96x73x73
inception_stem1_3x3_s2_bn = L.BatchNorm(inception_stem1_3x3_s2, use_global_stats=False, in_place=True)
inception_stem1_3x3_s2_scale = L.Scale(inception_stem1_3x3_s2, scale_param=dict(bias_term=True),
in_place=True)
inception_stem1_3x3_s2_relu = L.ReLU(inception_stem1_3x3_s2, in_place=True)
inception_stem1_pool = L.Pooling(conv3_3x3_s1, kernel_size=3, stride=2,
pool=P.Pooling.MAX) # 64x73x73
inception_stem1 = L.Concat(inception_stem1_3x3_s2, inception_stem1_pool) # 160x73x73
inception_stem2_3x3_reduce = L.Convolution(inception_stem1, kernel_size=1, num_output=64,
param=[dict(lr_mult=1, decay_mult=1),
dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 64x73x73
inception_stem2_3x3_reduce_bn = L.BatchNorm(inception_stem2_3x3_reduce, use_global_stats=False,
in_place=True)
inception_stem2_3x3_reduce_scale = L.Scale(inception_stem2_3x3_reduce,
scale_param=dict(bias_term=True), in_place=True)
inception_stem2_3x3_reduce_relu = L.ReLU(inception_stem2_3x3_reduce, in_place=True)
inception_stem2_3x3 = L.Convolution(inception_stem2_3x3_reduce, kernel_size=3, num_output=96,
param=[dict(lr_mult=1, decay_mult=1),
dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 96x71x71
inception_stem2_3x3_bn = L.BatchNorm(inception_stem2_3x3, use_global_stats=False, in_place=True)
inception_stem2_3x3_scale = L.Scale(inception_stem2_3x3, scale_param=dict(bias_term=True), in_place=True)
inception_stem2_3x3_relu = L.ReLU(inception_stem2_3x3, in_place=True)
inception_stem2_7x1_reduce = L.Convolution(inception_stem1, kernel_size=1, num_output=64,
param=[dict(lr_mult=1, decay_mult=1),
dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 64x73x73
inception_stem2_7x1_reduce_bn = L.BatchNorm(inception_stem2_7x1_reduce, use_global_stats=False,
in_place=True)
inception_stem2_7x1_reduce_scale = L.Scale(inception_stem2_7x1_reduce,
scale_param=dict(bias_term=True), in_place=True)
inception_stem2_7x1_reduce_relu = L.ReLU(inception_stem2_7x1_reduce, in_place=True)
inception_stem2_7x1 = L.Convolution(inception_stem2_7x1_reduce, kernel_h=7, kernel_w=1, num_output=64,
pad_h=3, pad_w=0, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 64x73x73
inception_stem2_7x1_bn = L.BatchNorm(inception_stem2_7x1, use_global_stats=False, in_place=True)
inception_stem2_7x1_scale = L.Scale(inception_stem2_7x1, scale_param=dict(bias_term=True), in_place=True)
inception_stem2_7x1_relu = L.ReLU(inception_stem2_7x1, in_place=True)
inception_stem2_1x7 = L.Convolution(inception_stem2_7x1, kernel_h=1, kernel_w=7, num_output=64,
pad_h=0, pad_w=3, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 64x73x73
inception_stem2_1x7_bn = L.BatchNorm(inception_stem2_1x7, use_global_stats=False, in_place=True)
inception_stem2_1x7_scale = L.Scale(inception_stem2_1x7, scale_param=dict(bias_term=True), in_place=True)
inception_stem2_1x7_relu = L.ReLU(inception_stem2_1x7, in_place=True)
inception_stem2_3x3_2 = L.Convolution(inception_stem2_1x7, kernel_size=3, num_output=96,
param=[dict(lr_mult=1, decay_mult=1),
dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 96x71x71
inception_stem2_3x3_2_bn = L.BatchNorm(inception_stem2_3x3_2, use_global_stats=False, in_place=True)
inception_stem2_3x3_2_scale = L.Scale(inception_stem2_3x3_2, scale_param=dict(bias_term=True),
in_place=True)
inception_stem2_3x3_2_relu = L.ReLU(inception_stem2_3x3_2, in_place=True)
inception_stem2 = L.Concat(inception_stem2_3x3, inception_stem2_3x3_2) # 192x71x71
inception_stem3_3x3_s2 = L.Convolution(inception_stem2, kernel_size=3, num_output=192, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier', std=0.01),
bias_filler=dict(type='constant', value=0.2)) # 192x35x35
inception_stem3_3x3_s2_bn = L.BatchNorm(inception_stem3_3x3_s2, use_global_stats=False, in_place=True)
inception_stem3_3x3_s2_scale = L.Scale(inception_stem3_3x3_s2, scale_param=dict(bias_term=True),
in_place=True)
inception_stem3_3x3_s2_relu = L.ReLU(inception_stem3_3x3_s2, in_place=True)
inception_stem3_pool = L.Pooling(inception_stem2, kernel_size=3, stride=2,
pool=P.Pooling.MAX) # 192x35x35
inception_stem3 = L.Concat(inception_stem3_3x3_s2, inception_stem3_pool) # 384x35x35
return conv1_3x3_s2, conv1_3x3_s2_bn, conv1_3x3_s2_scale, conv1_3x3_s2_relu, conv2_3x3_s1, conv2_3x3_s1_bn, \
conv2_3x3_s1_scale, conv2_3x3_s1_relu, conv3_3x3_s1, conv3_3x3_s1_bn, conv3_3x3_s1_scale, conv3_3x3_s1_relu, \
inception_stem1_3x3_s2, inception_stem1_3x3_s2_bn, inception_stem1_3x3_s2_scale, inception_stem1_3x3_s2_relu, \
inception_stem1_pool, inception_stem1, inception_stem2_3x3_reduce, inception_stem2_3x3_reduce_bn, \
inception_stem2_3x3_reduce_scale, inception_stem2_3x3_reduce_relu, inception_stem2_3x3, \
inception_stem2_3x3_bn, inception_stem2_3x3_scale, inception_stem2_3x3_relu, inception_stem2_7x1_reduce, \
inception_stem2_7x1_reduce_bn, inception_stem2_7x1_reduce_scale, inception_stem2_7x1_reduce_relu, \
inception_stem2_7x1, inception_stem2_7x1_bn, inception_stem2_7x1_scale, inception_stem2_7x1_relu, \
inception_stem2_1x7, inception_stem2_1x7_bn, inception_stem2_1x7_scale, inception_stem2_1x7_relu, \
inception_stem2_3x3_2, inception_stem2_3x3_2_bn, inception_stem2_3x3_2_scale, inception_stem2_3x3_2_relu, \
inception_stem2, inception_stem3_3x3_s2, inception_stem3_3x3_s2_bn, inception_stem3_3x3_s2_scale, \
inception_stem3_3x3_s2_relu, inception_stem3_pool, inception_stem3
def factorization_inception_resnet_a(bottom):
"""
input:384x35x35
output:384x35x35
:param bottom: bottom layer
:return: layers
"""
conv_1x1 = L.Convolution(bottom, kernel_size=1, num_output=32, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 32x35x35
conv_1x1_bn = L.BatchNorm(conv_1x1, use_global_stats=False, in_place=True)
conv_1x1_scale = L.Scale(conv_1x1, scale_param=dict(bias_term=True), in_place=True)
conv_1x1_relu = L.ReLU(conv_1x1, in_place=True)
conv_3x3_reduce = L.Convolution(bottom, kernel_size=1, num_output=32, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 32x35x35
conv_3x3_reduce_bn = L.BatchNorm(conv_3x3_reduce, use_global_stats=False, in_place=True)
conv_3x3_reduce_scale = L.Scale(conv_3x3_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_reduce_relu = L.ReLU(conv_3x3_reduce, in_place=True)
conv_3x3 = L.Convolution(conv_3x3_reduce, kernel_size=3, num_output=32, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 32x35x35
conv_3x3_bn = L.BatchNorm(conv_3x3, use_global_stats=False, in_place=True)
conv_3x3_scale = L.Scale(conv_3x3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_relu = L.ReLU(conv_3x3, in_place=True)
conv_3x3_2_reduce = L.Convolution(bottom, kernel_size=1, num_output=32, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 32x35x35
conv_3x3_2_reduce_bn = L.BatchNorm(conv_3x3_2_reduce, use_global_stats=False, in_place=True)
conv_3x3_2_reduce_scale = L.Scale(conv_3x3_2_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_reduce_relu = L.ReLU(conv_3x3_2_reduce, in_place=True)
conv_3x3_2 = L.Convolution(conv_3x3_2_reduce, kernel_size=3, num_output=48, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 48x35x35
conv_3x3_2_bn = L.BatchNorm(conv_3x3_2, use_global_stats=False, in_place=True)
conv_3x3_2_scale = L.Scale(conv_3x3_2, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_relu = L.ReLU(conv_3x3_2, in_place=True)
conv_3x3_3 = L.Convolution(conv_3x3_2, kernel_size=3, num_output=64, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 64x35x35
conv_3x3_3_bn = L.BatchNorm(conv_3x3_3, use_global_stats=False, in_place=True)
conv_3x3_3_scale = L.Scale(conv_3x3_3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_3_relu = L.ReLU(conv_3x3_3, in_place=True)
concat = L.Concat(conv_1x1, conv_3x3, conv_3x3_3) # 128x35x35
conv_1x1_2 = L.Convolution(concat, kernel_size=1, num_output=384, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 384x35x35
conv_1x1_2_bn = L.BatchNorm(conv_1x1_2, use_global_stats=False, in_place=True)
conv_1x1_2_scale = L.Scale(conv_1x1_2, scale_param=dict(bias_term=True), in_place=True)
# conv_1x1_2_relu = L.ReLU(conv_1x1_2_scale, in_place=True) # linear activation
residual_eltwise = L.Eltwise(bottom, conv_1x1_2,
eltwise_param=dict(operation=1))
return conv_1x1, conv_1x1_bn, conv_1x1_scale, conv_1x1_relu, conv_3x3_reduce, conv_3x3_reduce_bn, \
conv_3x3_reduce_scale, conv_3x3_reduce_relu, conv_3x3, conv_3x3_bn, conv_3x3_scale, conv_3x3_relu, \
conv_3x3_2_reduce, conv_3x3_2_reduce_bn, conv_3x3_2_reduce_scale, conv_3x3_2_reduce_relu, \
conv_3x3_2, conv_3x3_2_bn, conv_3x3_2_scale, conv_3x3_2_relu, conv_3x3_3, conv_3x3_3_bn, conv_3x3_3_scale, \
conv_3x3_3_relu, concat, conv_1x1_2, conv_1x1_2_bn, conv_1x1_2_scale, residual_eltwise
def reduction_a(bottom):
"""
input:384x35x35
output:1152x17x17
:param bottom: bottom layer
:return: layers
"""
pool = L.Pooling(bottom, kernel_size=3, stride=2, pool=P.Pooling.MAX) # 384x17x17
conv_3x3 = L.Convolution(bottom, kernel_size=3, num_output=384, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 384x17x17
conv_3x3_bn = L.BatchNorm(conv_3x3, use_global_stats=False, in_place=True)
conv_3x3_scale = L.Scale(conv_3x3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_relu = L.ReLU(conv_3x3, in_place=True)
conv_3x3_2_reduce = L.Convolution(bottom, kernel_size=1, num_output=256, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x35x35
conv_3x3_2_reduce_bn = L.BatchNorm(conv_3x3_2_reduce, use_global_stats=False, in_place=True)
conv_3x3_2_reduce_scale = L.Scale(conv_3x3_2_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_reduce_relu = L.ReLU(conv_3x3_2_reduce, in_place=True)
conv_3x3_2 = L.Convolution(conv_3x3_2_reduce, kernel_size=3, num_output=256, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x35x35
conv_3x3_2_bn = L.BatchNorm(conv_3x3_2, use_global_stats=False, in_place=True)
conv_3x3_2_scale = L.Scale(conv_3x3_2, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_relu = L.ReLU(conv_3x3_2, in_place=True)
conv_3x3_3 = L.Convolution(conv_3x3_2, kernel_size=3, num_output=384, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 384x17x17
conv_3x3_3_bn = L.BatchNorm(conv_3x3_3, use_global_stats=False, in_place=True)
conv_3x3_3_scale = L.Scale(conv_3x3_3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_3_relu = L.ReLU(conv_3x3_3, in_place=True)
concat = L.Concat(pool, conv_3x3, conv_3x3_3) # 1152x17x17
return pool, conv_3x3, conv_3x3_bn, conv_3x3_scale, conv_3x3_relu, conv_3x3_2_reduce, conv_3x3_2_reduce_bn, \
conv_3x3_2_reduce_scale, conv_3x3_2_reduce_relu, conv_3x3_2, conv_3x3_2_bn, conv_3x3_2_scale, \
conv_3x3_2_relu, conv_3x3_3, conv_3x3_3_bn, conv_3x3_3_scale, conv_3x3_3_relu, concat
def factorization_inception_resnet_b(bottom):
"""
input:1152x17x17
output:1152x17x17
:param bottom: bottom layer
:return: layers
"""
conv_1x1 = L.Convolution(bottom, kernel_size=1, num_output=192, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 192x17x17
conv_1x1_bn = L.BatchNorm(conv_1x1, use_global_stats=False, in_place=True)
conv_1x1_scale = L.Scale(conv_1x1, scale_param=dict(bias_term=True), in_place=True)
conv_1x1_relu = L.ReLU(conv_1x1, in_place=True)
conv_1x7_reduce = L.Convolution(bottom, kernel_size=1, num_output=128, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 128x17x17
conv_1x7_reduce_bn = L.BatchNorm(conv_1x7_reduce, use_global_stats=False, in_place=True)
conv_1x7_reduce_scale = L.Scale(conv_1x7_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_1x7_reduce_relu = L.ReLU(conv_1x7_reduce, in_place=True)
conv_1x7 = L.Convolution(conv_1x7_reduce, kernel_h=1, kernel_w=7, pad_h=0, pad_w=3, stride=1, num_output=160,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 160x17x17
conv_1x7_bn = L.BatchNorm(conv_1x7, use_global_stats=False, in_place=True)
conv_1x7_scale = L.Scale(conv_1x7, scale_param=dict(bias_term=True), in_place=True)
conv_1x7_relu = L.ReLU(conv_1x7, in_place=True)
conv_7x1 = L.Convolution(conv_1x7, kernel_h=7, kernel_w=1, pad_h=3, pad_w=0, stride=1, num_output=192,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 192x17x17
conv_7x1_bn = L.BatchNorm(conv_7x1, use_global_stats=False, in_place=True)
conv_7x1_scale = L.Scale(conv_7x1, scale_param=dict(bias_term=True), in_place=True)
conv_7x1_relu = L.ReLU(conv_7x1, in_place=True)
concat = L.Concat(conv_1x1, conv_7x1) # 384x17x17
conv_1x1_2 = L.Convolution(concat, kernel_size=1, num_output=1152, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 1152x17x17
conv_1x1_2_bn = L.BatchNorm(conv_1x1_2, use_global_stats=False, in_place=True)
conv_1x1_2_scale = L.Scale(conv_1x1_2, scale_param=dict(bias_term=True), in_place=True)
# conv_1x1_2_relu = L.ReLU(conv_1x1_2_scale, in_place=True) # linear activation
residual_eltwise = L.Eltwise(bottom, conv_1x1_2,
eltwise_param=dict(operation=1))
return conv_1x1, conv_1x1_bn, conv_1x1_scale, conv_1x1_relu, conv_1x7_reduce, conv_1x7_reduce_bn, \
conv_1x7_reduce_scale, conv_1x7_reduce_relu, conv_1x7, conv_1x7_bn, conv_1x7_scale, conv_1x7_relu, \
conv_7x1, conv_7x1_bn, conv_7x1_scale, conv_7x1_relu, concat, conv_1x1_2, conv_1x1_2_bn, conv_1x1_2_scale, \
residual_eltwise
def reduction_b(bottom):
"""
input:1152x17x17
output:2048x8x8
:param bottom: bottom layer
:return: layers
"""
pool = L.Pooling(bottom, kernel_size=3, stride=2, pool=P.Pooling.MAX) # 1152x8x8
conv_3x3_reduce = L.Convolution(bottom, kernel_size=1, num_output=256, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x17x17
conv_3x3_reduce_bn = L.BatchNorm(conv_3x3_reduce, use_global_stats=False, in_place=True)
conv_3x3_reduce_scale = L.Scale(conv_3x3_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_reduce_relu = L.ReLU(conv_3x3_reduce, in_place=True)
conv_3x3 = L.Convolution(conv_3x3_reduce, kernel_size=3, num_output=384, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 384x8x8
conv_3x3_bn = L.BatchNorm(conv_3x3, use_global_stats=False, in_place=True)
conv_3x3_scale = L.Scale(conv_3x3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_relu = L.ReLU(conv_3x3, in_place=True)
conv_3x3_2_reduce = L.Convolution(bottom, kernel_size=1, num_output=256, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x17x17
conv_3x3_2_reduce_bn = L.BatchNorm(conv_3x3_2_reduce, use_global_stats=False, in_place=True)
conv_3x3_2_reduce_scale = L.Scale(conv_3x3_2_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_reduce_relu = L.ReLU(conv_3x3_2_reduce, in_place=True)
conv_3x3_2 = L.Convolution(conv_3x3_2_reduce, kernel_size=3, num_output=256, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x8x8
conv_3x3_2_bn = L.BatchNorm(conv_3x3_2, use_global_stats=False, in_place=True)
conv_3x3_2_scale = L.Scale(conv_3x3_2, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_2_relu = L.ReLU(conv_3x3_2, in_place=True)
conv_3x3_3_reduce = L.Convolution(bottom, kernel_size=1, num_output=256, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x17x17
conv_3x3_3_reduce_bn = L.BatchNorm(conv_3x3_3_reduce, use_global_stats=False, in_place=True)
conv_3x3_3_reduce_scale = L.Scale(conv_3x3_3_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_3_reduce_relu = L.ReLU(conv_3x3_3_reduce, in_place=True)
conv_3x3_3 = L.Convolution(conv_3x3_3_reduce, kernel_size=3, num_output=256, stride=1, pad=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x17x17
conv_3x3_3_bn = L.BatchNorm(conv_3x3_3, use_global_stats=False, in_place=True)
conv_3x3_3_scale = L.Scale(conv_3x3_3, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_3_relu = L.ReLU(conv_3x3_3, in_place=True)
conv_3x3_4 = L.Convolution(conv_3x3_3, kernel_size=3, num_output=256, stride=2,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x8x8
conv_3x3_4_bn = L.BatchNorm(conv_3x3_4, use_global_stats=False, in_place=True)
conv_3x3_4_scale = L.Scale(conv_3x3_4, scale_param=dict(bias_term=True), in_place=True)
conv_3x3_4_relu = L.ReLU(conv_3x3_4, in_place=True)
concat = L.Concat(pool, conv_3x3, conv_3x3_2, conv_3x3_4) # 2048x8x8
return pool, conv_3x3_reduce, conv_3x3_reduce_bn, conv_3x3_reduce_scale, conv_3x3_reduce_relu, conv_3x3, \
conv_3x3_bn, conv_3x3_scale, conv_3x3_relu, conv_3x3_2_reduce, conv_3x3_2_reduce_bn, \
conv_3x3_2_reduce_scale, conv_3x3_2_reduce_relu, conv_3x3_2, conv_3x3_2_bn, conv_3x3_2_scale, \
conv_3x3_2_relu, conv_3x3_3_reduce, conv_3x3_3_reduce_bn, conv_3x3_3_reduce_scale, conv_3x3_3_reduce_relu, \
conv_3x3_3, conv_3x3_3_bn, conv_3x3_3_scale, conv_3x3_3_relu, conv_3x3_4, conv_3x3_4_bn, conv_3x3_4_scale, \
conv_3x3_4_relu, concat
def factorization_inception_resnet_c(bottom):
"""
input:2048x8x8
output:2048x8x8
:param bottom: bottom layer
:return: layers
"""
conv_1x1 = L.Convolution(bottom, kernel_size=1, num_output=192, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 192x8x8
conv_1x1_bn = L.BatchNorm(conv_1x1, use_global_stats=False, in_place=True)
conv_1x1_scale = L.Scale(conv_1x1, scale_param=dict(bias_term=True), in_place=True)
conv_1x1_relu = L.ReLU(conv_1x1, in_place=True)
conv_1x3_reduce = L.Convolution(bottom, kernel_size=1, num_output=192, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 192x8x8
conv_1x3_reduce_bn = L.BatchNorm(conv_1x3_reduce, use_global_stats=False, in_place=True)
conv_1x3_reduce_scale = L.Scale(conv_1x3_reduce, scale_param=dict(bias_term=True), in_place=True)
conv_1x3_reduce_relu = L.ReLU(conv_1x3_reduce, in_place=True)
conv_1x3 = L.Convolution(conv_1x3_reduce, kernel_h=1, kernel_w=3, pad_h=0, pad_w=1, stride=1, num_output=224,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 224x8x8
conv_1x3_bn = L.BatchNorm(conv_1x3, use_global_stats=False, in_place=True)
conv_1x3_scale = L.Scale(conv_1x3, scale_param=dict(bias_term=True), in_place=True)
conv_1x3_relu = L.ReLU(conv_1x3, in_place=True)
conv_3x1 = L.Convolution(conv_1x3, kernel_h=3, kernel_w=1, pad_h=1, pad_w=0, stride=1, num_output=256,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 256x8x8
conv_3x1_bn = L.BatchNorm(conv_3x1, use_global_stats=False, in_place=True)
conv_3x1_scale = L.Scale(conv_3x1, scale_param=dict(bias_term=True), in_place=True)
conv_3x1_relu = L.ReLU(conv_3x1, in_place=True)
concat = L.Concat(conv_1x1, conv_3x1) # 448x8x8
conv_1x1_2 = L.Convolution(concat, kernel_size=1, num_output=2048, stride=1,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0)) # 2048x8x8
conv_1x1_2_bn = L.BatchNorm(conv_1x1_2, use_global_stats=False, in_place=True)
conv_1x1_2_scale = L.Scale(conv_1x1_2, scale_param=dict(bias_term=True), in_place=True)
# conv_1x1_2_relu = L.ReLU(conv_1x1_2_scale, in_place=True) # linear activation
residual_eltwise = L.Eltwise(bottom, conv_1x1_2,
eltwise_param=dict(operation=1))
return conv_1x1, conv_1x1_bn, conv_1x1_scale, conv_1x1_relu, conv_1x3_reduce, conv_1x3_reduce_bn, \
conv_1x3_reduce_scale, conv_1x3_reduce_relu, conv_1x3, conv_1x3_bn, conv_1x3_scale, conv_1x3_relu, \
conv_3x1, conv_3x1_bn, conv_3x1_scale, conv_3x1_relu, concat, conv_1x1_2, conv_1x1_2_bn, conv_1x1_2_scale, \
residual_eltwise
inception_resnet_a = 'n.inception_resnet_a(order)_1x1, n.inception_resnet_a(order)_1x1_bn, n.inception_resnet_a(order)_1x1_scale, \
n.inception_resnet_a(order)_1x1_relu, n.inception_resnet_a(order)_3x3_reduce, n.inception_resnet_a(order)_3x3_reduce_bn, \
n.inception_resnet_a(order)_3x3_reduce_scale, n.inception_resnet_a(order)_3x3_reduce_relu, n.inception_resnet_a(order)_3x3, \
n.inception_resnet_a(order)_3x3_bn, n.inception_resnet_a(order)_3x3_scale, n.inception_resnet_a(order)_3x3_relu, \
n.inception_resnet_a(order)_3x3_2_reduce, n.inception_resnet_a(order)_3x3_2_reduce_bn, n.inception_resnet_a(order)_3x3_2_reduce_scale, \
n.inception_resnet_a(order)_3x3_2_reduce_relu, n.inception_resnet_a(order)_3x3_2, n.inception_resnet_a(order)_3x3_2_bn, \
n.inception_resnet_a(order)_3x3_2_scale, n.inception_resnet_a(order)_3x3_2_relu, n.inception_resnet_a(order)_3x3_3, \
n.inception_resnet_a(order)_3x3_3_bn, n.inception_resnet_a(order)_3x3_3_scale, n.inception_resnet_a(order)_3x3_3_relu, \
n.inception_resnet_a(order)_concat, n.inception_resnet_a(order)_1x1_2, n.inception_resnet_a(order)_1x1_2_bn, \
n.inception_resnet_a(order)_1x1_2_scale, n.inception_resnet_a(order)_residual_eltwise = \
factorization_inception_resnet_a(bottom)'
inception_resnet_b = 'n.inception_resnet_b(order)_1x1, n.inception_resnet_b(order)_1x1_bn, n.inception_resnet_b(order)_1x1_scale, \
n.inception_resnet_b(order)_1x1_relu, n.inception_resnet_b(order)_1x7_reduce, n.inception_resnet_b(order)_1x7_reduce_bn, \
n.inception_resnet_b(order)_1x7_reduce_scale, n.inception_resnet_b(order)_1x7_reduce_relu, n.inception_resnet_b(order)_1x7, \
n.inception_resnet_b(order)_1x7_bn, n.inception_resnet_b(order)_1x7_scale, n.inception_resnet_b(order)_1x7_relu, \
n.inception_resnet_b(order)_7x1, n.inception_resnet_b(order)_7x1_bn, n.inception_resnet_b(order)_7x1_scale, \
n.inception_resnet_b(order)_7x1_relu, n.inception_resnet_b(order)_concat, n.inception_resnet_b(order)_1x1_2, \
n.inception_resnet_b(order)_1x1_2_bn, n.inception_resnet_b(order)_1x1_2_scale, n.inception_resnet_b(order)_residual_eltwise \
= factorization_inception_resnet_b(bottom)'
inception_resnet_c = 'n.inception_resnet_c(order)_1x1, n.inception_resnet_c(order)_1x1_bn, n.inception_resnet_c(order)_1x1_scale, \
n.inception_resnet_c(order)_1x1_relu, n.inception_resnet_c(order)_1x3_reduce, n.inception_resnet_c(order)_1x3_reduce_bn, \
n.inception_resnet_c(order)_1x3_reduce_scale, n.inception_resnet_c(order)_1x3_reduce_relu, n.inception_resnet_c(order)_1x3, \
n.inception_resnet_c(order)_1x3_bn, n.inception_resnet_c(order)_1x3_scale, n.inception_resnet_c(order)_1x3_relu, \
n.inception_resnet_c(order)_3x1, n.inception_resnet_c(order)_3x1_bn, n.inception_resnet_c(order)_3x1_scale, \
n.inception_resnet_c(order)_3x1_relu, n.inception_resnet_c(order)_concat, n.inception_resnet_c(order)_1x1_2, \
n.inception_resnet_c(order)_1x1_2_bn, n.inception_resnet_c(order)_1x1_2_scale, n.inception_resnet_c(order)_residual_eltwise = \
factorization_inception_resnet_c(bottom)'
class InceptionResNet(object):
def __init__(self, lmdb_train, lmdb_test, num_output):
self.train_data = lmdb_train
self.test_data = lmdb_test
self.classifier_num = num_output
def inception_resnet_v2_proto(self, batch_size, phase='TRAIN'):
n = caffe.NetSpec()
if phase == 'TRAIN':
source_data = self.train_data
mirror = True
else:
source_data = self.test_data
mirror = False
n.data, n.label = L.Data(source=source_data, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
transform_param=dict(crop_size=299, mean_value=[104, 117, 123], mirror=mirror))
# stem
n.conv1_3x3_s2, n.conv1_3x3_s2_bn, n.conv1_3x3_s2_scale, n.conv1_3x3_s2_relu, n.conv2_3x3_s1, n.conv2_3x3_s1_bn, \
n.conv2_3x3_s1_scale, n.conv2_3x3_s1_relu, n.conv3_3x3_s1, n.conv3_3x3_s1_bn, n.conv3_3x3_s1_scale, n.conv3_3x3_s1_relu, \
n.inception_stem1_3x3_s2, n.inception_stem1_3x3_s2_bn, n.inception_stem1_3x3_s2_scale, n.inception_stem1_3x3_s2_relu, \
n.inception_stem1_pool, n.inception_stem1, n.inception_stem2_3x3_reduce, n.inception_stem2_3x3_reduce_bn, \
n.inception_stem2_3x3_reduce_scale, n.inception_stem2_3x3_reduce_relu, n.inception_stem2_3x3, \
n.inception_stem2_3x3_bn, n.inception_stem2_3x3_scale, n.inception_stem2_3x3_relu, n.inception_stem2_7x1_reduce, \
n.inception_stem2_7x1_reduce_bn, n.inception_stem2_7x1_reduce_scale, n.inception_stem2_7x1_reduce_relu, \
n.inception_stem2_7x1, n.inception_stem2_7x1_bn, n.inception_stem2_7x1_scale, n.inception_stem2_7x1_relu, \
n.inception_stem2_1x7, n.inception_stem2_1x7_bn, n.inception_stem2_1x7_scale, n.inception_stem2_1x7_relu, \
n.inception_stem2_3x3_2, n.inception_stem2_3x3_2_bn, n.inception_stem2_3x3_2_scale, n.inception_stem2_3x3_2_relu, \
n.inception_stem2, n.inception_stem3_3x3_s2, n.inception_stem3_3x3_s2_bn, n.inception_stem3_3x3_s2_scale, \
n.inception_stem3_3x3_s2_relu, n.inception_stem3_pool, n.inception_stem3 = \
stem_299x299(n.data) # 384x35x35
# 5 x inception_resnet_a
for i in xrange(5):
if i == 0:
bottom = 'n.inception_stem3'
else:
bottom = 'n.inception_resnet_a(order)_residual_eltwise'.replace('(order)', str(i))
exec (inception_resnet_a.replace('(order)', str(i + 1)).replace('bottom', bottom)) # 384x35x35
# reduction_a
n.reduction_a_pool, n.reduction_a_3x3, n.reduction_a_3x3_bn, n.reduction_a_3x3_scale, n.reduction_a_3x3_relu, \
n.reduction_a_3x3_2_reduce, n.reduction_a_3x3_2_reduce_bn, n.reduction_a_3x3_2_reduce_scale, \
n.reduction_a_3x3_2_reduce_relu, n.reduction_a_3x3_2, n.reduction_a_3x3_2_bn, n.reduction_a_3x3_2_scale, \
n.reduction_a_3x3_2_relu, n.reduction_a_3x3_3, n.reduction_a_3x3_3_bn, n.reduction_a_3x3_3_scale, \
n.reduction_a_3x3_3_relu, n.reduction_a_concat = \
reduction_a(n.inception_resnet_a5_residual_eltwise) # 1152x17x17
# 10 x inception_resnet_b
for i in xrange(10):
if i == 0:
bottom = 'n.reduction_a_concat'
else:
bottom = 'n.inception_resnet_b(order)_residual_eltwise'.replace('(order)', str(i))
exec (inception_resnet_b.replace('(order)', str(i + 1)).replace('bottom', bottom)) # 1152x17x17
# reduction_b
n.reduction_b_pool, n.reduction_b_3x3_reduce, n.reduction_b_3x3_reduce_bn, n.reduction_b_3x3_reduce_scale, \
n.reduction_b_3x3_reduce_relu, n.reduction_b_3x3, n.reduction_b_3x3_bn, n.reduction_b_3x3_scale, \
n.reduction_b_3x3_relu, n.reduction_b_3x3_2_reduce, n.reduction_b_3x3_2_reduce_bn, n.reduction_b_3x3_2_reduce_scale, \
n.reduction_b_3x3_2_reduce_relu, n.reduction_b_3x3_2, n.reduction_b_3x3_2_bn, n.reduction_b_3x3_2_scale, \
n.reduction_b_3x3_2_relu, n.reduction_b_3x3_3_reduce, n.reduction_b_3x3_3_reduce_bn, n.reduction_b_3x3_3_reduce_scale, \
n.reduction_b_3x3_3_reduce_relu, n.reduction_b_3x3_3, n.reduction_b_3x3_3_bn, n.reduction_b_3x3_3_scale, \
n.reduction_b_3x3_3_relu, n.reduction_b_3x3_4, n.reduction_b_3x3_4_bn, n.reduction_b_3x3_4_scale, \
n.reduction_b_3x3_4_relu, n.reduction_b_concat = \
reduction_b(n.inception_resnet_b10_residual_eltwise) # 2048x8x8
# 5 x inception_resnet_c
for i in xrange(5):
if i == 0:
bottom = 'n.reduction_b_concat'
else:
bottom = 'n.inception_resnet_c(order)_residual_eltwise'.replace('(order)', str(i))
exec (inception_resnet_c.replace('(order)', str(i + 1)).replace('bottom', bottom)) # 2048x8x8
n.pool_8x8_s1 = L.Pooling(n.inception_resnet_c5_residual_eltwise,
pool=P.Pooling.AVE,
global_pooling=True) # 2048x1x1
n.pool_8x8_s1_drop = L.Dropout(n.pool_8x8_s1, dropout_param=dict(dropout_ratio=0.2))
n.classifier = L.InnerProduct(n.pool_8x8_s1_drop, num_output=self.classifier_num,
param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
weight_filler=dict(type='xavier'),
bias_filler=dict(type='constant', value=0))
n.loss = L.SoftmaxWithLoss(n.classifier, n.label)
if phase == 'TRAIN':
pass
else:
n.accuracy_top1 = L.Accuracy(n.classifier, n.label, include=dict(phase=1))
n.accuracy_top5 = L.Accuracy(n.classifier, n.label, include=dict(phase=1),
accuracy_param=dict(top_k=5))
return n.to_proto()