pp_lcnet.py 17.0 KB
Newer Older
C
cuicheng01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# 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.

from __future__ import absolute_import, division, print_function

import paddle
import paddle.nn as nn
from paddle import ParamAttr
C
cuicheng01 已提交
20
from paddle.nn import AdaptiveAvgPool2D, BatchNorm2D, Conv2D, Dropout, Linear
C
cuicheng01 已提交
21 22 23 24 25 26
from paddle.regularizer import L2Decay
from paddle.nn.initializer import KaimingNormal
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url

MODEL_URLS = {
C
cuicheng01 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    "PPLCNet_x0_25":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x0_25_pretrained.pdparams",
    "PPLCNet_x0_35":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x0_35_pretrained.pdparams",
    "PPLCNet_x0_5":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x0_5_pretrained.pdparams",
    "PPLCNet_x0_75":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x0_75_pretrained.pdparams",
    "PPLCNet_x1_0":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x1_0_pretrained.pdparams",
    "PPLCNet_x1_5":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x1_5_pretrained.pdparams",
    "PPLCNet_x2_0":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x2_0_pretrained.pdparams",
    "PPLCNet_x2_5":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x2_5_pretrained.pdparams"
C
cuicheng01 已提交
43 44
}

45 46 47 48
MODEL_STAGES_PATTERN = {
    "PPLCNet": ["blocks2", "blocks3", "blocks4", "blocks5", "blocks6"]
}

C
cuicheng01 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
__all__ = list(MODEL_URLS.keys())

# Each element(list) represents a depthwise block, which is composed of k, in_c, out_c, s, use_se.
# k: kernel_size
# in_c: input channel number in depthwise block
# out_c: output channel number in depthwise block
# s: stride in depthwise block
# use_se: whether to use SE block

NET_CONFIG = {
    "blocks2":
    #k, in_c, out_c, s, use_se
    [[3, 16, 32, 1, False]],
    "blocks3": [[3, 32, 64, 2, False], [3, 64, 64, 1, False]],
    "blocks4": [[3, 64, 128, 2, False], [3, 128, 128, 1, False]],
    "blocks5": [[3, 128, 256, 2, False], [5, 256, 256, 1, False],
                [5, 256, 256, 1, False], [5, 256, 256, 1, False],
                [5, 256, 256, 1, False], [5, 256, 256, 1, False]],
    "blocks6": [[5, 256, 512, 2, True], [5, 512, 512, 1, True]]
}


def make_divisible(v, divisor=8, min_value=None):
    if min_value is None:
        min_value = divisor
    new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
    if new_v < 0.9 * v:
        new_v += divisor
    return new_v


class ConvBNLayer(TheseusLayer):
    def __init__(self,
                 num_channels,
                 filter_size,
                 num_filters,
                 stride,
C
cuicheng01 已提交
86 87
                 num_groups=1,
                 lr_mult=1.0):
C
cuicheng01 已提交
88 89 90 91 92 93 94 95 96
        super().__init__()

        self.conv = Conv2D(
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=num_groups,
C
cuicheng01 已提交
97 98
            weight_attr=ParamAttr(
                initializer=KaimingNormal(), learning_rate=lr_mult),
C
cuicheng01 已提交
99 100
            bias_attr=False)

C
cuicheng01 已提交
101
        self.bn = BatchNorm2D(
C
cuicheng01 已提交
102
            num_filters,
C
cuicheng01 已提交
103 104 105 106
            weight_attr=ParamAttr(
                regularizer=L2Decay(0.0), learning_rate=lr_mult),
            bias_attr=ParamAttr(
                regularizer=L2Decay(0.0), learning_rate=lr_mult))
C
cuicheng01 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        self.hardswish = nn.Hardswish()

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.hardswish(x)
        return x


class DepthwiseSeparable(TheseusLayer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 stride,
                 dw_size=3,
C
cuicheng01 已提交
122 123
                 use_se=False,
                 lr_mult=1.0):
C
cuicheng01 已提交
124 125 126 127 128 129 130
        super().__init__()
        self.use_se = use_se
        self.dw_conv = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_channels,
            filter_size=dw_size,
            stride=stride,
C
cuicheng01 已提交
131 132
            num_groups=num_channels,
            lr_mult=lr_mult)
C
cuicheng01 已提交
133
        if use_se:
C
cuicheng01 已提交
134
            self.se = SEModule(num_channels, lr_mult=lr_mult)
C
cuicheng01 已提交
135 136 137 138
        self.pw_conv = ConvBNLayer(
            num_channels=num_channels,
            filter_size=1,
            num_filters=num_filters,
C
cuicheng01 已提交
139 140
            stride=1,
            lr_mult=lr_mult)
C
cuicheng01 已提交
141 142 143 144 145 146 147 148 149 150

    def forward(self, x):
        x = self.dw_conv(x)
        if self.use_se:
            x = self.se(x)
        x = self.pw_conv(x)
        return x


class SEModule(TheseusLayer):
C
cuicheng01 已提交
151
    def __init__(self, channel, reduction=4, lr_mult=1.0):
C
cuicheng01 已提交
152 153 154 155 156 157 158
        super().__init__()
        self.avg_pool = AdaptiveAvgPool2D(1)
        self.conv1 = Conv2D(
            in_channels=channel,
            out_channels=channel // reduction,
            kernel_size=1,
            stride=1,
C
cuicheng01 已提交
159 160 161
            padding=0,
            weight_attr=ParamAttr(learning_rate=lr_mult),
            bias_attr=ParamAttr(learning_rate=lr_mult))
C
cuicheng01 已提交
162 163 164 165 166 167
        self.relu = nn.ReLU()
        self.conv2 = Conv2D(
            in_channels=channel // reduction,
            out_channels=channel,
            kernel_size=1,
            stride=1,
C
cuicheng01 已提交
168 169 170
            padding=0,
            weight_attr=ParamAttr(learning_rate=lr_mult),
            bias_attr=ParamAttr(learning_rate=lr_mult))
C
cuicheng01 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
        self.hardsigmoid = nn.Hardsigmoid()

    def forward(self, x):
        identity = x
        x = self.avg_pool(x)
        x = self.conv1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x = self.hardsigmoid(x)
        x = paddle.multiply(x=identity, y=x)
        return x


C
cuicheng01 已提交
184
class PPLCNet(TheseusLayer):
C
cuicheng01 已提交
185
    def __init__(self,
186
                 stages_pattern,
C
cuicheng01 已提交
187 188 189
                 scale=1.0,
                 class_num=1000,
                 dropout_prob=0.2,
190
                 class_expand=1280,
C
cuicheng01 已提交
191 192 193
                 lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                 stride_list=[2, 2, 2, 2, 2],
                 use_last_conv=True,
194
                 return_patterns=None,
195 196
                 return_stages=None,
                 **kwargs):
C
cuicheng01 已提交
197 198 199
        super().__init__()
        self.scale = scale
        self.class_expand = class_expand
C
cuicheng01 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        self.lr_mult_list = lr_mult_list
        self.use_last_conv = use_last_conv
        self.stride_list = stride_list
        self.net_config = NET_CONFIG
        if isinstance(self.lr_mult_list, str):
            self.lr_mult_list = eval(self.lr_mult_list)

        assert isinstance(self.lr_mult_list, (
            list, tuple
        )), "lr_mult_list should be in (list, tuple) but got {}".format(
            type(self.lr_mult_list))
        assert len(self.lr_mult_list
                   ) == 6, "lr_mult_list length should be 6 but got {}".format(
                       len(self.lr_mult_list))

        assert isinstance(self.stride_list, (
            list, tuple
        )), "stride_list should be in (list, tuple) but got {}".format(
            type(self.stride_list))
        assert len(self.stride_list
                   ) == 5, "stride_list length should be 5 but got {}".format(
                       len(self.stride_list))
C
cuicheng01 已提交
222

C
cuicheng01 已提交
223
        for i, stride in enumerate(stride_list[1:]):
C
cuicheng01 已提交
224
            self.net_config["blocks{}".format(i + 3)][0][3] = stride
C
cuicheng01 已提交
225 226 227 228
        self.conv1 = ConvBNLayer(
            num_channels=3,
            filter_size=3,
            num_filters=make_divisible(16 * scale),
C
cuicheng01 已提交
229 230
            stride=stride_list[0],
            lr_mult=self.lr_mult_list[0])
C
cuicheng01 已提交
231

C
cuicheng01 已提交
232
        self.blocks2 = nn.Sequential(*[
C
cuicheng01 已提交
233 234 235 236 237
            DepthwiseSeparable(
                num_channels=make_divisible(in_c * scale),
                num_filters=make_divisible(out_c * scale),
                dw_size=k,
                stride=s,
C
cuicheng01 已提交
238 239
                use_se=se,
                lr_mult=self.lr_mult_list[1])
C
cuicheng01 已提交
240 241
            for i, (k, in_c, out_c, s, se
                    ) in enumerate(self.net_config["blocks2"])
C
cuicheng01 已提交
242 243
        ])

C
cuicheng01 已提交
244
        self.blocks3 = nn.Sequential(*[
C
cuicheng01 已提交
245 246 247 248 249
            DepthwiseSeparable(
                num_channels=make_divisible(in_c * scale),
                num_filters=make_divisible(out_c * scale),
                dw_size=k,
                stride=s,
C
cuicheng01 已提交
250 251
                use_se=se,
                lr_mult=self.lr_mult_list[2])
C
cuicheng01 已提交
252 253
            for i, (k, in_c, out_c, s, se
                    ) in enumerate(self.net_config["blocks3"])
C
cuicheng01 已提交
254 255
        ])

C
cuicheng01 已提交
256
        self.blocks4 = nn.Sequential(*[
C
cuicheng01 已提交
257 258 259 260 261
            DepthwiseSeparable(
                num_channels=make_divisible(in_c * scale),
                num_filters=make_divisible(out_c * scale),
                dw_size=k,
                stride=s,
C
cuicheng01 已提交
262 263
                use_se=se,
                lr_mult=self.lr_mult_list[3])
C
cuicheng01 已提交
264 265
            for i, (k, in_c, out_c, s, se
                    ) in enumerate(self.net_config["blocks4"])
C
cuicheng01 已提交
266 267
        ])

C
cuicheng01 已提交
268
        self.blocks5 = nn.Sequential(*[
C
cuicheng01 已提交
269 270 271 272 273
            DepthwiseSeparable(
                num_channels=make_divisible(in_c * scale),
                num_filters=make_divisible(out_c * scale),
                dw_size=k,
                stride=s,
C
cuicheng01 已提交
274 275
                use_se=se,
                lr_mult=self.lr_mult_list[4])
C
cuicheng01 已提交
276 277
            for i, (k, in_c, out_c, s, se
                    ) in enumerate(self.net_config["blocks5"])
C
cuicheng01 已提交
278 279
        ])

C
cuicheng01 已提交
280
        self.blocks6 = nn.Sequential(*[
C
cuicheng01 已提交
281 282 283 284 285
            DepthwiseSeparable(
                num_channels=make_divisible(in_c * scale),
                num_filters=make_divisible(out_c * scale),
                dw_size=k,
                stride=s,
C
cuicheng01 已提交
286 287
                use_se=se,
                lr_mult=self.lr_mult_list[5])
C
cuicheng01 已提交
288 289
            for i, (k, in_c, out_c, s, se
                    ) in enumerate(self.net_config["blocks6"])
C
cuicheng01 已提交
290 291 292
        ])

        self.avg_pool = AdaptiveAvgPool2D(1)
C
cuicheng01 已提交
293 294
        if self.use_last_conv:
            self.last_conv = Conv2D(
C
cuicheng01 已提交
295 296
                in_channels=make_divisible(self.net_config["blocks6"][-1][2] *
                                           scale),
C
cuicheng01 已提交
297 298 299 300 301 302 303 304 305
                out_channels=self.class_expand,
                kernel_size=1,
                stride=1,
                padding=0,
                bias_attr=False)
            self.hardswish = nn.Hardswish()
            self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer")
        else:
            self.last_conv = None
C
cuicheng01 已提交
306
        self.flatten = nn.Flatten(start_axis=1, stop_axis=-1)
C
cuicheng01 已提交
307 308 309
        self.fc = Linear(
            self.class_expand if self.use_last_conv else
            make_divisible(self.net_config["blocks6"][-1][2]), class_num)
C
cuicheng01 已提交
310

311 312 313 314
        super().init_res(
            stages_pattern,
            return_patterns=return_patterns,
            return_stages=return_stages)
315

C
cuicheng01 已提交
316 317 318 319 320 321 322 323 324 325
    def forward(self, x):
        x = self.conv1(x)

        x = self.blocks2(x)
        x = self.blocks3(x)
        x = self.blocks4(x)
        x = self.blocks5(x)
        x = self.blocks6(x)

        x = self.avg_pool(x)
C
cuicheng01 已提交
326 327 328 329
        if self.last_conv is not None:
            x = self.last_conv(x)
            x = self.hardswish(x)
            x = self.dropout(x)
C
cuicheng01 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        x = self.flatten(x)
        x = self.fc(x)
        return x


def _load_pretrained(pretrained, model, model_url, use_ssld):
    if pretrained is False:
        pass
    elif pretrained is True:
        load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
    elif isinstance(pretrained, str):
        load_dygraph_pretrain(model, pretrained)
    else:
        raise RuntimeError(
            "pretrained type is not available. Please use `string` or `boolean` type."
        )


C
cuicheng01 已提交
348
def PPLCNet_x0_25(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
349
    """
C
cuicheng01 已提交
350
    PPLCNet_x0_25
C
cuicheng01 已提交
351 352 353 354 355
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
356
        model: nn.Layer. Specific `PPLCNet_x0_25` model depends on args.
C
cuicheng01 已提交
357
    """
358 359
    model = PPLCNet(
        scale=0.25, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
360
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x0_25"], use_ssld)
C
cuicheng01 已提交
361 362 363
    return model


C
cuicheng01 已提交
364
def PPLCNet_x0_35(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
365
    """
C
cuicheng01 已提交
366
    PPLCNet_x0_35
C
cuicheng01 已提交
367 368 369 370 371
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
372
        model: nn.Layer. Specific `PPLCNet_x0_35` model depends on args.
C
cuicheng01 已提交
373
    """
374 375
    model = PPLCNet(
        scale=0.35, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
376
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x0_35"], use_ssld)
C
cuicheng01 已提交
377 378 379
    return model


C
cuicheng01 已提交
380
def PPLCNet_x0_5(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
381
    """
C
cuicheng01 已提交
382
    PPLCNet_x0_5
C
cuicheng01 已提交
383 384 385 386 387
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
388
        model: nn.Layer. Specific `PPLCNet_x0_5` model depends on args.
C
cuicheng01 已提交
389
    """
390 391
    model = PPLCNet(
        scale=0.5, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
392
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x0_5"], use_ssld)
C
cuicheng01 已提交
393 394 395
    return model


C
cuicheng01 已提交
396
def PPLCNet_x0_75(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
397
    """
C
cuicheng01 已提交
398
    PPLCNet_x0_75
C
cuicheng01 已提交
399 400 401 402 403
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
404
        model: nn.Layer. Specific `PPLCNet_x0_75` model depends on args.
C
cuicheng01 已提交
405
    """
406 407
    model = PPLCNet(
        scale=0.75, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
408
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x0_75"], use_ssld)
C
cuicheng01 已提交
409 410 411
    return model


C
cuicheng01 已提交
412
def PPLCNet_x1_0(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
413
    """
C
cuicheng01 已提交
414
    PPLCNet_x1_0
C
cuicheng01 已提交
415 416 417 418 419
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
420
        model: nn.Layer. Specific `PPLCNet_x1_0` model depends on args.
C
cuicheng01 已提交
421
    """
422 423
    model = PPLCNet(
        scale=1.0, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
424
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x1_0"], use_ssld)
C
cuicheng01 已提交
425 426 427
    return model


C
cuicheng01 已提交
428
def PPLCNet_x1_5(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
429
    """
C
cuicheng01 已提交
430
    PPLCNet_x1_5
C
cuicheng01 已提交
431 432 433 434 435
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
436
        model: nn.Layer. Specific `PPLCNet_x1_5` model depends on args.
C
cuicheng01 已提交
437
    """
438 439
    model = PPLCNet(
        scale=1.5, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
440
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x1_5"], use_ssld)
C
cuicheng01 已提交
441 442 443
    return model


C
cuicheng01 已提交
444
def PPLCNet_x2_0(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
445
    """
C
cuicheng01 已提交
446
    PPLCNet_x2_0
C
cuicheng01 已提交
447 448 449 450 451
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
452
        model: nn.Layer. Specific `PPLCNet_x2_0` model depends on args.
C
cuicheng01 已提交
453
    """
454 455
    model = PPLCNet(
        scale=2.0, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
456
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x2_0"], use_ssld)
C
cuicheng01 已提交
457 458 459
    return model


C
cuicheng01 已提交
460
def PPLCNet_x2_5(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
461
    """
C
cuicheng01 已提交
462
    PPLCNet_x2_5
C
cuicheng01 已提交
463 464 465 466 467
    Args:
        pretrained: bool=False or str. If `True` load pretrained parameters, `False` otherwise.
                    If str, means the path of the pretrained model.
        use_ssld: bool=False. Whether using distillation pretrained model when pretrained=True.
    Returns:
C
cuicheng01 已提交
468
        model: nn.Layer. Specific `PPLCNet_x2_5` model depends on args.
C
cuicheng01 已提交
469
    """
470 471
    model = PPLCNet(
        scale=2.5, stages_pattern=MODEL_STAGES_PATTERN["PPLCNet"], **kwargs)
C
cuicheng01 已提交
472
    _load_pretrained(pretrained, model, MODEL_URLS["PPLCNet_x2_5"], use_ssld)
C
cuicheng01 已提交
473
    return model