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

G
gaotingquan 已提交
15 16
# reference: https://arxiv.org/pdf/1512.03385

C
cuicheng01 已提交
17
from __future__ import absolute_import, division, print_function
C
cuicheng01 已提交
18 19 20 21 22

import numpy as np
import paddle
from paddle import ParamAttr
import paddle.nn as nn
Z
zhiboniu 已提交
23
from paddle.nn import Conv2D, BatchNorm, Linear, BatchNorm2D
C
cuicheng01 已提交
24 25
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
C
cuicheng01 已提交
26
from paddle.regularizer import L2Decay
C
cuicheng01 已提交
27 28
import math

C
cuicheng01 已提交
29
from ppcls.utils import logger
C
cuicheng01 已提交
30
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
D
dongshuilong 已提交
31
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
C
cuicheng01 已提交
32 33

MODEL_URLS = {
D
dongshuilong 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    "ResNet18":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet18_pretrained.pdparams",
    "ResNet18_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet18_vd_pretrained.pdparams",
    "ResNet34":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet34_pretrained.pdparams",
    "ResNet34_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet34_vd_pretrained.pdparams",
    "ResNet50":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet50_pretrained.pdparams",
    "ResNet50_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet50_vd_pretrained.pdparams",
    "ResNet101":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet101_pretrained.pdparams",
    "ResNet101_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet101_vd_pretrained.pdparams",
    "ResNet152":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet152_pretrained.pdparams",
    "ResNet152_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet152_vd_pretrained.pdparams",
    "ResNet200_vd":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet200_vd_pretrained.pdparams",
C
cuicheng01 已提交
56
}
C
cuicheng01 已提交
57

58 59 60 61 62 63 64 65 66
MODEL_STAGES_PATTERN = {
    "ResNet18": ["blocks[1]", "blocks[3]", "blocks[5]", "blocks[7]"],
    "ResNet34": ["blocks[2]", "blocks[6]", "blocks[12]", "blocks[15]"],
    "ResNet50": ["blocks[2]", "blocks[6]", "blocks[12]", "blocks[15]"],
    "ResNet101": ["blocks[2]", "blocks[6]", "blocks[29]", "blocks[32]"],
    "ResNet152": ["blocks[2]", "blocks[10]", "blocks[46]", "blocks[49]"],
    "ResNet200": ["blocks[2]", "blocks[14]", "blocks[62]", "blocks[65]"]
}

C
cuicheng01 已提交
67 68 69 70 71 72 73 74 75 76
__all__ = MODEL_URLS.keys()
'''
ResNet config: dict.
    key: depth of ResNet.
    values: config's dict of specific model.
        keys:
            block_type: Two different blocks in ResNet, BasicBlock and BottleneckBlock are optional.
            block_depth: The number of blocks in different stages in ResNet.
            num_channels: The number of channels to enter the next stage.
'''
C
cuicheng01 已提交
77 78
NET_CONFIG = {
    "18": {
D
dongshuilong 已提交
79 80 81 82
        "block_type": "BasicBlock",
        "block_depth": [2, 2, 2, 2],
        "num_channels": [64, 64, 128, 256]
    },
C
cuicheng01 已提交
83
    "34": {
D
dongshuilong 已提交
84 85 86 87
        "block_type": "BasicBlock",
        "block_depth": [3, 4, 6, 3],
        "num_channels": [64, 64, 128, 256]
    },
C
cuicheng01 已提交
88
    "50": {
D
dongshuilong 已提交
89 90 91 92
        "block_type": "BottleneckBlock",
        "block_depth": [3, 4, 6, 3],
        "num_channels": [64, 256, 512, 1024]
    },
C
cuicheng01 已提交
93
    "101": {
D
dongshuilong 已提交
94 95 96 97
        "block_type": "BottleneckBlock",
        "block_depth": [3, 4, 23, 3],
        "num_channels": [64, 256, 512, 1024]
    },
C
cuicheng01 已提交
98
    "152": {
D
dongshuilong 已提交
99 100 101 102
        "block_type": "BottleneckBlock",
        "block_depth": [3, 8, 36, 3],
        "num_channels": [64, 256, 512, 1024]
    },
C
cuicheng01 已提交
103
    "200": {
D
dongshuilong 已提交
104 105 106 107
        "block_type": "BottleneckBlock",
        "block_depth": [3, 12, 48, 3],
        "num_channels": [64, 256, 512, 1024]
    },
C
cuicheng01 已提交
108 109 110 111 112 113 114 115 116 117 118 119
}


class ConvBNLayer(TheseusLayer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 is_vd_mode=False,
                 act=None,
littletomatodonkey's avatar
littletomatodonkey 已提交
120 121
                 lr_mult=1.0,
                 data_format="NCHW"):
C
cuicheng01 已提交
122
        super().__init__()
C
cuicheng01 已提交
123 124
        self.is_vd_mode = is_vd_mode
        self.act = act
C
cuicheng01 已提交
125
        self.avg_pool = AvgPool2D(
C
cuicheng01 已提交
126
            kernel_size=2, stride=stride, padding="SAME", ceil_mode=True)
C
cuicheng01 已提交
127 128 129 130
        self.conv = Conv2D(
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
C
cuicheng01 已提交
131
            stride=1 if is_vd_mode else stride,
C
cuicheng01 已提交
132 133 134
            padding=(filter_size - 1) // 2,
            groups=groups,
            weight_attr=ParamAttr(learning_rate=lr_mult),
littletomatodonkey's avatar
littletomatodonkey 已提交
135 136
            bias_attr=False,
            data_format=data_format)
C
cuicheng01 已提交
137

C
cuicheng01 已提交
138 139 140
        self.bn = BatchNorm(
            num_filters,
            param_attr=ParamAttr(learning_rate=lr_mult),
littletomatodonkey's avatar
littletomatodonkey 已提交
141 142
            bias_attr=ParamAttr(learning_rate=lr_mult),
            data_layout=data_format)
C
cuicheng01 已提交
143 144 145 146
        self.relu = nn.ReLU()

    def forward(self, x):
        if self.is_vd_mode:
C
cuicheng01 已提交
147
            x = self.avg_pool(x)
C
cuicheng01 已提交
148 149 150 151 152 153 154 155
        x = self.conv(x)
        x = self.bn(x)
        if self.act:
            x = self.relu(x)
        return x


class BottleneckBlock(TheseusLayer):
littletomatodonkey's avatar
littletomatodonkey 已提交
156 157 158 159 160 161 162 163
    def __init__(self,
                 num_channels,
                 num_filters,
                 stride,
                 shortcut=True,
                 if_first=False,
                 lr_mult=1.0,
                 data_format="NCHW"):
C
cuicheng01 已提交
164
        super().__init__()
C
cuicheng01 已提交
165 166 167 168
        self.conv0 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=1,
C
cuicheng01 已提交
169
            act="relu",
littletomatodonkey's avatar
littletomatodonkey 已提交
170 171
            lr_mult=lr_mult,
            data_format=data_format)
C
cuicheng01 已提交
172 173 174 175 176
        self.conv1 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
C
cuicheng01 已提交
177
            act="relu",
littletomatodonkey's avatar
littletomatodonkey 已提交
178 179
            lr_mult=lr_mult,
            data_format=data_format)
C
cuicheng01 已提交
180 181 182 183 184
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters * 4,
            filter_size=1,
            act=None,
littletomatodonkey's avatar
littletomatodonkey 已提交
185 186
            lr_mult=lr_mult,
            data_format=data_format)
C
cuicheng01 已提交
187 188 189 190 191 192

        if not shortcut:
            self.short = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters * 4,
                filter_size=1,
C
cuicheng01 已提交
193
                stride=stride,
C
cuicheng01 已提交
194
                is_vd_mode=False if if_first else True,
littletomatodonkey's avatar
littletomatodonkey 已提交
195 196
                lr_mult=lr_mult,
                data_format=data_format)
C
cuicheng01 已提交
197

C
cuicheng01 已提交
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
        self.relu = nn.ReLU()
        self.shortcut = shortcut

    def forward(self, x):
        identity = x
        x = self.conv0(x)
        x = self.conv1(x)
        x = self.conv2(x)

        if self.shortcut:
            short = identity
        else:
            short = self.short(identity)
        x = paddle.add(x=x, y=short)
        x = self.relu(x)
        return x


class BasicBlock(TheseusLayer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 stride,
                 shortcut=True,
                 if_first=False,
littletomatodonkey's avatar
littletomatodonkey 已提交
223 224
                 lr_mult=1.0,
                 data_format="NCHW"):
C
cuicheng01 已提交
225 226
        super().__init__()

C
cuicheng01 已提交
227 228 229 230 231 232
        self.stride = stride
        self.conv0 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
C
cuicheng01 已提交
233
            act="relu",
littletomatodonkey's avatar
littletomatodonkey 已提交
234 235
            lr_mult=lr_mult,
            data_format=data_format)
C
cuicheng01 已提交
236 237 238 239 240
        self.conv1 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            act=None,
littletomatodonkey's avatar
littletomatodonkey 已提交
241 242
            lr_mult=lr_mult,
            data_format=data_format)
C
cuicheng01 已提交
243 244 245 246 247
        if not shortcut:
            self.short = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters,
                filter_size=1,
C
cuicheng01 已提交
248
                stride=stride,
C
cuicheng01 已提交
249
                is_vd_mode=False if if_first else True,
littletomatodonkey's avatar
littletomatodonkey 已提交
250 251
                lr_mult=lr_mult,
                data_format=data_format)
C
cuicheng01 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        self.shortcut = shortcut
        self.relu = nn.ReLU()

    def forward(self, x):
        identity = x
        x = self.conv0(x)
        x = self.conv1(x)
        if self.shortcut:
            short = identity
        else:
            short = self.short(identity)
        x = paddle.add(x=x, y=short)
        x = self.relu(x)
        return x


class ResNet(TheseusLayer):
C
cuicheng01 已提交
269 270 271 272 273 274 275 276 277
    """
    ResNet
    Args:
        config: dict. config of ResNet.
        version: str="vb". Different version of ResNet, version vd can perform better. 
        class_num: int=1000. The number of classes.
        lr_mult_list: list. Control the learning rate of different stages.
    Returns:
        model: nn.Layer. Specific ResNet model depends on args.
C
cuicheng01 已提交
278
    """
D
dongshuilong 已提交
279

C
cuicheng01 已提交
280 281
    def __init__(self,
                 config,
282
                 stages_pattern,
C
cuicheng01 已提交
283
                 version="vb",
H
HydrogenSulfate 已提交
284
                 stem_act="relu",
C
cuicheng01 已提交
285
                 class_num=1000,
littletomatodonkey's avatar
littletomatodonkey 已提交
286
                 lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0],
C
cuicheng01 已提交
287
                 stride_list=[2, 2, 2, 2, 2],
littletomatodonkey's avatar
littletomatodonkey 已提交
288
                 data_format="NCHW",
W
weishengyu 已提交
289
                 input_image_channel=3,
290
                 return_patterns=None,
C
cuicheng01 已提交
291 292
                 return_stages=None,
                 **kargs):
C
cuicheng01 已提交
293
        super().__init__()
C
cuicheng01 已提交
294 295 296

        self.cfg = config
        self.lr_mult_list = lr_mult_list
C
cuicheng01 已提交
297
        self.stride_list = stride_list
C
cuicheng01 已提交
298
        self.is_vd_mode = version == "vd"
C
cuicheng01 已提交
299 300 301 302 303 304
        self.class_num = class_num
        self.num_filters = [64, 128, 256, 512]
        self.block_depth = self.cfg["block_depth"]
        self.block_type = self.cfg["block_type"]
        self.num_channels = self.cfg["num_channels"]
        self.channels_mult = 1 if self.num_channels[-1] == 256 else 4
D
dongshuilong 已提交
305

C
cuicheng01 已提交
306 307 308 309
        assert isinstance(self.lr_mult_list, (
            list, tuple
        )), "lr_mult_list should be in (list, tuple) but got {}".format(
            type(self.lr_mult_list))
C
cuicheng01 已提交
310
        if len(self.lr_mult_list) != 5:
C
cuicheng01 已提交
311 312
            msg = "lr_mult_list length should be 5 but got {}, default lr_mult_list used".format(
                len(self.lr_mult_list))
C
cuicheng01 已提交
313
            logger.warning(msg)
C
cuicheng01 已提交
314
            self.lr_mult_list = [1.0, 1.0, 1.0, 1.0, 1.0]
C
cuicheng01 已提交
315

C
cuicheng01 已提交
316 317 318 319 320 321 322 323
        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 已提交
324
        self.stem_cfg = {
C
cuicheng01 已提交
325
            #num_channels, num_filters, filter_size, stride
C
cuicheng01 已提交
326
            "vb": [[input_image_channel, 64, 7, self.stride_list[0]]],
C
cuicheng01 已提交
327 328
            "vd": [[input_image_channel, 32, 3, self.stride_list[0]],
                   [32, 32, 3, 1], [32, 64, 3, 1]]
D
dongshuilong 已提交
329 330
        }

C
cuicheng01 已提交
331
        self.stem = nn.Sequential(*[
C
cuicheng01 已提交
332
            ConvBNLayer(
D
dongshuilong 已提交
333 334 335 336
                num_channels=in_c,
                num_filters=out_c,
                filter_size=k,
                stride=s,
W
dbg  
weishengyu 已提交
337
                act=stem_act,
littletomatodonkey's avatar
littletomatodonkey 已提交
338 339
                lr_mult=self.lr_mult_list[0],
                data_format=data_format)
C
cuicheng01 已提交
340 341
            for in_c, out_c, k, s in self.stem_cfg[version]
        ])
D
dongshuilong 已提交
342

littletomatodonkey's avatar
littletomatodonkey 已提交
343
        self.max_pool = MaxPool2D(
C
cuicheng01 已提交
344 345 346 347
            kernel_size=3,
            stride=stride_list[1],
            padding=1,
            data_format=data_format)
C
cuicheng01 已提交
348 349
        block_list = []
        for block_idx in range(len(self.block_depth)):
C
cuicheng01 已提交
350
            shortcut = False
C
cuicheng01 已提交
351
            for i in range(self.block_depth[block_idx]):
D
dongshuilong 已提交
352 353 354
                block_list.append(globals()[self.block_type](
                    num_channels=self.num_channels[block_idx] if i == 0 else
                    self.num_filters[block_idx] * self.channels_mult,
C
cuicheng01 已提交
355
                    num_filters=self.num_filters[block_idx],
C
cuicheng01 已提交
356 357
                    stride=self.stride_list[block_idx + 1]
                    if i == 0 and block_idx != 0 else 1,
C
cuicheng01 已提交
358
                    shortcut=shortcut,
C
cuicheng01 已提交
359
                    if_first=block_idx == i == 0 if version == "vd" else True,
littletomatodonkey's avatar
littletomatodonkey 已提交
360 361
                    lr_mult=self.lr_mult_list[block_idx + 1],
                    data_format=data_format))
D
dongshuilong 已提交
362
                shortcut = True
C
cuicheng01 已提交
363
        self.blocks = nn.Sequential(*block_list)
C
cuicheng01 已提交
364

littletomatodonkey's avatar
littletomatodonkey 已提交
365
        self.avg_pool = AdaptiveAvgPool2D(1, data_format=data_format)
366
        self.flatten = nn.Flatten()
W
dbg  
weishengyu 已提交
367
        self.avg_pool_channels = self.num_channels[-1] * 2
C
cuicheng01 已提交
368
        stdv = 1.0 / math.sqrt(self.avg_pool_channels * 1.0)
C
cuicheng01 已提交
369
        self.fc = Linear(
C
cuicheng01 已提交
370
            self.avg_pool_channels,
C
cuicheng01 已提交
371
            self.class_num,
D
dongshuilong 已提交
372
            weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
C
cuicheng01 已提交
373

littletomatodonkey's avatar
littletomatodonkey 已提交
374
        self.data_format = data_format
375 376 377 378 379

        super().init_res(
            stages_pattern,
            return_patterns=return_patterns,
            return_stages=return_stages)
littletomatodonkey's avatar
littletomatodonkey 已提交
380

C
cuicheng01 已提交
381
    def forward(self, x):
littletomatodonkey's avatar
littletomatodonkey 已提交
382 383 384 385 386 387 388 389 390 391
        with paddle.static.amp.fp16_guard():
            if self.data_format == "NHWC":
                x = paddle.transpose(x, [0, 2, 3, 1])
                x.stop_gradient = True
            x = self.stem(x)
            x = self.max_pool(x)
            x = self.blocks(x)
            x = self.avg_pool(x)
            x = self.flatten(x)
            x = self.fc(x)
C
cuicheng01 已提交
392 393 394
        return x


D
dongshuilong 已提交
395 396 397 398 399 400
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):
H
HydrogenSulfate 已提交
401 402 403 404
        if 'http' in pretrained:
            load_dygraph_pretrain_from_url(model, pretrained, use_ssld=False)
        else:
            load_dygraph_pretrain(model, pretrained)
D
dongshuilong 已提交
405 406 407 408 409 410 411
    else:
        raise RuntimeError(
            "pretrained type is not available. Please use `string` or `boolean` type."
        )


def ResNet18(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
412 413 414
    """
    ResNet18
    Args:
D
dongshuilong 已提交
415 416 417
        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.
C
cuicheng01 已提交
418 419 420
    Returns:
        model: nn.Layer. Specific `ResNet18` model depends on args.
    """
421 422 423 424 425
    model = ResNet(
        config=NET_CONFIG["18"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet18"],
        version="vb",
        **kwargs)
D
dongshuilong 已提交
426
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet18"], use_ssld)
C
cuicheng01 已提交
427 428
    return model

C
cuicheng01 已提交
429

D
dongshuilong 已提交
430
def ResNet18_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
431 432 433
    """
    ResNet18_vd
    Args:
D
dongshuilong 已提交
434 435 436
        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.
C
cuicheng01 已提交
437 438 439
    Returns:
        model: nn.Layer. Specific `ResNet18_vd` model depends on args.
    """
440 441 442 443 444
    model = ResNet(
        config=NET_CONFIG["18"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet18"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
445
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet18_vd"], use_ssld)
C
cuicheng01 已提交
446 447
    return model

C
cuicheng01 已提交
448

D
dongshuilong 已提交
449
def ResNet34(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
450 451 452
    """
    ResNet34
    Args:
D
dongshuilong 已提交
453 454 455
        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.
C
cuicheng01 已提交
456
    Returns:
C
cuicheng01 已提交
457
        model: nn.Layer. Specific `ResNet34` model depends on args.
C
cuicheng01 已提交
458
    """
459 460 461 462 463
    model = ResNet(
        config=NET_CONFIG["34"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet34"],
        version="vb",
        **kwargs)
D
dongshuilong 已提交
464
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet34"], use_ssld)
C
cuicheng01 已提交
465 466 467
    return model


D
dongshuilong 已提交
468
def ResNet34_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
469 470 471
    """
    ResNet34_vd
    Args:
D
dongshuilong 已提交
472 473 474
        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.
C
cuicheng01 已提交
475
    Returns:
C
cuicheng01 已提交
476
        model: nn.Layer. Specific `ResNet34_vd` model depends on args.
C
cuicheng01 已提交
477
    """
478 479 480 481 482
    model = ResNet(
        config=NET_CONFIG["34"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet34"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
483
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet34_vd"], use_ssld)
C
cuicheng01 已提交
484 485 486
    return model


D
dongshuilong 已提交
487
def ResNet50(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
488 489 490
    """
    ResNet50
    Args:
D
dongshuilong 已提交
491 492 493
        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.
C
cuicheng01 已提交
494 495 496
    Returns:
        model: nn.Layer. Specific `ResNet50` model depends on args.
    """
497 498 499 500 501
    model = ResNet(
        config=NET_CONFIG["50"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet50"],
        version="vb",
        **kwargs)
D
dongshuilong 已提交
502
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet50"], use_ssld)
C
cuicheng01 已提交
503 504
    return model

C
cuicheng01 已提交
505

D
dongshuilong 已提交
506
def ResNet50_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
507 508 509
    """
    ResNet50_vd
    Args:
D
dongshuilong 已提交
510 511 512
        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.
C
cuicheng01 已提交
513 514 515
    Returns:
        model: nn.Layer. Specific `ResNet50_vd` model depends on args.
    """
516 517 518 519 520
    model = ResNet(
        config=NET_CONFIG["50"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet50"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
521
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet50_vd"], use_ssld)
C
cuicheng01 已提交
522 523
    return model

C
cuicheng01 已提交
524

D
dongshuilong 已提交
525
def ResNet101(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
526 527 528
    """
    ResNet101
    Args:
D
dongshuilong 已提交
529 530 531
        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.
C
cuicheng01 已提交
532 533 534
    Returns:
        model: nn.Layer. Specific `ResNet101` model depends on args.
    """
535 536 537 538 539
    model = ResNet(
        config=NET_CONFIG["101"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet101"],
        version="vb",
        **kwargs)
D
dongshuilong 已提交
540
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet101"], use_ssld)
C
cuicheng01 已提交
541 542
    return model

C
cuicheng01 已提交
543

D
dongshuilong 已提交
544
def ResNet101_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
545 546 547
    """
    ResNet101_vd
    Args:
D
dongshuilong 已提交
548 549 550
        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.
C
cuicheng01 已提交
551 552 553
    Returns:
        model: nn.Layer. Specific `ResNet101_vd` model depends on args.
    """
554 555 556 557 558
    model = ResNet(
        config=NET_CONFIG["101"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet101"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
559
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet101_vd"], use_ssld)
C
cuicheng01 已提交
560 561
    return model

C
cuicheng01 已提交
562

D
dongshuilong 已提交
563
def ResNet152(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
564 565 566
    """
    ResNet152
    Args:
D
dongshuilong 已提交
567 568 569
        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.
C
cuicheng01 已提交
570 571 572
    Returns:
        model: nn.Layer. Specific `ResNet152` model depends on args.
    """
573 574 575 576 577
    model = ResNet(
        config=NET_CONFIG["152"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet152"],
        version="vb",
        **kwargs)
D
dongshuilong 已提交
578
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet152"], use_ssld)
C
cuicheng01 已提交
579 580
    return model

C
cuicheng01 已提交
581

D
dongshuilong 已提交
582
def ResNet152_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
583 584 585
    """
    ResNet152_vd
    Args:
D
dongshuilong 已提交
586 587 588
        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.
C
cuicheng01 已提交
589 590 591
    Returns:
        model: nn.Layer. Specific `ResNet152_vd` model depends on args.
    """
592 593 594 595 596
    model = ResNet(
        config=NET_CONFIG["152"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet152"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
597
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet152_vd"], use_ssld)
C
cuicheng01 已提交
598 599 600
    return model


D
dongshuilong 已提交
601
def ResNet200_vd(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
602 603 604
    """
    ResNet200_vd
    Args:
D
dongshuilong 已提交
605 606 607
        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.
C
cuicheng01 已提交
608 609 610
    Returns:
        model: nn.Layer. Specific `ResNet200_vd` model depends on args.
    """
611 612 613 614 615
    model = ResNet(
        config=NET_CONFIG["200"],
        stages_pattern=MODEL_STAGES_PATTERN["ResNet200"],
        version="vd",
        **kwargs)
D
dongshuilong 已提交
616
    _load_pretrained(pretrained, model, MODEL_URLS["ResNet200_vd"], use_ssld)
C
cuicheng01 已提交
617
    return model