zhreshold closed pull request #10603: fix MobileNetV2 structure according to 
1801.04381 v3
URL: https://github.com/apache/incubator-mxnet/pull/10603
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/mxnet/gluon/model_zoo/vision/mobilenet.py 
b/python/mxnet/gluon/model_zoo/vision/mobilenet.py
index 7c3b7d643eb..5b4c9a8e615 100644
--- a/python/mxnet/gluon/model_zoo/vision/mobilenet.py
+++ b/python/mxnet/gluon/model_zoo/vision/mobilenet.py
@@ -23,7 +23,7 @@
            'mobilenet_v2_0_25', 'get_mobilenet', 'get_mobilenet_v2']
 
 __modify__ = 'dwSun'
-__modified_date__ = '18/01/31'
+__modified_date__ = '18/04/18'
 
 import os
 
@@ -33,17 +33,29 @@
 
 
 # Helpers
+class RELU6(nn.HybridBlock):
+    """Relu6 used in MobileNetV2."""
+
+    def __init__(self, **kwargs):
+        super(RELU6, self).__init__(**kwargs)
+
+    def hybrid_forward(self, F, x):
+        return F.clip(x, 0, 6, name="relu6")
+
+
 # pylint: disable= too-many-arguments
-def _add_conv(out, channels=1, kernel=1, stride=1, pad=0, num_group=1, 
active=True):
+def _add_conv(out, channels=1, kernel=1, stride=1, pad=0,
+              num_group=1, active=True, relu6=False):
     out.add(nn.Conv2D(channels, kernel, stride, pad, groups=num_group, 
use_bias=False))
     out.add(nn.BatchNorm(scale=True))
     if active:
-        out.add(nn.Activation('relu'))
+        out.add(RELU6() if relu6 else nn.Activation('relu'))
 
 
-def _add_conv_dw(out, dw_channels, channels, stride):
-    _add_conv(out, channels=dw_channels, kernel=3, stride=stride, pad=1, 
num_group=dw_channels)
-    _add_conv(out, channels=channels)
+def _add_conv_dw(out, dw_channels, channels, stride, relu6=False):
+    _add_conv(out, channels=dw_channels, kernel=3, stride=stride,
+              pad=1, num_group=dw_channels, relu6=relu6)
+    _add_conv(out, channels=channels, relu6=relu6)
 
 
 class LinearBottleneck(nn.HybridBlock):
@@ -70,10 +82,10 @@ def __init__(self, in_channels, channels, t, stride, 
**kwargs):
         with self.name_scope():
             self.out = nn.HybridSequential()
 
-            _add_conv(self.out, in_channels * t)
+            _add_conv(self.out, in_channels * t, relu6=True)
             _add_conv(self.out, in_channels * t, kernel=3, stride=stride,
-                      pad=1, num_group=in_channels * t)
-            _add_conv(self.out, channels, active=False)
+                      pad=1, num_group=in_channels * t, relu6=True)
+            _add_conv(self.out, channels, active=False, relu6=True)
 
     def hybrid_forward(self, F, x):
         out = self.out(x)
@@ -143,21 +155,21 @@ def __init__(self, multiplier=1.0, classes=1000, 
**kwargs):
             self.features = nn.HybridSequential(prefix='features_')
             with self.features.name_scope():
                 _add_conv(self.features, int(32 * multiplier), kernel=3,
-                          stride=2, pad=1)
+                          stride=2, pad=1, relu6=True)
 
                 in_channels_group = [int(x * multiplier) for x in [32] + [16] 
+ [24] * 2
                                      + [32] * 3 + [64] * 4 + [96] * 3 + [160] 
* 3]
                 channels_group = [int(x * multiplier) for x in [16] + [24] * 2 
+ [32] * 3
                                   + [64] * 4 + [96] * 3 + [160] * 3 + [320]]
                 ts = [1] + [6] * 16
-                strides = [1, 2] * 2 + [1] * 6 + [2, 1, 1] * 2 + [1]
+                strides = [1, 2] * 2 + [1, 1, 2] + [1] * 6 + [2] + [1] * 3
 
                 for in_c, c, t, s in zip(in_channels_group, channels_group, 
ts, strides):
                     self.features.add(LinearBottleneck(in_channels=in_c, 
channels=c,
                                                        t=t, stride=s))
 
                 last_channels = int(1280 * multiplier) if multiplier > 1.0 
else 1280
-                _add_conv(self.features, last_channels)
+                _add_conv(self.features, last_channels, relu6=True)
 
                 self.features.add(nn.GlobalAvgPool2D())
 


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to