hrnet.py 26.2 KB
Newer Older
W
weishengyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# copyright (c) 2020 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.

G
gaotingquan 已提交
15 16
# reference: https://arxiv.org/abs/1908.07919

W
weishengyu 已提交
17 18 19 20 21 22
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
import paddle
W
weishengyu 已提交
23
from paddle import nn
W
weishengyu 已提交
24
from paddle import ParamAttr
W
weishengyu 已提交
25
from paddle.nn.functional import upsample
W
weishengyu 已提交
26 27
from paddle.nn.initializer import Uniform

W
weishengyu 已提交
28
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer, Identity
D
dongshuilong 已提交
29
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
W
weishengyu 已提交
30

W
weishengyu 已提交
31
MODEL_URLS = {
D
dongshuilong 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45
    "HRNet_W18_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W18_C_pretrained.pdparams",
    "HRNet_W30_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W30_C_pretrained.pdparams",
    "HRNet_W32_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W32_C_pretrained.pdparams",
    "HRNet_W40_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W40_C_pretrained.pdparams",
    "HRNet_W44_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W44_C_pretrained.pdparams",
    "HRNet_W48_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W48_C_pretrained.pdparams",
    "HRNet_W64_C":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/HRNet_W64_C_pretrained.pdparams"
W
weishengyu 已提交
46 47
}

48 49
MODEL_STAGES_PATTERN = {"HRNet": ["st4"]}

W
weishengyu 已提交
50
__all__ = list(MODEL_URLS.keys())
W
weishengyu 已提交
51 52


D
dongshuilong 已提交
53 54 55 56 57 58 59 60 61 62 63 64
def _create_act(act):
    if act == "hardswish":
        return nn.Hardswish()
    elif act == "relu":
        return nn.ReLU()
    elif act is None:
        return Identity()
    else:
        raise RuntimeError(
            "The activation function is not supported: {}".format(act))


W
weishengyu 已提交
65 66 67 68 69 70 71
class ConvBNLayer(TheseusLayer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
72
                 act="relu"):
D
dongshuilong 已提交
73
        super().__init__()
W
weishengyu 已提交
74

W
weishengyu 已提交
75
        self.conv = nn.Conv2D(
W
weishengyu 已提交
76 77 78 79 80 81 82
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            bias_attr=False)
D
dongshuilong 已提交
83 84
        self.bn = nn.BatchNorm(num_filters, act=None)
        self.act = _create_act(act)
W
weishengyu 已提交
85

W
weishengyu 已提交
86 87 88 89 90 91 92
    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.act(x)
        return x


W
weishengyu 已提交
93 94 95 96 97 98
class BottleneckBlock(TheseusLayer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 has_se,
                 stride=1,
W
weishengyu 已提交
99
                 downsample=False):
D
dongshuilong 已提交
100
        super().__init__()
W
weishengyu 已提交
101 102 103 104 105 106 107 108

        self.has_se = has_se
        self.downsample = downsample

        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=1,
W
weishengyu 已提交
109
            act="relu")
W
weishengyu 已提交
110 111 112 113 114
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
W
weishengyu 已提交
115
            act="relu")
W
weishengyu 已提交
116 117 118 119
        self.conv3 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters * 4,
            filter_size=1,
W
weishengyu 已提交
120
            act=None)
W
weishengyu 已提交
121 122 123 124 125 126

        if self.downsample:
            self.conv_down = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters * 4,
                filter_size=1,
W
weishengyu 已提交
127
                act=None)
W
weishengyu 已提交
128 129 130 131 132

        if self.has_se:
            self.se = SELayer(
                num_channels=num_filters * 4,
                num_filters=num_filters * 4,
W
weishengyu 已提交
133
                reduction_ratio=16)
W
weishengyu 已提交
134
        self.relu = nn.ReLU()
W
weishengyu 已提交
135

W
weishengyu 已提交
136 137
    def forward(self, x, res_dict=None):
        residual = x
W
weishengyu 已提交
138 139 140
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
W
weishengyu 已提交
141
        if self.downsample:
W
weishengyu 已提交
142
            residual = self.conv_down(residual)
W
weishengyu 已提交
143
        if self.has_se:
W
weishengyu 已提交
144 145 146 147
            x = self.se(x)
        x = paddle.add(x=residual, y=x)
        x = self.relu(x)
        return x
W
weishengyu 已提交
148 149


W
dbg  
weishengyu 已提交
150
class BasicBlock(nn.Layer):
D
dongshuilong 已提交
151 152
    def __init__(self, num_channels, num_filters, has_se=False):
        super().__init__()
W
weishengyu 已提交
153 154 155 156 157 158 159

        self.has_se = has_se

        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=3,
W
dbg  
weishengyu 已提交
160
            stride=1,
W
dbg  
weishengyu 已提交
161
            act="relu")
W
weishengyu 已提交
162 163 164 165 166
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=1,
W
dbg  
weishengyu 已提交
167
            act=None)
W
weishengyu 已提交
168 169 170 171 172

        if self.has_se:
            self.se = SELayer(
                num_channels=num_filters,
                num_filters=num_filters,
W
weishengyu 已提交
173
                reduction_ratio=16)
W
weishengyu 已提交
174
        self.relu = nn.ReLU()
W
weishengyu 已提交
175

W
weishengyu 已提交
176 177 178 179
    def forward(self, x):
        residual = x
        x = self.conv1(x)
        x = self.conv2(x)
W
weishengyu 已提交
180 181

        if self.has_se:
W
weishengyu 已提交
182
            x = self.se(x)
W
weishengyu 已提交
183

W
weishengyu 已提交
184 185 186
        x = paddle.add(x=residual, y=x)
        x = self.relu(x)
        return x
W
weishengyu 已提交
187 188 189


class SELayer(TheseusLayer):
W
weishengyu 已提交
190
    def __init__(self, num_channels, num_filters, reduction_ratio):
D
dongshuilong 已提交
191
        super().__init__()
W
weishengyu 已提交
192

D
dongshuilong 已提交
193
        self.avg_pool = nn.AdaptiveAvgPool2D(1)
W
weishengyu 已提交
194 195 196 197 198

        self._num_channels = num_channels

        med_ch = int(num_channels / reduction_ratio)
        stdv = 1.0 / math.sqrt(num_channels * 1.0)
W
weishengyu 已提交
199
        self.fc_squeeze = nn.Linear(
W
weishengyu 已提交
200 201
            num_channels,
            med_ch,
D
dongshuilong 已提交
202
            weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
W
weishengyu 已提交
203
        self.relu = nn.ReLU()
W
weishengyu 已提交
204
        stdv = 1.0 / math.sqrt(med_ch * 1.0)
W
weishengyu 已提交
205
        self.fc_excitation = nn.Linear(
W
weishengyu 已提交
206 207
            med_ch,
            num_filters,
W
weishengyu 已提交
208
            weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
W
weishengyu 已提交
209
        self.sigmoid = nn.Sigmoid()
W
weishengyu 已提交
210

W
weishengyu 已提交
211 212
    def forward(self, x, res_dict=None):
        residual = x
D
dongshuilong 已提交
213
        x = self.avg_pool(x)
W
weishengyu 已提交
214 215 216 217 218 219 220 221
        x = paddle.squeeze(x, axis=[2, 3])
        x = self.fc_squeeze(x)
        x = self.relu(x)
        x = self.fc_excitation(x)
        x = self.sigmoid(x)
        x = paddle.unsqueeze(x, axis=[2, 3])
        x = residual * x
        return x
W
weishengyu 已提交
222 223 224


class Stage(TheseusLayer):
D
dongshuilong 已提交
225 226
    def __init__(self, num_modules, num_filters, has_se=False):
        super().__init__()
W
weishengyu 已提交
227 228 229

        self._num_modules = num_modules

W
dbg  
weishengyu 已提交
230
        self.stage_func_list = nn.LayerList()
W
weishengyu 已提交
231
        for i in range(num_modules):
W
weishengyu 已提交
232 233
            self.stage_func_list.append(
                HighResolutionModule(
D
dongshuilong 已提交
234
                    num_filters=num_filters, has_se=has_se))
W
weishengyu 已提交
235

W
weishengyu 已提交
236 237
    def forward(self, x, res_dict=None):
        x = x
W
weishengyu 已提交
238
        for idx in range(self._num_modules):
W
weishengyu 已提交
239 240
            x = self.stage_func_list[idx](x)
        return x
W
weishengyu 已提交
241 242 243


class HighResolutionModule(TheseusLayer):
D
dongshuilong 已提交
244 245
    def __init__(self, num_filters, has_se=False):
        super().__init__()
W
weishengyu 已提交
246

W
weishengyu 已提交
247
        self.basic_block_list = nn.LayerList()
W
dbg  
weishengyu 已提交
248 249

        for i in range(len(num_filters)):
W
weishengyu 已提交
250
            self.basic_block_list.append(
251
                nn.Sequential(* [
W
dbg  
weishengyu 已提交
252
                    BasicBlock(
W
weishengyu 已提交
253
                        num_channels=num_filters[i],
W
dbg  
weishengyu 已提交
254
                        num_filters=num_filters[i],
D
dongshuilong 已提交
255 256
                        has_se=has_se) for j in range(4)
                ]))
W
weishengyu 已提交
257 258

        self.fuse_func = FuseLayers(
D
dongshuilong 已提交
259
            in_channels=num_filters, out_channels=num_filters)
W
weishengyu 已提交
260

W
weishengyu 已提交
261 262 263
    def forward(self, x, res_dict=None):
        out = []
        for idx, xi in enumerate(x):
W
dbg  
weishengyu 已提交
264 265
            basic_block_list = self.basic_block_list[idx]
            for basic_block_func in basic_block_list:
W
weishengyu 已提交
266 267 268
                xi = basic_block_func(xi)
            out.append(xi)
        out = self.fuse_func(out)
W
weishengyu 已提交
269 270 271 272
        return out


class FuseLayers(TheseusLayer):
D
dongshuilong 已提交
273 274
    def __init__(self, in_channels, out_channels):
        super().__init__()
W
weishengyu 已提交
275

W
weishengyu 已提交
276
        self._actual_ch = len(in_channels)
W
weishengyu 已提交
277 278
        self._in_channels = in_channels

W
weishengyu 已提交
279
        self.residual_func_list = nn.LayerList()
W
weishengyu 已提交
280
        self.relu = nn.ReLU()
W
weishengyu 已提交
281
        for i in range(len(in_channels)):
W
weishengyu 已提交
282 283
            for j in range(len(in_channels)):
                if j > i:
W
weishengyu 已提交
284
                    self.residual_func_list.append(
W
weishengyu 已提交
285 286 287 288 289
                        ConvBNLayer(
                            num_channels=in_channels[j],
                            num_filters=out_channels[i],
                            filter_size=1,
                            stride=1,
W
weishengyu 已提交
290
                            act=None))
W
weishengyu 已提交
291 292 293 294
                elif j < i:
                    pre_num_filters = in_channels[j]
                    for k in range(i - j):
                        if k == i - j - 1:
W
weishengyu 已提交
295
                            self.residual_func_list.append(
W
weishengyu 已提交
296 297 298 299 300
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[i],
                                    filter_size=3,
                                    stride=2,
W
weishengyu 已提交
301
                                    act=None))
W
weishengyu 已提交
302 303
                            pre_num_filters = out_channels[i]
                        else:
W
weishengyu 已提交
304
                            self.residual_func_list.append(
W
weishengyu 已提交
305 306 307 308 309
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[j],
                                    filter_size=3,
                                    stride=2,
W
weishengyu 已提交
310
                                    act="relu"))
W
weishengyu 已提交
311 312
                            pre_num_filters = out_channels[j]

W
weishengyu 已提交
313 314
    def forward(self, x, res_dict=None):
        out = []
W
weishengyu 已提交
315
        residual_func_idx = 0
W
weishengyu 已提交
316
        for i in range(len(self._in_channels)):
W
weishengyu 已提交
317
            residual = x[i]
W
weishengyu 已提交
318 319
            for j in range(len(self._in_channels)):
                if j > i:
W
weishengyu 已提交
320
                    xj = self.residual_func_list[residual_func_idx](x[j])
W
weishengyu 已提交
321 322
                    residual_func_idx += 1

W
weishengyu 已提交
323
                    xj = upsample(xj, scale_factor=2**(j - i), mode="nearest")
W
weishengyu 已提交
324
                    residual = paddle.add(x=residual, y=xj)
W
weishengyu 已提交
325
                elif j < i:
W
weishengyu 已提交
326
                    xj = x[j]
W
weishengyu 已提交
327
                    for k in range(i - j):
W
weishengyu 已提交
328
                        xj = self.residual_func_list[residual_func_idx](xj)
W
weishengyu 已提交
329 330
                        residual_func_idx += 1

W
weishengyu 已提交
331
                    residual = paddle.add(x=residual, y=xj)
W
weishengyu 已提交
332

W
weishengyu 已提交
333 334
            residual = self.relu(residual)
            out.append(residual)
W
weishengyu 已提交
335

W
weishengyu 已提交
336
        return out
W
weishengyu 已提交
337 338 339 340 341 342


class LastClsOut(TheseusLayer):
    def __init__(self,
                 num_channel_list,
                 has_se,
W
weishengyu 已提交
343
                 num_filters_list=[32, 64, 128, 256]):
D
dongshuilong 已提交
344
        super().__init__()
W
weishengyu 已提交
345

W
weishengyu 已提交
346
        self.func_list = nn.LayerList()
W
weishengyu 已提交
347
        for idx in range(len(num_channel_list)):
W
weishengyu 已提交
348
            self.func_list.append(
W
weishengyu 已提交
349 350 351 352
                BottleneckBlock(
                    num_channels=num_channel_list[idx],
                    num_filters=num_filters_list[idx],
                    has_se=has_se,
W
weishengyu 已提交
353
                    downsample=True))
W
weishengyu 已提交
354

W
weishengyu 已提交
355 356 357 358 359 360
    def forward(self, x, res_dict=None):
        out = []
        for idx, xi in enumerate(x):
            xi = self.func_list[idx](xi)
            out.append(xi)
        return out
W
weishengyu 已提交
361 362 363


class HRNet(TheseusLayer):
W
dbg  
weishengyu 已提交
364
    """
365 366 367 368 369
    HRNet
    Args:
        width: int=18. Base channel number of HRNet.
        has_se: bool=False. If 'True', add se module to HRNet.
        class_num: int=1000. Output num of last fc layer.
D
dongshuilong 已提交
370 371
    Returns:
        model: nn.Layer. Specific HRNet model depends on args.
W
dbg  
weishengyu 已提交
372
    """
D
dongshuilong 已提交
373

374
    def __init__(self,
375
                 stages_pattern,
376 377 378
                 width=18,
                 has_se=False,
                 class_num=1000,
379 380
                 return_patterns=None,
                 return_stages=None):
D
dongshuilong 已提交
381
        super().__init__()
W
weishengyu 已提交
382 383 384

        self.width = width
        self.has_se = has_se
385
        self._class_num = class_num
W
weishengyu 已提交
386

W
weishengyu 已提交
387 388
        channels_2 = [self.width, self.width * 2]
        channels_3 = [self.width, self.width * 2, self.width * 4]
D
dongshuilong 已提交
389 390 391
        channels_4 = [
            self.width, self.width * 2, self.width * 4, self.width * 8
        ]
W
weishengyu 已提交
392 393 394 395 396 397

        self.conv_layer1_1 = ConvBNLayer(
            num_channels=3,
            num_filters=64,
            filter_size=3,
            stride=2,
D
dongshuilong 已提交
398
            act="relu")
W
weishengyu 已提交
399 400 401 402 403 404

        self.conv_layer1_2 = ConvBNLayer(
            num_channels=64,
            num_filters=64,
            filter_size=3,
            stride=2,
D
dongshuilong 已提交
405
            act="relu")
W
weishengyu 已提交
406

407
        self.layer1 = nn.Sequential(* [
W
weishengyu 已提交
408
            BottleneckBlock(
W
weishengyu 已提交
409 410 411 412
                num_channels=64 if i == 0 else 256,
                num_filters=64,
                has_se=has_se,
                stride=1,
D
dongshuilong 已提交
413
                downsample=True if i == 0 else False) for i in range(4)
W
weishengyu 已提交
414
        ])
W
weishengyu 已提交
415

W
weishengyu 已提交
416
        self.conv_tr1_1 = ConvBNLayer(
D
dongshuilong 已提交
417
            num_channels=256, num_filters=width, filter_size=3)
W
weishengyu 已提交
418
        self.conv_tr1_2 = ConvBNLayer(
D
dongshuilong 已提交
419
            num_channels=256, num_filters=width * 2, filter_size=3, stride=2)
W
weishengyu 已提交
420 421

        self.st2 = Stage(
D
dongshuilong 已提交
422
            num_modules=1, num_filters=channels_2, has_se=self.has_se)
W
weishengyu 已提交
423

W
weishengyu 已提交
424
        self.conv_tr2 = ConvBNLayer(
W
weishengyu 已提交
425 426
            num_channels=width * 2,
            num_filters=width * 4,
W
dbg  
weishengyu 已提交
427
            filter_size=3,
D
dongshuilong 已提交
428
            stride=2)
W
weishengyu 已提交
429
        self.st3 = Stage(
D
dongshuilong 已提交
430
            num_modules=4, num_filters=channels_3, has_se=self.has_se)
W
weishengyu 已提交
431

W
weishengyu 已提交
432
        self.conv_tr3 = ConvBNLayer(
W
weishengyu 已提交
433 434
            num_channels=width * 4,
            num_filters=width * 8,
W
dbg  
weishengyu 已提交
435
            filter_size=3,
D
dongshuilong 已提交
436
            stride=2)
W
weishengyu 已提交
437

W
weishengyu 已提交
438
        self.st4 = Stage(
D
dongshuilong 已提交
439
            num_modules=3, num_filters=channels_4, has_se=self.has_se)
W
weishengyu 已提交
440 441 442 443 444 445

        # classification
        num_filters_list = [32, 64, 128, 256]
        self.last_cls = LastClsOut(
            num_channel_list=channels_4,
            has_se=self.has_se,
W
weishengyu 已提交
446
            num_filters_list=num_filters_list)
W
weishengyu 已提交
447 448

        last_num_filters = [256, 512, 1024]
W
weishengyu 已提交
449
        self.cls_head_conv_list = nn.LayerList()
W
weishengyu 已提交
450 451
        for idx in range(3):
            self.cls_head_conv_list.append(
D
dongshuilong 已提交
452 453 454 455 456
                ConvBNLayer(
                    num_channels=num_filters_list[idx] * 4,
                    num_filters=last_num_filters[idx],
                    filter_size=3,
                    stride=2))
W
weishengyu 已提交
457 458

        self.conv_last = ConvBNLayer(
D
dongshuilong 已提交
459
            num_channels=1024, num_filters=2048, filter_size=1, stride=1)
W
weishengyu 已提交
460

W
weishengyu 已提交
461
        self.avg_pool = nn.AdaptiveAvgPool2D(1)
W
weishengyu 已提交
462 463

        stdv = 1.0 / math.sqrt(2048 * 1.0)
464
        self.flatten = nn.Flatten(start_axis=1, stop_axis=-1)
W
weishengyu 已提交
465

W
weishengyu 已提交
466
        self.fc = nn.Linear(
W
weishengyu 已提交
467
            2048,
468
            class_num,
W
weishengyu 已提交
469
            weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)))
470 471 472 473 474

        super().init_res(
            stages_pattern,
            return_patterns=return_patterns,
            return_stages=return_stages)
W
weishengyu 已提交
475

W
weishengyu 已提交
476
    def forward(self, x):
W
weishengyu 已提交
477 478
        x = self.conv_layer1_1(x)
        x = self.conv_layer1_2(x)
W
weishengyu 已提交
479

W
weishengyu 已提交
480
        x = self.layer1(x)
W
weishengyu 已提交
481

W
weishengyu 已提交
482 483 484
        tr1_1 = self.conv_tr1_1(x)
        tr1_2 = self.conv_tr1_2(x)
        x = self.st2([tr1_1, tr1_2])
W
weishengyu 已提交
485

W
weishengyu 已提交
486 487 488
        tr2 = self.conv_tr2(x[-1])
        x.append(tr2)
        x = self.st3(x)
W
weishengyu 已提交
489

W
weishengyu 已提交
490 491 492
        tr3 = self.conv_tr3(x[-1])
        x.append(tr3)
        x = self.st4(x)
W
weishengyu 已提交
493

W
weishengyu 已提交
494
        x = self.last_cls(x)
W
weishengyu 已提交
495

W
weishengyu 已提交
496
        y = x[0]
W
weishengyu 已提交
497
        for idx in range(3):
W
weishengyu 已提交
498
            y = paddle.add(x[idx + 1], self.cls_head_conv_list[idx](y))
W
weishengyu 已提交
499 500

        y = self.conv_last(y)
W
weishengyu 已提交
501
        y = self.avg_pool(y)
502
        y = self.flatten(y)
W
weishengyu 已提交
503
        y = self.fc(y)
W
weishengyu 已提交
504 505 506
        return y


D
dongshuilong 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
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 HRNet_W18_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W18_C
    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:
        model: nn.Layer. Specific `HRNet_W18_C` model depends on args.
    """
530 531
    model = HRNet(
        width=18, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
532
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W18_C"], use_ssld)
W
weishengyu 已提交
533 534 535
    return model


D
dongshuilong 已提交
536 537 538 539 540 541 542 543 544 545
def HRNet_W30_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W30_C
    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:
        model: nn.Layer. Specific `HRNet_W30_C` model depends on args.
    """
546 547
    model = HRNet(
        width=30, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
548
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W30_C"], use_ssld)
W
weishengyu 已提交
549 550 551
    return model


D
dongshuilong 已提交
552 553 554 555 556 557 558 559 560 561
def HRNet_W32_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W32_C
    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:
        model: nn.Layer. Specific `HRNet_W32_C` model depends on args.
    """
562 563
    model = HRNet(
        width=32, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
564
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W32_C"], use_ssld)
W
weishengyu 已提交
565 566 567
    return model


D
dongshuilong 已提交
568 569 570 571 572 573 574 575 576 577
def HRNet_W40_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W40_C
    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:
        model: nn.Layer. Specific `HRNet_W40_C` model depends on args.
    """
578 579
    model = HRNet(
        width=40, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
580
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W40_C"], use_ssld)
W
weishengyu 已提交
581 582 583
    return model


D
dongshuilong 已提交
584 585 586 587 588 589 590 591 592 593
def HRNet_W44_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W44_C
    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:
        model: nn.Layer. Specific `HRNet_W44_C` model depends on args.
    """
594 595
    model = HRNet(
        width=44, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
596
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W44_C"], use_ssld)
W
weishengyu 已提交
597 598 599
    return model


D
dongshuilong 已提交
600 601 602 603 604 605 606 607 608 609
def HRNet_W48_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W48_C
    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:
        model: nn.Layer. Specific `HRNet_W48_C` model depends on args.
    """
610 611
    model = HRNet(
        width=48, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
612
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W48_C"], use_ssld)
W
weishengyu 已提交
613 614 615
    return model


D
dongshuilong 已提交
616 617 618 619 620 621 622 623 624 625
def HRNet_W60_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W60_C
    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:
        model: nn.Layer. Specific `HRNet_W60_C` model depends on args.
    """
626 627
    model = HRNet(
        width=60, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
628
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W60_C"], use_ssld)
W
weishengyu 已提交
629 630 631
    return model


D
dongshuilong 已提交
632 633 634 635 636 637 638 639 640 641
def HRNet_W64_C(pretrained=False, use_ssld=False, **kwargs):
    """
    HRNet_W64_C
    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:
        model: nn.Layer. Specific `HRNet_W64_C` model depends on args.
    """
642 643
    model = HRNet(
        width=64, stages_pattern=MODEL_STAGES_PATTERN["HRNet"], **kwargs)
D
dongshuilong 已提交
644
    _load_pretrained(pretrained, model, MODEL_URLS["HRNet_W64_C"], use_ssld)
W
weishengyu 已提交
645 646 647
    return model


D
dongshuilong 已提交
648 649 650 651 652 653 654 655 656 657
def SE_HRNet_W18_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W18_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W18_C` model depends on args.
    """
658 659 660 661 662
    model = HRNet(
        width=18,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
663
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W18_C"], use_ssld)
W
weishengyu 已提交
664 665 666
    return model


D
dongshuilong 已提交
667 668 669 670 671 672 673 674 675 676
def SE_HRNet_W30_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W30_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W30_C` model depends on args.
    """
677 678 679 680 681
    model = HRNet(
        width=30,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
682
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W30_C"], use_ssld)
W
weishengyu 已提交
683 684 685
    return model


D
dongshuilong 已提交
686 687 688 689 690 691 692 693 694 695
def SE_HRNet_W32_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W32_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W32_C` model depends on args.
    """
696 697 698 699 700
    model = HRNet(
        width=32,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
701
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W32_C"], use_ssld)
W
weishengyu 已提交
702 703 704
    return model


D
dongshuilong 已提交
705 706 707 708 709 710 711 712 713 714
def SE_HRNet_W40_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W40_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W40_C` model depends on args.
    """
715 716 717 718 719
    model = HRNet(
        width=40,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
720
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W40_C"], use_ssld)
W
weishengyu 已提交
721 722 723
    return model


D
dongshuilong 已提交
724 725 726 727 728 729 730 731 732 733
def SE_HRNet_W44_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W44_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W44_C` model depends on args.
    """
734 735 736 737 738
    model = HRNet(
        width=44,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
739
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W44_C"], use_ssld)
W
weishengyu 已提交
740 741 742
    return model


D
dongshuilong 已提交
743 744 745 746 747 748 749 750 751 752
def SE_HRNet_W48_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W48_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W48_C` model depends on args.
    """
753 754 755 756 757
    model = HRNet(
        width=48,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
758
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W48_C"], use_ssld)
W
weishengyu 已提交
759 760 761
    return model


D
dongshuilong 已提交
762 763 764 765 766 767 768 769 770 771
def SE_HRNet_W60_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W60_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W60_C` model depends on args.
    """
772 773 774 775 776
    model = HRNet(
        width=60,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
777
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W60_C"], use_ssld)
W
weishengyu 已提交
778 779 780
    return model


D
dongshuilong 已提交
781 782 783 784 785 786 787 788 789 790
def SE_HRNet_W64_C(pretrained=False, use_ssld=False, **kwargs):
    """
    SE_HRNet_W64_C
    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:
        model: nn.Layer. Specific `SE_HRNet_W64_C` model depends on args.
    """
791 792 793 794 795
    model = HRNet(
        width=64,
        stages_pattern=MODEL_STAGES_PATTERN["HRNet"],
        has_se=True,
        **kwargs)
D
dongshuilong 已提交
796
    _load_pretrained(pretrained, model, MODEL_URLS["SE_HRNet_W64_C"], use_ssld)
W
weishengyu 已提交
797
    return model