mobilenet_v2.py 7.3 KB
Newer Older
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
3 4 5
# Licensed 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
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
9 10 11 12 13
# 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.
W
WuHaobo 已提交
14 15 16 17

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
18 19 20

import numpy as np
import paddle
W
WuHaobo 已提交
21 22
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
23 24 25 26
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout

import math
W
WuHaobo 已提交
27 28

__all__ = [
29 30
    "MobileNetV2_x0_25", "MobileNetV2_x0_5", "MobileNetV2_x0_75",
    "MobileNetV2", "MobileNetV2_x1_5", "MobileNetV2_x2_0"
W
WuHaobo 已提交
31 32 33
]


34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class ConvBNLayer(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 filter_size,
                 num_filters,
                 stride,
                 padding,
                 channels=None,
                 num_groups=1,
                 name=None,
                 use_cudnn=True):
        super(ConvBNLayer, self).__init__()

        self._conv = Conv2D(
            num_channels=num_channels,
W
WuHaobo 已提交
49 50 51 52 53 54 55
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=num_groups,
            act=None,
            use_cudnn=use_cudnn,
56
            param_attr=ParamAttr(name=name + "_weights"),
W
WuHaobo 已提交
57
            bias_attr=False)
58 59 60 61 62 63 64 65 66 67 68

        self._batch_norm = BatchNorm(
            num_filters,
            param_attr=ParamAttr(name=name + "_bn_scale"),
            bias_attr=ParamAttr(name=name + "_bn_offset"),
            moving_mean_name=name + "_bn_mean",
            moving_variance_name=name + "_bn_variance")

    def forward(self, inputs, if_act=True):
        y = self._conv(inputs)
        y = self._batch_norm(y)
W
WuHaobo 已提交
69
        if if_act:
70 71
            y = fluid.layers.relu6(y)
        return y
W
WuHaobo 已提交
72

73 74 75 76 77 78 79 80

class InvertedResidualUnit(fluid.dygraph.Layer):
    def __init__(self, num_channels, num_in_filter, num_filters, stride,
                 filter_size, padding, expansion_factor, name):
        super(InvertedResidualUnit, self).__init__()
        num_expfilter = int(round(num_in_filter * expansion_factor))
        self._expand_conv = ConvBNLayer(
            num_channels=num_channels,
W
WuHaobo 已提交
81 82 83 84 85
            num_filters=num_expfilter,
            filter_size=1,
            stride=1,
            padding=0,
            num_groups=1,
86
            name=name + "_expand")
W
WuHaobo 已提交
87

88 89
        self._bottleneck_conv = ConvBNLayer(
            num_channels=num_expfilter,
W
WuHaobo 已提交
90 91 92 93 94
            num_filters=num_expfilter,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            num_groups=num_expfilter,
95 96
            use_cudnn=False,
            name=name + "_dwise")
W
WuHaobo 已提交
97

98 99
        self._linear_conv = ConvBNLayer(
            num_channels=num_expfilter,
W
WuHaobo 已提交
100 101 102 103 104
            num_filters=num_filters,
            filter_size=1,
            stride=1,
            padding=0,
            num_groups=1,
105 106 107 108 109 110
            name=name + "_linear")

    def forward(self, inputs, ifshortcut):
        y = self._expand_conv(inputs, if_act=True)
        y = self._bottleneck_conv(y, if_act=True)
        y = self._linear_conv(y, if_act=False)
W
WuHaobo 已提交
111
        if ifshortcut:
112 113 114 115 116 117 118 119 120 121
            y = fluid.layers.elementwise_add(inputs, y)
        return y


class InvresiBlocks(fluid.dygraph.Layer):
    def __init__(self, in_c, t, c, n, s, name):
        super(InvresiBlocks, self).__init__()

        self._first_block = InvertedResidualUnit(
            num_channels=in_c,
W
WuHaobo 已提交
122 123 124 125 126 127
            num_in_filter=in_c,
            num_filters=c,
            stride=s,
            filter_size=3,
            padding=1,
            expansion_factor=t,
128
            name=name + "_1")
W
WuHaobo 已提交
129

130
        self._block_list = []
W
WuHaobo 已提交
131
        for i in range(1, n):
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            block = self.add_sublayer(
                name + "_" + str(i + 1),
                sublayer=InvertedResidualUnit(
                    num_channels=c,
                    num_in_filter=c,
                    num_filters=c,
                    stride=1,
                    filter_size=3,
                    padding=1,
                    expansion_factor=t,
                    name=name + "_" + str(i + 1)))
            self._block_list.append(block)

    def forward(self, inputs):
        y = self._first_block(inputs, ifshortcut=False)
        for block in self._block_list:
            y = block(y, ifshortcut=True)
        return y


class MobileNet(fluid.dygraph.Layer):
    def __init__(self, class_dim=1000, scale=1.0):
        super(MobileNet, self).__init__()
        self.scale = scale
        self.class_dim = class_dim

        bottleneck_params_list = [
            (1, 16, 1, 1),
            (6, 24, 2, 2),
            (6, 32, 3, 2),
            (6, 64, 4, 2),
            (6, 96, 3, 1),
            (6, 160, 3, 2),
            (6, 320, 1, 1),
        ]

        self.conv1 = ConvBNLayer(
            num_channels=3,
            num_filters=int(32 * scale),
            filter_size=3,
            stride=2,
            padding=1,
            name="conv1_1")

        self.block_list = []
        i = 1
        in_c = int(32 * scale)
        for layer_setting in bottleneck_params_list:
            t, c, n, s = layer_setting
            i += 1
            block = self.add_sublayer(
                "conv" + str(i),
                sublayer=InvresiBlocks(
                    in_c=in_c,
                    t=t,
                    c=int(c * scale),
                    n=n,
                    s=s,
                    name="conv" + str(i)))
            self.block_list.append(block)
            in_c = int(c * scale)

        self.out_c = int(1280 * scale) if scale > 1.0 else 1280
        self.conv9 = ConvBNLayer(
            num_channels=in_c,
            num_filters=self.out_c,
            filter_size=1,
            stride=1,
            padding=0,
            name="conv9")

        self.pool2d_avg = Pool2D(pool_type="avg", global_pooling=True)

        self.out = Linear(
            self.out_c,
            class_dim,
            param_attr=ParamAttr(name="fc10_weights"),
            bias_attr=ParamAttr(name="fc10_offset"))

    def forward(self, inputs):
        y = self.conv1(inputs, if_act=True)
        for block in self.block_list:
            y = block(y)
        y = self.conv9(y, if_act=True)
        y = self.pool2d_avg(y)
        y = fluid.layers.reshape(y, shape=[-1, self.out_c])
        y = self.out(y)
        return y


def MobileNetV2_x0_25(**args):
    model = MobileNet(scale=0.25, **args)
W
WuHaobo 已提交
224 225 226
    return model


227 228
def MobileNetV2_x0_5(**args):
    model = MobileNet(scale=0.5, **args)
W
WuHaobo 已提交
229 230 231
    return model


232 233
def MobileNetV2_x0_75(**args):
    model = MobileNet(scale=0.75, **args)
W
WuHaobo 已提交
234 235 236
    return model


237 238
def MobileNetV2(**args):
    model = MobileNet(scale=1.0, **args)
W
WuHaobo 已提交
239 240 241
    return model


242 243
def MobileNetV2_x1_5(**args):
    model = MobileNet(scale=1.5, **args)
W
WuHaobo 已提交
244 245 246
    return model


247 248
def MobileNetV2_x2_0(**args):
    model = MobileNet(scale=2.0, **args)
W
WuHaobo 已提交
249
    return model