PaddleClas/ppcls/arch/backbone/legendary_models/resnet.py

602 lines
21 KiB
Python
Raw Normal View History

2021-05-28 10:50:17 +08:00
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
2021-05-25 19:18:14 +08:00
#
# 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.
2022-04-29 13:58:26 +08:00
# reference: https://arxiv.org/pdf/1512.03385
2021-05-28 10:50:17 +08:00
from __future__ import absolute_import, division, print_function
2021-05-25 19:18:14 +08:00
import numpy as np
import paddle
from paddle import ParamAttr
import paddle.nn as nn
2022-05-12 10:52:34 +08:00
from paddle.nn import Conv2D, BatchNorm, Linear, BatchNorm2D
2021-05-25 19:18:14 +08:00
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
2022-05-12 10:52:34 +08:00
from paddle.regularizer import L2Decay
2021-05-25 19:18:14 +08:00
import math
2021-05-28 10:57:26 +08:00
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
2021-05-31 16:01:02 +08:00
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
2021-05-28 10:50:17 +08:00
MODEL_URLS = {
2021-05-31 16:01:02 +08:00
"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",
2021-05-28 10:50:17 +08:00
}
2021-05-25 19:18:14 +08:00
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]"]
}
2021-05-28 10:50:17 +08:00
__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.
'''
2021-05-25 19:18:14 +08:00
NET_CONFIG = {
"18": {
2021-05-31 16:01:02 +08:00
"block_type": "BasicBlock",
"block_depth": [2, 2, 2, 2],
"num_channels": [64, 64, 128, 256]
},
2021-05-25 19:18:14 +08:00
"34": {
2021-05-31 16:01:02 +08:00
"block_type": "BasicBlock",
"block_depth": [3, 4, 6, 3],
"num_channels": [64, 64, 128, 256]
},
2021-05-25 19:18:14 +08:00
"50": {
2021-05-31 16:01:02 +08:00
"block_type": "BottleneckBlock",
"block_depth": [3, 4, 6, 3],
"num_channels": [64, 256, 512, 1024]
},
2021-05-25 19:18:14 +08:00
"101": {
2021-05-31 16:01:02 +08:00
"block_type": "BottleneckBlock",
"block_depth": [3, 4, 23, 3],
"num_channels": [64, 256, 512, 1024]
},
2021-05-25 19:18:14 +08:00
"152": {
2021-05-31 16:01:02 +08:00
"block_type": "BottleneckBlock",
"block_depth": [3, 8, 36, 3],
"num_channels": [64, 256, 512, 1024]
},
2021-05-25 19:18:14 +08:00
"200": {
2021-05-31 16:01:02 +08:00
"block_type": "BottleneckBlock",
"block_depth": [3, 12, 48, 3],
"num_channels": [64, 256, 512, 1024]
},
2021-05-25 19:18:14 +08:00
}
class ConvBNLayer(TheseusLayer):
def __init__(self,
num_channels,
num_filters,
filter_size,
stride=1,
groups=1,
is_vd_mode=False,
act=None,
lr_mult=1.0,
data_format="NCHW"):
2021-05-28 10:50:17 +08:00
super().__init__()
2021-05-25 19:18:14 +08:00
self.is_vd_mode = is_vd_mode
self.act = act
2021-05-28 11:29:37 +08:00
self.avg_pool = AvgPool2D(
2021-05-25 19:18:14 +08:00
kernel_size=2, stride=2, padding=0, ceil_mode=True)
self.conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=(filter_size - 1) // 2,
groups=groups,
weight_attr=ParamAttr(learning_rate=lr_mult),
bias_attr=False,
data_format=data_format)
2022-05-12 10:52:34 +08:00
2022-05-18 19:58:53 +08:00
weight_attr = ParamAttr(learning_rate=lr_mult, trainable=True)
bias_attr = ParamAttr(learning_rate=lr_mult, trainable=True)
2022-05-12 10:52:34 +08:00
self.bn = BatchNorm2D(
2022-05-26 17:12:28 +08:00
num_filters,
weight_attr=ParamAttr(learning_rate=lr_mult),
bias_attr=ParamAttr(learning_rate=lr_mult),
data_format=data_format)
2021-05-25 19:18:14 +08:00
self.relu = nn.ReLU()
def forward(self, x):
if self.is_vd_mode:
2021-05-28 11:29:37 +08:00
x = self.avg_pool(x)
2021-05-25 19:18:14 +08:00
x = self.conv(x)
x = self.bn(x)
if self.act:
x = self.relu(x)
return x
class BottleneckBlock(TheseusLayer):
def __init__(self,
num_channels,
num_filters,
stride,
shortcut=True,
if_first=False,
lr_mult=1.0,
data_format="NCHW"):
2021-05-28 10:50:17 +08:00
super().__init__()
2021-05-25 19:18:14 +08:00
self.conv0 = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=1,
2021-05-28 10:50:17 +08:00
act="relu",
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
self.conv1 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters,
filter_size=3,
stride=stride,
2021-05-28 10:50:17 +08:00
act="relu",
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
self.conv2 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters * 4,
filter_size=1,
act=None,
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
if not shortcut:
self.short = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters * 4,
filter_size=1,
stride=stride if if_first else 1,
is_vd_mode=False if if_first else True,
lr_mult=lr_mult,
data_format=data_format)
2022-05-12 10:52:34 +08:00
2021-05-25 19:18:14 +08:00
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,
lr_mult=1.0,
data_format="NCHW"):
2021-05-28 10:50:17 +08:00
super().__init__()
2021-05-25 19:18:14 +08:00
self.stride = stride
self.conv0 = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=3,
stride=stride,
2021-05-28 10:50:17 +08:00
act="relu",
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
self.conv1 = ConvBNLayer(
num_channels=num_filters,
num_filters=num_filters,
filter_size=3,
act=None,
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
if not shortcut:
self.short = ConvBNLayer(
num_channels=num_channels,
num_filters=num_filters,
filter_size=1,
stride=stride if if_first else 1,
is_vd_mode=False if if_first else True,
lr_mult=lr_mult,
data_format=data_format)
2021-05-25 19:18:14 +08:00
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):
2021-05-28 10:50:17 +08:00
"""
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.
2021-05-25 19:18:14 +08:00
"""
2021-05-31 16:01:02 +08:00
2021-05-25 19:18:14 +08:00
def __init__(self,
config,
stages_pattern,
2021-05-28 10:50:17 +08:00
version="vb",
2022-04-21 14:37:00 +08:00
stem_act="relu",
2021-05-28 10:50:17 +08:00
class_num=1000,
lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0],
data_format="NCHW",
2021-09-15 11:03:46 +08:00
input_image_channel=3,
return_patterns=None,
2022-05-25 16:13:38 +08:00
return_stages=None,
**kargs):
2021-05-28 10:50:17 +08:00
super().__init__()
2021-05-25 19:18:14 +08:00
self.cfg = config
self.lr_mult_list = lr_mult_list
self.is_vd_mode = version == "vd"
2021-05-28 10:50:17 +08:00
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
2021-05-31 16:01:02 +08:00
2021-05-25 19:18:14 +08:00
assert isinstance(self.lr_mult_list, (
list, tuple
)), "lr_mult_list should be in (list, tuple) but got {}".format(
type(self.lr_mult_list))
2021-05-31 16:01:02 +08:00
assert len(self.lr_mult_list
) == 5, "lr_mult_list length should be 5 but got {}".format(
len(self.lr_mult_list))
2021-05-25 19:18:14 +08:00
self.stem_cfg = {
2021-05-28 10:50:17 +08:00
#num_channels, num_filters, filter_size, stride
"vb": [[input_image_channel, 64, 7, 2]],
"vd":
[[input_image_channel, 32, 3, 2], [32, 32, 3, 1], [32, 64, 3, 1]]
2021-05-31 16:01:02 +08:00
}
2022-05-12 10:52:34 +08:00
self.stem = nn.Sequential(* [
2021-05-25 19:18:14 +08:00
ConvBNLayer(
2021-05-31 16:01:02 +08:00
num_channels=in_c,
num_filters=out_c,
filter_size=k,
stride=s,
2022-04-06 19:49:10 +08:00
act=stem_act,
lr_mult=self.lr_mult_list[0],
data_format=data_format)
2021-05-25 19:18:14 +08:00
for in_c, out_c, k, s in self.stem_cfg[version]
])
2021-05-31 16:01:02 +08:00
self.max_pool = MaxPool2D(
kernel_size=3, stride=2, padding=1, data_format=data_format)
2021-05-28 10:50:17 +08:00
block_list = []
for block_idx in range(len(self.block_depth)):
2021-05-25 19:18:14 +08:00
shortcut = False
2021-05-28 10:50:17 +08:00
for i in range(self.block_depth[block_idx]):
2021-05-31 16:01:02 +08:00
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,
2021-05-28 10:50:17 +08:00
num_filters=self.num_filters[block_idx],
stride=2 if i == 0 and block_idx != 0 else 1,
2021-05-25 19:18:14 +08:00
shortcut=shortcut,
2021-05-28 10:50:17 +08:00
if_first=block_idx == i == 0 if version == "vd" else True,
lr_mult=self.lr_mult_list[block_idx + 1],
data_format=data_format))
2021-05-31 16:01:02 +08:00
shortcut = True
2021-05-28 10:50:17 +08:00
self.blocks = nn.Sequential(*block_list)
2021-05-25 19:18:14 +08:00
self.avg_pool = AdaptiveAvgPool2D(1, data_format=data_format)
self.flatten = nn.Flatten()
2021-05-31 16:49:25 +08:00
self.avg_pool_channels = self.num_channels[-1] * 2
2021-05-28 11:29:37 +08:00
stdv = 1.0 / math.sqrt(self.avg_pool_channels * 1.0)
2021-05-28 11:32:51 +08:00
self.fc = Linear(
2021-05-28 11:29:37 +08:00
self.avg_pool_channels,
2021-05-28 10:50:17 +08:00
self.class_num,
2021-05-31 16:01:02 +08:00
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
2021-05-25 19:18:14 +08:00
self.data_format = data_format
super().init_res(
stages_pattern,
return_patterns=return_patterns,
return_stages=return_stages)
2021-05-25 19:18:14 +08:00
def forward(self, x):
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)
2021-05-25 19:18:14 +08:00
return x
2021-05-31 16:01:02 +08:00
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."
)
def ResNet18(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet18
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet18` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["18"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet18"],
version="vb",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet18"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet18_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet18_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet18_vd` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["18"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet18"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet18_vd"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet34(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet34
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
2021-05-28 11:34:39 +08:00
model: nn.Layer. Specific `ResNet34` model depends on args.
2021-05-28 10:50:17 +08:00
"""
model = ResNet(
config=NET_CONFIG["34"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet34"],
version="vb",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet34"], use_ssld)
2021-05-28 10:50:17 +08:00
return model
2021-05-31 16:01:02 +08:00
def ResNet34_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet34_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
2021-05-28 11:34:39 +08:00
model: nn.Layer. Specific `ResNet34_vd` model depends on args.
2021-05-28 10:50:17 +08:00
"""
model = ResNet(
config=NET_CONFIG["34"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet34"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet34_vd"], use_ssld)
2021-05-28 10:50:17 +08:00
return model
2021-05-31 16:01:02 +08:00
def ResNet50(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet50
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet50` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["50"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet50"],
version="vb",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet50"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet50_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet50_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet50_vd` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["50"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet50"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet50_vd"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet101(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet101
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet101` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["101"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet101"],
version="vb",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet101"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet101_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet101_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet101_vd` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["101"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet101"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet101_vd"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet152(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet152
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet152` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["152"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet152"],
version="vb",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet152"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-28 10:50:17 +08:00
2021-05-31 16:01:02 +08:00
def ResNet152_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet152_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet152_vd` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["152"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet152"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet152_vd"], use_ssld)
2021-05-25 19:18:14 +08:00
return model
2021-05-31 16:01:02 +08:00
def ResNet200_vd(pretrained=False, use_ssld=False, **kwargs):
2021-05-28 10:50:17 +08:00
"""
ResNet200_vd
Args:
2021-05-31 16:01:02 +08:00
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.
2021-05-28 10:50:17 +08:00
Returns:
model: nn.Layer. Specific `ResNet200_vd` model depends on args.
"""
model = ResNet(
config=NET_CONFIG["200"],
stages_pattern=MODEL_STAGES_PATTERN["ResNet200"],
version="vd",
**kwargs)
2021-05-31 16:01:02 +08:00
_load_pretrained(pretrained, model, MODEL_URLS["ResNet200_vd"], use_ssld)
2021-05-25 19:18:14 +08:00
return model