mobilenetv1.py 8.2 KB
Newer Older
L
LielinJiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     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.

L
LielinJiang 已提交
15 16
import paddle
import paddle.nn as nn
L
LielinJiang 已提交
17

18
from paddle.utils.download import get_weights_path_from_url
L
LielinJiang 已提交
19 20 21 22 23 24

__all__ = ['MobileNetV1', 'mobilenet_v1']

model_urls = {
    'mobilenetv1_1.0':
    ('https://paddle-hapi.bj.bcebos.com/models/mobilenet_v1_x1.0.pdparams',
L
LielinJiang 已提交
25
     '42a154c2f26f86e7457d6daded114e8c')
L
LielinJiang 已提交
26 27 28
}


L
LielinJiang 已提交
29
class ConvBNLayer(nn.Layer):
L
LielinJiang 已提交
30
    def __init__(self,
L
LielinJiang 已提交
31 32 33
                 in_channels,
                 out_channels,
                 kernel_size,
L
LielinJiang 已提交
34 35
                 stride,
                 padding,
L
LielinJiang 已提交
36
                 num_groups=1):
L
LielinJiang 已提交
37 38
        super(ConvBNLayer, self).__init__()

L
LielinJiang 已提交
39 40 41 42
        self._conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
L
LielinJiang 已提交
43 44 45 46 47
            stride=stride,
            padding=padding,
            groups=num_groups,
            bias_attr=False)

L
LielinJiang 已提交
48 49
        self._norm_layer = nn.BatchNorm2d(out_channels)
        self._act = nn.ReLU()
L
LielinJiang 已提交
50

L
LielinJiang 已提交
51 52 53 54 55
    def forward(self, x):
        x = self._conv(x)
        x = self._norm_layer(x)
        x = self._act(x)
        return x
L
LielinJiang 已提交
56 57


L
LielinJiang 已提交
58 59 60
class DepthwiseSeparable(nn.Layer):
    def __init__(self, in_channels, out_channels1, out_channels2, num_groups,
                 stride, scale):
L
LielinJiang 已提交
61 62 63
        super(DepthwiseSeparable, self).__init__()

        self._depthwise_conv = ConvBNLayer(
L
LielinJiang 已提交
64 65 66
            in_channels,
            int(out_channels1 * scale),
            kernel_size=3,
L
LielinJiang 已提交
67 68
            stride=stride,
            padding=1,
L
LielinJiang 已提交
69
            num_groups=int(num_groups * scale))
L
LielinJiang 已提交
70 71

        self._pointwise_conv = ConvBNLayer(
L
LielinJiang 已提交
72 73 74
            int(out_channels1 * scale),
            int(out_channels2 * scale),
            kernel_size=1,
L
LielinJiang 已提交
75 76 77
            stride=1,
            padding=0)

L
LielinJiang 已提交
78 79 80 81
    def forward(self, x):
        x = self._depthwise_conv(x)
        x = self._pointwise_conv(x)
        return x
L
LielinJiang 已提交
82 83


L
LielinJiang 已提交
84
class MobileNetV1(nn.Layer):
L
LielinJiang 已提交
85 86 87 88 89 90 91 92 93 94 95 96
    """MobileNetV1 model from
    `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_.

    Args:
        scale (float): scale of channels in each layer. Default: 1.0.
        num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer 
                            will not be defined. Default: 1000.
        with_pool (bool): use pool before the last fc layer or not. Default: True.

    Examples:
        .. code-block:: python

97
            from paddle.vision.models import MobileNetV1
L
LielinJiang 已提交
98 99 100 101

            model = MobileNetV1()
    """

L
LielinJiang 已提交
102
    def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
L
LielinJiang 已提交
103 104 105 106 107 108 109
        super(MobileNetV1, self).__init__()
        self.scale = scale
        self.dwsl = []
        self.num_classes = num_classes
        self.with_pool = with_pool

        self.conv1 = ConvBNLayer(
L
LielinJiang 已提交
110 111 112
            in_channels=3,
            out_channels=int(32 * scale),
            kernel_size=3,
L
LielinJiang 已提交
113 114 115 116 117
            stride=2,
            padding=1)

        dws21 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
118 119 120
                in_channels=int(32 * scale),
                out_channels1=32,
                out_channels2=64,
L
LielinJiang 已提交
121 122 123 124 125 126 127 128
                num_groups=32,
                stride=1,
                scale=scale),
            name="conv2_1")
        self.dwsl.append(dws21)

        dws22 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
129 130 131
                in_channels=int(64 * scale),
                out_channels1=64,
                out_channels2=128,
L
LielinJiang 已提交
132 133 134 135 136 137 138 139
                num_groups=64,
                stride=2,
                scale=scale),
            name="conv2_2")
        self.dwsl.append(dws22)

        dws31 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
140 141 142
                in_channels=int(128 * scale),
                out_channels1=128,
                out_channels2=128,
L
LielinJiang 已提交
143 144 145 146 147 148 149 150
                num_groups=128,
                stride=1,
                scale=scale),
            name="conv3_1")
        self.dwsl.append(dws31)

        dws32 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
151 152 153
                in_channels=int(128 * scale),
                out_channels1=128,
                out_channels2=256,
L
LielinJiang 已提交
154 155 156 157 158 159 160 161
                num_groups=128,
                stride=2,
                scale=scale),
            name="conv3_2")
        self.dwsl.append(dws32)

        dws41 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
162 163 164
                in_channels=int(256 * scale),
                out_channels1=256,
                out_channels2=256,
L
LielinJiang 已提交
165 166 167 168 169 170 171 172
                num_groups=256,
                stride=1,
                scale=scale),
            name="conv4_1")
        self.dwsl.append(dws41)

        dws42 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
173 174 175
                in_channels=int(256 * scale),
                out_channels1=256,
                out_channels2=512,
L
LielinJiang 已提交
176 177 178 179 180 181 182 183 184
                num_groups=256,
                stride=2,
                scale=scale),
            name="conv4_2")
        self.dwsl.append(dws42)

        for i in range(5):
            tmp = self.add_sublayer(
                sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
185 186 187
                    in_channels=int(512 * scale),
                    out_channels1=512,
                    out_channels2=512,
L
LielinJiang 已提交
188 189 190 191 192 193 194 195
                    num_groups=512,
                    stride=1,
                    scale=scale),
                name="conv5_" + str(i + 1))
            self.dwsl.append(tmp)

        dws56 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
196 197 198
                in_channels=int(512 * scale),
                out_channels1=512,
                out_channels2=1024,
L
LielinJiang 已提交
199 200 201 202 203 204 205 206
                num_groups=512,
                stride=2,
                scale=scale),
            name="conv5_6")
        self.dwsl.append(dws56)

        dws6 = self.add_sublayer(
            sublayer=DepthwiseSeparable(
L
LielinJiang 已提交
207 208 209
                in_channels=int(1024 * scale),
                out_channels1=1024,
                out_channels2=1024,
L
LielinJiang 已提交
210 211 212 213 214 215 216
                num_groups=1024,
                stride=1,
                scale=scale),
            name="conv6")
        self.dwsl.append(dws6)

        if with_pool:
L
LielinJiang 已提交
217 218 219 220 221 222 223
            self.pool2d_avg = nn.AdaptiveAvgPool2d(1)

        if num_classes > 0:
            self.fc = nn.Linear(int(1024 * scale), num_classes)

    def forward(self, x):
        x = self.conv1(x)
L
LielinJiang 已提交
224
        for dws in self.dwsl:
L
LielinJiang 已提交
225
            x = dws(x)
L
LielinJiang 已提交
226 227

        if self.with_pool:
L
LielinJiang 已提交
228
            x = self.pool2d_avg(x)
L
LielinJiang 已提交
229 230

        if self.num_classes > 0:
L
LielinJiang 已提交
231 232 233
            x = paddle.flatten(x, 1)
            x = self.fc(x)
        return x
L
LielinJiang 已提交
234 235 236 237 238 239 240 241 242 243 244


def _mobilenet(arch, pretrained=False, **kwargs):
    model = MobileNetV1(**kwargs)
    if pretrained:
        assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format(
            arch)
        weight_path = get_weights_path_from_url(model_urls[arch][0],
                                                model_urls[arch][1])
        assert weight_path.endswith(
            '.pdparams'), "suffix of weight must be .pdparams"
L
LielinJiang 已提交
245
        param, _ = paddle.load(weight_path)
246
        model.load_dict(param)
L
LielinJiang 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260

    return model


def mobilenet_v1(pretrained=False, scale=1.0, **kwargs):
    """MobileNetV1
    
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
        scale: (float): scale of channels in each layer. Default: 1.0.

    Examples:
        .. code-block:: python

261
            from paddle.vision.models import mobilenet_v1
L
LielinJiang 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274

            # build model
            model = mobilenet_v1()

            # build model and load imagenet pretrained weight
            # model = mobilenet_v1(pretrained=True)

            # build mobilenet v1 with scale=0.5
            model = mobilenet_v1(scale=0.5)
    """
    model = _mobilenet(
        'mobilenetv1_' + str(scale), pretrained, scale=scale, **kwargs)
    return model