@@ -146,7 +146,6 @@ def _make_divisible(v, divisor, min_value=None):
146
146
def MobileNetV2 (input_shape = None ,
147
147
alpha = 1.0 ,
148
148
depth_multiplier = 1 ,
149
- dropout = 1e-3 ,
150
149
include_top = True ,
151
150
weights = 'imagenet' ,
152
151
input_tensor = None ,
@@ -180,7 +179,6 @@ def MobileNetV2(input_shape=None,
180
179
are used at each layer.
181
180
depth_multiplier: depth multiplier for depthwise convolution
182
181
(also called the resolution multiplier)
183
- dropout: dropout rate, dropout is currently not in use
184
182
include_top: whether to include the fully-connected
185
183
layer at the top of the network.
186
184
weights: one of `None` (random initialization),
@@ -350,12 +348,8 @@ def MobileNetV2(input_shape=None,
350
348
x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 , name = 'bn_Conv1' )(x )
351
349
x = Activation (relu6 , name = 'Conv1_relu' )(x )
352
350
353
- x = _first_inverted_res_block (x ,
354
- filters = 16 ,
355
- alpha = alpha ,
356
- stride = 1 ,
357
- expansion = 1 ,
358
- block_id = 0 )
351
+ x = _inverted_res_block (x , filters = 16 , alpha = alpha , stride = 1 ,
352
+ expansion = 1 , block_id = 0 )
359
353
360
354
x = _inverted_res_block (x , filters = 24 , alpha = alpha , stride = 2 ,
361
355
expansion = 6 , block_id = 1 )
@@ -454,73 +448,39 @@ def MobileNetV2(input_shape=None,
454
448
455
449
def _inverted_res_block (inputs , expansion , stride , alpha , filters , block_id ):
456
450
in_channels = inputs ._keras_shape [- 1 ]
457
- prefix = 'features.' + str (block_id ) + '.conv.'
458
451
pointwise_conv_filters = int (filters * alpha )
459
452
pointwise_filters = _make_divisible (pointwise_conv_filters , 8 )
460
- # Expand
461
-
462
- x = Conv2D (expansion * in_channels , kernel_size = 1 , padding = 'same' ,
463
- use_bias = False , activation = None ,
464
- name = 'mobl%d_conv_expand' % block_id )(inputs )
465
- x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
466
- name = 'bn%d_conv_bn_expand' %
467
- block_id )(x )
468
- x = Activation (relu6 , name = 'conv_%d_relu' % block_id )(x )
453
+ x = inputs
454
+ prefix = 'block_{}_' .format (block_id )
455
+
456
+ if block_id :
457
+ # Expand
458
+ x = Conv2D (expansion * in_channels , kernel_size = 1 , padding = 'same' ,
459
+ use_bias = False , activation = None ,
460
+ name = prefix + 'expand' )(x )
461
+ x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
462
+ name = prefix + 'expand_BN' )(x )
463
+ x = Activation (relu6 , name = prefix + 'expand_relu' )(x )
464
+ else :
465
+ prefix = 'expanded_conv_'
469
466
470
467
# Depthwise
471
468
x = DepthwiseConv2D (kernel_size = 3 , strides = stride , activation = None ,
472
469
use_bias = False , padding = 'same' ,
473
- name = 'mobl%d_conv_depthwise' % block_id )(x )
470
+ name = prefix + 'depthwise' )(x )
474
471
x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
475
- name = 'bn%d_conv_depthwise' % block_id )(x )
472
+ name = prefix + 'depthwise_BN' )(x )
476
473
477
- x = Activation (relu6 , name = 'conv_dw_%d_relu' % block_id )(x )
474
+ x = Activation (relu6 , name = prefix + 'depthwise_relu' )(x )
478
475
479
476
# Project
480
477
x = Conv2D (pointwise_filters ,
481
478
kernel_size = 1 , padding = 'same' , use_bias = False , activation = None ,
482
- name = 'mobl%d_conv_project' % block_id )(x )
483
- x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
484
- name = 'bn%d_conv_bn_project' % block_id )(x )
485
-
486
- if in_channels == pointwise_filters and stride == 1 :
487
- return Add (name = 'res_connect_' + str (block_id ))([inputs , x ])
488
-
489
- return x
490
-
491
-
492
- def _first_inverted_res_block (inputs ,
493
- expansion , stride ,
494
- alpha , filters , block_id ):
495
- in_channels = inputs ._keras_shape [- 1 ]
496
- prefix = 'features.' + str (block_id ) + '.conv.'
497
- pointwise_conv_filters = int (filters * alpha )
498
- pointwise_filters = _make_divisible (pointwise_conv_filters , 8 )
499
-
500
- # Depthwise
501
- x = DepthwiseConv2D (kernel_size = 3 ,
502
- strides = stride , activation = None ,
503
- use_bias = False , padding = 'same' ,
504
- name = 'mobl%d_conv_depthwise' %
505
- block_id )(inputs )
506
- x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
507
- name = 'bn%d_conv_depthwise' %
508
- block_id )(x )
509
- x = Activation (relu6 , name = 'conv_dw_%d_relu' % block_id )(x )
510
-
511
- # Project
512
- x = Conv2D (pointwise_filters ,
513
- kernel_size = 1 ,
514
- padding = 'same' ,
515
- use_bias = False ,
516
- activation = None ,
517
- name = 'mobl%d_conv_project' %
518
- block_id )(x )
479
+ name = prefix + 'project' )(x )
519
480
x = BatchNormalization (epsilon = 1e-3 , momentum = 0.999 ,
520
- name = 'bn%d_conv_project' %
521
- block_id )(x )
481
+ name = prefix + 'project_BN' )(x )
522
482
523
483
if in_channels == pointwise_filters and stride == 1 :
524
- return Add (name = 'res_connect_' + str ( block_id ) )([inputs , x ])
484
+ return Add (name = prefix + 'add' )([inputs , x ])
525
485
526
486
return x
0 commit comments