You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/04/18 22:06:36 UTC
[GitHub] zhreshold commented on a change in pull request #10595: Update
mobilenetv2 symbol definition
zhreshold commented on a change in pull request #10595: Update mobilenetv2 symbol definition
URL: https://github.com/apache/incubator-mxnet/pull/10595#discussion_r182583557
##########
File path: example/image-classification/symbols/mobilenetv2.py
##########
@@ -17,60 +17,239 @@
# -*- coding:utf-8 -*-
'''
-MobileNetV2, implemented in Gluon.
+MobileNetV2, implemented in built-in symbols.
Reference:
Inverted Residuals and Linear Bottlenecks:
Mobile Networks for Classification, Detection and Segmentation
https://arxiv.org/abs/1801.04381
'''
-__author__ = 'dwSun'
-__date__ = '18/1/31'
+__author__ = 'liangfu'
+__date__ = '18/4/3'
import mxnet as mx
-from mxnet.gluon.model_zoo.vision.mobilenet import MobileNetV2
+def relu6(data, prefix):
+ return mx.sym.clip(data,0,6,name='%s-relu6'%prefix)
+def shortcut(data_in, data_residual, prefix):
+ out=mx.sym.elemwise_add(data_in, data_residual, name='%s-shortcut'%prefix)
+ return out
-__all__ = ['MobileNetV2', 'get_symbol']
+def mobilenet_unit(data, num_filter=1, kernel=(1, 1), stride=(1, 1), pad=(0, 0), num_group=1, if_act=True, prefix=''):
+ conv = mx.sym.Convolution(
+ data=data,
+ num_filter=num_filter,
+ kernel=kernel,
+ num_group=num_group,
+ stride=stride,
+ pad=pad,
+ no_bias=True,
+ name='%s-conv2d'%prefix)
+ bn = mx.sym.BatchNorm(data=conv, name='%s-batchnorm'%prefix, fix_gamma=False, use_global_stats=False, eps=1e-5)
+ if if_act:
+ act = relu6(bn, prefix)
+ return act
+ else:
+ return bn
+def inverted_residual_unit(data, num_in_filter, num_filter, ifshortcut, stride, kernel, pad, expansion_factor, prefix):
+ num_expfilter = int(round(num_in_filter*expansion_factor))
-def get_symbol(num_classes=1000, multiplier=1.0, ctx=mx.cpu(), **kwargs):
- r"""MobileNetV2 model from the
- `"Inverted Residuals and Linear Bottlenecks:
- Mobile Networks for Classification, Detection and Segmentation"
- <https://arxiv.org/abs/1801.04381>`_ paper.
+ channel_expand = mobilenet_unit(
+ data=data,
+ num_filter=num_expfilter,
+ kernel=(1,1),
+ stride=(1,1),
+ pad=(0,0),
+ num_group=1,
+ if_act=True,
+ prefix='%s-exp'%prefix,
+ )
+ bottleneck_conv = mobilenet_unit(
+ data= channel_expand,
+ num_filter=num_expfilter,
+ stride=stride,
+ kernel=kernel,
+ pad=pad,
+ num_group=num_expfilter,
+ if_act=True,
+ prefix='%s-depthwise'%prefix,
+ )
+ linear_out = mobilenet_unit(
+ data=bottleneck_conv,
+ num_filter=num_filter,
+ kernel=(1, 1),
+ stride=(1, 1),
+ pad=(0, 0),
+ num_group=1,
+ if_act=False,
+ prefix='%s-linear'%prefix
+ )
+ if ifshortcut:
+ out = shortcut(
+ data_in=data,
+ data_residual=linear_out,
+ prefix=prefix,
+ )
+ return out
+ else:
+ return linear_out
- Parameters
- ----------
- num_classes : int, default 1000
- Number of classes for the output layer.
- multiplier : float, default 1.0
- The width multiplier for controling the model size. The actual number of channels
- is equal to the original channel size multiplied by this multiplier.
- ctx : Context, default CPU
- The context in which to initialize the model weights.
- """
- net = MobileNetV2(multiplier=multiplier, classes=num_classes, **kwargs)
- net.initialize(ctx=ctx, init=mx.init.Xavier())
- net.hybridize()
+def invresi_blocks(data, in_c, t, c, n, s, prefix):
+ first_block = inverted_residual_unit(
+ data=data,
+ num_in_filter=in_c,
+ num_filter=c,
+ ifshortcut=False,
+ stride=(s,s),
+ kernel=(3,3),
+ pad=(1,1),
+ expansion_factor=t,
+ prefix='%s-block0'%prefix
+ )
- data = mx.sym.var('data')
- out = net(data)
- sym = mx.sym.SoftmaxOutput(out, name='softmax')
- return sym
+ last_residual_block = first_block
+ last_c = c
+ for i in range(1,n):
+ last_residual_block = inverted_residual_unit(
+ data=last_residual_block,
+ num_in_filter=last_c,
+ num_filter=c,
+ ifshortcut=True,
+ stride=(1,1),
+ kernel=(3,3),
+ pad=(1,1),
+ expansion_factor=t,
+ prefix='%s-block%d'%(prefix, i)
+ )
+ return last_residual_block
-def plot_net():
- """
- Visualize the network.
- """
- sym = get_symbol(1000, prefix='mob_')
+MNETV2_CONFIGS_MAP = {
+ (224,224):{
+ 'firstconv_filter_num': 32, # 3*224*224 -> 32*112*112
+ # t, c, n, s
+ 'bottleneck_params_list':[
+ (1, 16, 1, 1), # 32x112x112 -> 16x112x112
+ (6, 24, 2, 2), # 16x112x112 -> 24x56x56
+ (6, 32, 3, 2), # 24x56x56 -> 32x28x28
+ (6, 64, 4, 1), # 32x28x28 -> 64x28x28
+ (6, 96, 3, 2), # 64x28x28 -> 96x14x14
+ (6, 160, 3, 2), # 96x14x14 -> 160x7x7
+ (6, 320, 1, 1), # 160x7x7 -> 320x7x7
+ ],
+ 'filter_num_before_gp': 1280, # 320x7x7 -> 1280x7x7
+ }
+}
- # plot network graph
- mx.viz.plot_network(sym, shape={'data': (8, 3, 224, 224)},
- node_attrs={'shape': 'oval', 'fixedsize': 'fasl==false'}).view()
+class MNetV2Gen(object):
Review comment:
use MobileNetv2
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services