mikerossgithub commented on a change in pull request #7957: add densenet
URL: https://github.com/apache/incubator-mxnet/pull/7957#discussion_r141428733
 
 

 ##########
 File path: example/image-classification/symbols/densenet.py
 ##########
 @@ -0,0 +1,182 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Adapted from 
https://github.com/bruinxiong/densenet.mxnet/blob/master/symbol_densenet.py
+Implemented the following paper:
+Gao Huang, Zhang Liu, Laurend van der Maaten. "Densely Connected Convolutional 
Networks"
+"""
+import mxnet as mx
+import math
+
+def BasicBlock(data, growth_rate, stride, name, bottle_neck=True, 
drop_out=0.0, bn_mom=0.9, workspace=512):
+    """Return BaiscBlock Unit symbol for building DenseBlock
+    Parameters
+    ----------
+    data : str
+        Input data
+    growth_rate : int
+        Number of output channels
+    stride : tupe
+        Stride used in convolution
+    drop_out : float
+        Probability of an element to be zeroed. Default = 0.2
+    name : str
+        Base name of the operators
+    workspace : int
+        Workspace used in convolution operator
+    """
+
+    if bottle_neck:
+        # the same as https://github.com/facebook/fb.resnet.torch#notes, a bit 
difference with origin paper
+        bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, 
momentum=bn_mom, name=name + '_bn1')
+        act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + 
'_relu1')
+        conv1 = mx.sym.Convolution(data=act1, num_filter=int(growth_rate * 4), 
kernel=(1, 1), stride=(1, 1), pad=(0, 0),
+                                   no_bias=True, workspace=workspace, 
name=name + '_conv1')
+        if drop_out > 0:
+            conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + 
'_dp1')
+        bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, 
momentum=bn_mom, name=name + '_bn2')
+        act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + 
'_relu2')
+        conv2 = mx.sym.Convolution(data=act2, num_filter=int(growth_rate), 
kernel=(3, 3), stride=stride, pad=(1, 1),
+                                   no_bias=True, workspace=workspace, 
name=name + '_conv2')
+        if drop_out > 0:
+            conv2 = mx.symbol.Dropout(data=conv2, p=drop_out, name=name + 
'_dp2')
+        # return mx.symbol.Concat(data, conv2, name=name + '_concat0')
+        return conv2
+    else:
+        bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, 
momentum=bn_mom, name=name + '_bn1')
+        act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + 
'_relu1')
+        conv1 = mx.sym.Convolution(data=act1, num_filter=int(growth_rate), 
kernel=(3, 3), stride=(1, 1), pad=(1, 1),
+                                   no_bias=True, workspace=workspace, 
name=name + '_conv1')
+        if drop_out > 0:
+            conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + 
'_dp1')
+        # return mx.symbol.Concat(data, conv1, name=name + '_concat0')
+        return conv1
+
+
+def DenseBlock(units_num, data, growth_rate, name, bottle_neck=True, 
drop_out=0.0, bn_mom=0.9, workspace=512):
+    """Return DenseBlock Unit symbol for building DenseNet
+    Parameters
+    ----------
+    units_num : int
+        the number of BasicBlock in each DenseBlock
+    data : str
+        Input data
+    growth_rate : int
+        Number of output channels
+    drop_out : float
+        Probability of an element to be zeroed. Default = 0.2
+    workspace : int
+        Workspace used in convolution operator
+    """
+
+    for i in range(units_num):
+        Block = BasicBlock(data, growth_rate=growth_rate, stride=(1, 1), 
name=name + '_unit%d' % (i + 1),
+                           bottle_neck=bottle_neck, drop_out=drop_out,
+                           bn_mom=bn_mom, workspace=workspace)
+        data = mx.symbol.Concat(data, Block, name=name + '_concat%d' % (i + 1))
+    return data
+
+def TransitionBlock(num_stage, data, num_filter, stride, name, drop_out=0.0, 
bn_mom=0.9, workspace=512):
+    """Return TransitionBlock Unit symbol for building DenseNet
+    Parameters
+    ----------
+    num_stage : int
+        Number of stage
+    data : str
+        Input data
+    num : int
+        Number of output channels
+    stride : tupe
+        Stride used in convolution
+    name : str
+        Base name of the operators
+    drop_out : float
+        Probability of an element to be zeroed. Default = 0.2
+    workspace : int
+        Workspace used in convolution operator
+    """
+    bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, 
momentum=bn_mom, name=name + '_bn1')
+    act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
+    conv1 = mx.sym.Convolution(data=act1, num_filter=num_filter,
+                               kernel=(1, 1), stride=stride, pad=(0, 0), 
no_bias=True,
+                               workspace=workspace, name=name + '_conv1')
+    if drop_out > 0:
+        conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + '_dp1')
+    return mx.symbol.Pooling(conv1, global_pool=False, kernel=(2, 2), 
stride=(2, 2), pool_type='avg',
+                             name=name + '_pool%d' % (num_stage + 1))
+
+def get_symbol(num_classes, num_layers=121, reduction=0.5, drop_out=0.2, 
bottle_neck=True, bn_mom=0.9, 
+                            workspace=512, num_stage=4, growth_rate=32,  
**kwargs):
+    """
+    Adapted from 
https://github.com/bruinxiong/densenet.mxnet/blob/master/symbol_densenet.py
+    Return DenseNet symbol of imagenet
+    Parameters
+    ----------
+    num_class : int
+        Ouput size of symbol
+    num_layers : int
+        Number of layers of the whole net
+    reduction : float
+        Compression ratio. Default = 0.5
+    drop_out : float
+        Probability of an element to be zeroed. Default = 0.2
+    workspace : int
+        Workspace used in convolution operator
+    num_stage : int
+        Number of stage
+    growth_rate : int
+        Number of output channels
+    """
+
+    if num_layers == 121:
+        units = [6, 12, 24, 16]
+    elif num_layers == 169:
+        units = [6, 12, 32, 32]
+    elif num_layers == 201:
+        units = [6, 12, 48, 16]
 
 Review comment:
   To be consistent with https://arxiv.org/pdf/1608.06993.pdf you would want:
   ```
   elif num_layers == 201:
     units = [6, 12, 48, 32]
   ```
   Using [6, 12, 48, 16] would just be 169 layers.
 
----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to