Skip to content

Commit cbadaf0

Browse files
bonlimetaehoonlee
authored andcommitted
Merge 2 functions together in applications MobileNetV2 (keras-team#10163)
* merge 2 functions together _inverted_res_block() and _first_inverted_res_block() are nearly the same. Merged them into one function 1. skip "Expand" part for 0 block 2. made layers names similar to original TF graph names * added "mobl_" to prefix, pep8 fix * remove dropout parameter * concise prefix name
1 parent 0ba6d95 commit cbadaf0

File tree

1 file changed

+21
-61
lines changed

1 file changed

+21
-61
lines changed

keras/applications/mobilenetv2.py

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ def _make_divisible(v, divisor, min_value=None):
146146
def MobileNetV2(input_shape=None,
147147
alpha=1.0,
148148
depth_multiplier=1,
149-
dropout=1e-3,
150149
include_top=True,
151150
weights='imagenet',
152151
input_tensor=None,
@@ -180,7 +179,6 @@ def MobileNetV2(input_shape=None,
180179
are used at each layer.
181180
depth_multiplier: depth multiplier for depthwise convolution
182181
(also called the resolution multiplier)
183-
dropout: dropout rate, dropout is currently not in use
184182
include_top: whether to include the fully-connected
185183
layer at the top of the network.
186184
weights: one of `None` (random initialization),
@@ -350,12 +348,8 @@ def MobileNetV2(input_shape=None,
350348
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x)
351349
x = Activation(relu6, name='Conv1_relu')(x)
352350

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)
359353

360354
x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2,
361355
expansion=6, block_id=1)
@@ -454,73 +448,39 @@ def MobileNetV2(input_shape=None,
454448

455449
def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id):
456450
in_channels = inputs._keras_shape[-1]
457-
prefix = 'features.' + str(block_id) + '.conv.'
458451
pointwise_conv_filters = int(filters * alpha)
459452
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_'
469466

470467
# Depthwise
471468
x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None,
472469
use_bias=False, padding='same',
473-
name='mobl%d_conv_depthwise' % block_id)(x)
470+
name=prefix + 'depthwise')(x)
474471
x = BatchNormalization(epsilon=1e-3, momentum=0.999,
475-
name='bn%d_conv_depthwise' % block_id)(x)
472+
name=prefix + 'depthwise_BN')(x)
476473

477-
x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)
474+
x = Activation(relu6, name=prefix + 'depthwise_relu')(x)
478475

479476
# Project
480477
x = Conv2D(pointwise_filters,
481478
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)
519480
x = BatchNormalization(epsilon=1e-3, momentum=0.999,
520-
name='bn%d_conv_project' %
521-
block_id)(x)
481+
name=prefix + 'project_BN')(x)
522482

523483
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])
525485

526486
return x

0 commit comments

Comments
 (0)