mobilenet.py 6.92 KB
Newer Older
eqy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.
"""
Port of NNVM version of MobileNet to Relay.
"""
# pylint: disable=invalid-name

from tvm import relay
from . import layers
from .init import create_workload

26

eqy committed
27
def conv_block(data, name, channels, kernel_size=(3, 3), strides=(1, 1),
28
               padding=(1, 1), epsilon=1e-5, layout='NCHW'):
eqy committed
29 30 31 32 33 34 35 36
    """Helper function to construct conv_bn-relu"""
    # convolution + bn + relu
    conv = layers.conv2d(
        data=data,
        channels=channels,
        kernel_size=kernel_size,
        strides=strides,
        padding=padding,
37 38
        data_layout=layout,
        kernel_layout=layers.conv_kernel_layout(layout),
eqy committed
39 40 41 42 43 44 45 46
        name=name+'_conv')
    bn = layers.batch_norm_infer(data=conv, epsilon=epsilon, name=name + '_bn')
    act = relay.nn.relu(data=bn)
    return act


def separable_conv_block(data, name, depthwise_channels, pointwise_channels,
                         kernel_size=(3, 3), downsample=False, padding=(1, 1),
47
                         epsilon=1e-5, layout='NCHW', dtype="float32"):
eqy committed
48 49 50 51 52 53
    """Helper function to get a separable conv block"""
    if downsample:
        strides = (2, 2)
    else:
        strides = (1, 1)
    # depthwise convolution + bn + relu
54 55
    wshape = (depthwise_channels, 1) + kernel_size
    weight = relay.var(name + "_weight", shape=wshape, dtype=dtype)
eqy committed
56 57
    conv1 = layers.conv2d(
        data=data,
58
        weight=weight,
eqy committed
59 60 61 62 63
        channels=depthwise_channels,
        groups=depthwise_channels,
        kernel_size=kernel_size,
        strides=strides,
        padding=padding,
64 65
        data_layout=layout,
        kernel_layout=layers.conv_kernel_layout(layout, True),
eqy committed
66 67 68 69 70 71 72 73 74 75
        name=name+'_depthwise_conv1')
    bn1 = layers.batch_norm_infer(data=conv1, epsilon=epsilon, name=name+'_bn1')
    act1 = relay.nn.relu(data=bn1)
    # pointwise convolution + bn + relu
    conv2 = layers.conv2d(
        data=act1,
        channels=pointwise_channels,
        kernel_size=(1, 1),
        strides=(1, 1),
        padding=(0, 0),
76 77
        data_layout=layout,
        kernel_layout=layers.conv_kernel_layout(layout),
eqy committed
78 79 80 81 82 83 84
        name=name + '_conv2')
    bn2 = layers.batch_norm_infer(data=conv2, epsilon=epsilon, name=name+'_bn2')
    act2 = relay.nn.relu(data=bn2)
    return act2


def mobile_net(num_classes=1000, data_shape=(1, 3, 224, 224),
85
               dtype='float32', alpha=1.0, is_shallow=False, layout='NCHW'):
eqy committed
86 87
    """Function to construct a MobileNet"""
    data = relay.var("data", shape=data_shape, dtype=dtype)
88 89
    body = conv_block(data, 'conv_block_1', int(32*alpha), strides=(2, 2),
                      layout=layout)
eqy committed
90
    body = separable_conv_block(body, 'separable_conv_block_1',
91 92
                                int(32*alpha), int(64*alpha), layout=layout,
                                dtype=dtype)
eqy committed
93
    body = separable_conv_block(body, 'separable_conv_block_2',
94
                                int(64*alpha), int(128*alpha), downsample=True,
95
                                layout=layout, dtype=dtype)
eqy committed
96
    body = separable_conv_block(body, 'separable_conv_block_3',
97 98
                                int(128*alpha), int(128*alpha), layout=layout,
                                dtype=dtype)
eqy committed
99
    body = separable_conv_block(body, 'separable_conv_block_4',
100
                                int(128*alpha), int(256*alpha), downsample=True,
101
                                layout=layout, dtype=dtype)
eqy committed
102
    body = separable_conv_block(body, 'separable_conv_block_5',
103 104
                                int(256*alpha), int(256*alpha), layout=layout,
                                dtype=dtype)
eqy committed
105
    body = separable_conv_block(body, 'separable_conv_block_6',
106
                                int(256*alpha), int(512*alpha), downsample=True,
107
                                layout=layout, dtype=dtype)
eqy committed
108 109
    if is_shallow:
        body = separable_conv_block(body, 'separable_conv_block_7',
110
                                    int(512*alpha), int(1024*alpha),
111
                                    downsample=True, layout=layout, dtype=dtype)
eqy committed
112
        body = separable_conv_block(body, 'separable_conv_block_8',
113
                                    int(1024*alpha), int(1024*alpha),
114
                                    downsample=True, layout=layout, dtype=dtype)
eqy committed
115 116 117
    else:
        for i in range(7, 12):
            body = separable_conv_block(body, 'separable_conv_block_%d' % i,
118
                                        int(512*alpha), int(512*alpha),
119
                                        layout=layout, dtype=dtype)
eqy committed
120
        body = separable_conv_block(body, 'separable_conv_block_12',
121
                                    int(512*alpha), int(1024*alpha),
122
                                    downsample=True, layout=layout, dtype=dtype)
eqy committed
123
        body = separable_conv_block(body, 'separable_conv_block_13',
124
                                    int(1024*alpha), int(1024*alpha),
125
                                    layout=layout, dtype=dtype)
126
    pool = relay.nn.global_avg_pool2d(data=body, layout=layout)
eqy committed
127 128
    flatten = relay.nn.batch_flatten(data=pool)
    weight = relay.var('fc_weight')
129
    bias = relay.var('fc_bias')
eqy committed
130
    fc = relay.nn.dense(data=flatten, weight=weight, units=num_classes)
131
    fc = relay.nn.bias_add(fc, bias)
eqy committed
132
    softmax = relay.nn.softmax(data=fc)
Zhi committed
133
    return relay.Function(relay.analysis.free_vars(softmax), softmax)
eqy committed
134 135


136 137
def get_workload(batch_size=1, num_classes=1000, image_shape=(3, 224, 224),
                 dtype='float32', layout='NCHW'):
eqy committed
138 139 140 141 142 143 144 145 146 147 148
    """Get benchmark workload for mobilenet

    Parameters
    ----------
    batch_size : int, optional
        The batch size used in the model

    num_classes : int, optional
        Number of classes

    image_shape : tuple, optional
149
        The input image shape, cooperate with layout
eqy committed
150 151 152 153

    dtype : str, optional
        The data type

154 155 156 157
    layout : str, optional
        The data layout of image_shape and the operators
        cooperate with image_shape

eqy committed
158 159
    Returns
    -------
160
    mod : tvm.IRModule
161
        The relay module that contains a MobileNet network.
eqy committed
162 163 164 165 166 167

    params : dict of str to NDArray
        The parameters.
    """
    data_shape = tuple([batch_size] + list(image_shape))
    net = mobile_net(num_classes=num_classes, data_shape=data_shape,
168 169
                     dtype=dtype, alpha=1.0, is_shallow=False,
                     layout=layout)
eqy committed
170
    return create_workload(net)