hrnet.py 22.8 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

19
import numpy as np
W
WuHaobo 已提交
20
import paddle
littletomatodonkey's avatar
littletomatodonkey 已提交
21 22 23
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
littletomatodonkey's avatar
littletomatodonkey 已提交
24 25
from paddle.nn import Conv2d, BatchNorm, Linear
from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d
littletomatodonkey's avatar
littletomatodonkey 已提交
26
from paddle.nn.initializer import Uniform
27 28

import math
W
WuHaobo 已提交
29 30

__all__ = [
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    "HRNet_W18_C",
    "HRNet_W30_C",
    "HRNet_W32_C",
    "HRNet_W40_C",
    "HRNet_W44_C",
    "HRNet_W48_C",
    "HRNet_W60_C",
    "HRNet_W64_C",
    "SE_HRNet_W18_C",
    "SE_HRNet_W30_C",
    "SE_HRNet_W32_C",
    "SE_HRNet_W40_C",
    "SE_HRNet_W44_C",
    "SE_HRNet_W48_C",
    "SE_HRNet_W60_C",
    "SE_HRNet_W64_C",
W
WuHaobo 已提交
47 48 49
]


littletomatodonkey's avatar
littletomatodonkey 已提交
50
class ConvBNLayer(nn.Layer):
51 52 53 54 55 56 57 58 59
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 act="relu",
                 name=None):
        super(ConvBNLayer, self).__init__()
W
WuHaobo 已提交
60

littletomatodonkey's avatar
littletomatodonkey 已提交
61 62 63 64
        self._conv = Conv2d(
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
65 66 67
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
littletomatodonkey's avatar
littletomatodonkey 已提交
68
            weight_attr=ParamAttr(name=name + "_weights"),
69 70 71 72 73 74 75 76 77
            bias_attr=False)
        bn_name = name + '_bn'
        self._batch_norm = BatchNorm(
            num_filters,
            act=act,
            param_attr=ParamAttr(name=bn_name + '_scale'),
            bias_attr=ParamAttr(bn_name + '_offset'),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
W
WuHaobo 已提交
78

79 80 81 82 83 84
    def forward(self, input):
        y = self._conv(input)
        y = self._batch_norm(y)
        return y


littletomatodonkey's avatar
littletomatodonkey 已提交
85
class Layer1(nn.Layer):
86 87 88 89
    def __init__(self, num_channels, has_se=False, name=None):
        super(Layer1, self).__init__()

        self.bottleneck_block_list = []
W
WuHaobo 已提交
90 91

        for i in range(4):
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            bottleneck_block = self.add_sublayer(
                "bb_{}_{}".format(name, i + 1),
                BottleneckBlock(
                    num_channels=num_channels if i == 0 else 256,
                    num_filters=64,
                    has_se=has_se,
                    stride=1,
                    downsample=True if i == 0 else False,
                    name=name + '_' + str(i + 1)))
            self.bottleneck_block_list.append(bottleneck_block)

    def forward(self, input):
        conv = input
        for block_func in self.bottleneck_block_list:
            conv = block_func(conv)
W
WuHaobo 已提交
107 108
        return conv

109

littletomatodonkey's avatar
littletomatodonkey 已提交
110
class TransitionLayer(nn.Layer):
111 112 113
    def __init__(self, in_channels, out_channels, name=None):
        super(TransitionLayer, self).__init__()

W
WuHaobo 已提交
114 115 116
        num_in = len(in_channels)
        num_out = len(out_channels)
        out = []
117
        self.conv_bn_func_list = []
W
WuHaobo 已提交
118
        for i in range(num_out):
119
            residual = None
W
WuHaobo 已提交
120 121
            if i < num_in:
                if in_channels[i] != out_channels[i]:
122 123 124 125 126 127 128 129 130 131 132 133
                    residual = self.add_sublayer(
                        "transition_{}_layer_{}".format(name, i + 1),
                        ConvBNLayer(
                            num_channels=in_channels[i],
                            num_filters=out_channels[i],
                            filter_size=3,
                            name=name + '_layer_' + str(i + 1)))
            else:
                residual = self.add_sublayer(
                    "transition_{}_layer_{}".format(name, i + 1),
                    ConvBNLayer(
                        num_channels=in_channels[-1],
W
WuHaobo 已提交
134
                        num_filters=out_channels[i],
135 136 137 138 139 140 141 142 143 144
                        filter_size=3,
                        stride=2,
                        name=name + '_layer_' + str(i + 1)))
            self.conv_bn_func_list.append(residual)

    def forward(self, input):
        outs = []
        for idx, conv_bn_func in enumerate(self.conv_bn_func_list):
            if conv_bn_func is None:
                outs.append(input[idx])
W
WuHaobo 已提交
145
            else:
146 147 148 149 150
                if idx < len(input):
                    outs.append(conv_bn_func(input[idx]))
                else:
                    outs.append(conv_bn_func(input[-1]))
        return outs
W
WuHaobo 已提交
151 152


littletomatodonkey's avatar
littletomatodonkey 已提交
153
class Branches(nn.Layer):
154 155 156 157 158 159 160
    def __init__(self,
                 block_num,
                 in_channels,
                 out_channels,
                 has_se=False,
                 name=None):
        super(Branches, self).__init__()
W
WuHaobo 已提交
161

162
        self.basic_block_list = []
W
WuHaobo 已提交
163

164 165 166 167 168 169 170 171 172 173 174 175 176
        for i in range(len(out_channels)):
            self.basic_block_list.append([])
            for j in range(block_num):
                in_ch = in_channels[i] if j == 0 else out_channels[i]
                basic_block_func = self.add_sublayer(
                    "bb_{}_branch_layer_{}_{}".format(name, i + 1, j + 1),
                    BasicBlock(
                        num_channels=in_ch,
                        num_filters=out_channels[i],
                        has_se=has_se,
                        name=name + '_branch_layer_' + str(i + 1) + '_' +
                        str(j + 1)))
                self.basic_block_list[i].append(basic_block_func)
W
WuHaobo 已提交
177

178 179 180 181 182 183 184 185
    def forward(self, inputs):
        outs = []
        for idx, input in enumerate(inputs):
            conv = input
            for basic_block_func in self.basic_block_list[idx]:
                conv = basic_block_func(conv)
            outs.append(conv)
        return outs
W
WuHaobo 已提交
186 187


littletomatodonkey's avatar
littletomatodonkey 已提交
188
class BottleneckBlock(nn.Layer):
189 190 191 192 193 194 195 196
    def __init__(self,
                 num_channels,
                 num_filters,
                 has_se,
                 stride=1,
                 downsample=False,
                 name=None):
        super(BottleneckBlock, self).__init__()
W
WuHaobo 已提交
197

198 199
        self.has_se = has_se
        self.downsample = downsample
W
WuHaobo 已提交
200

201 202
        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
W
WuHaobo 已提交
203
            num_filters=num_filters,
204 205 206 207 208
            filter_size=1,
            act="relu",
            name=name + "_conv1", )
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
W
WuHaobo 已提交
209
            num_filters=num_filters,
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            filter_size=3,
            stride=stride,
            act="relu",
            name=name + "_conv2")
        self.conv3 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters * 4,
            filter_size=1,
            act=None,
            name=name + "_conv3")

        if self.downsample:
            self.conv_down = ConvBNLayer(
                num_channels=num_channels,
                num_filters=num_filters * 4,
W
WuHaobo 已提交
225
                filter_size=1,
226 227 228
                act=None,
                name=name + "_downsample")

W
WuHaobo 已提交
229
        if self.has_se:
230 231 232
            self.se = SELayer(
                num_channels=num_filters * 4,
                num_filters=num_filters * 4,
W
WuHaobo 已提交
233
                reduction_ratio=16,
234 235 236
                name='fc' + name)

    def forward(self, input):
W
WuHaobo 已提交
237
        residual = input
238 239 240 241 242 243 244 245 246 247
        conv1 = self.conv1(input)
        conv2 = self.conv2(conv1)
        conv3 = self.conv3(conv2)

        if self.downsample:
            residual = self.conv_down(input)

        if self.has_se:
            conv3 = self.se(conv3)

littletomatodonkey's avatar
littletomatodonkey 已提交
248
        y = paddle.elementwise_add(x=conv3, y=residual, act="relu")
249 250 251
        return y


littletomatodonkey's avatar
littletomatodonkey 已提交
252
class BasicBlock(nn.Layer):
253 254 255 256 257 258 259 260 261 262 263 264 265 266
    def __init__(self,
                 num_channels,
                 num_filters,
                 stride=1,
                 has_se=False,
                 downsample=False,
                 name=None):
        super(BasicBlock, self).__init__()

        self.has_se = has_se
        self.downsample = downsample

        self.conv1 = ConvBNLayer(
            num_channels=num_channels,
W
WuHaobo 已提交
267 268 269
            num_filters=num_filters,
            filter_size=3,
            stride=stride,
270 271 272 273 274 275 276 277 278 279 280 281 282
            act="relu",
            name=name + "_conv1")
        self.conv2 = ConvBNLayer(
            num_channels=num_filters,
            num_filters=num_filters,
            filter_size=3,
            stride=1,
            act=None,
            name=name + "_conv2")

        if self.downsample:
            self.conv_down = ConvBNLayer(
                num_channels=num_channels,
W
WuHaobo 已提交
283
                num_filters=num_filters * 4,
284 285 286 287
                filter_size=1,
                act="relu",
                name=name + "_downsample")

W
WuHaobo 已提交
288
        if self.has_se:
289 290 291
            self.se = SELayer(
                num_channels=num_filters,
                num_filters=num_filters,
W
WuHaobo 已提交
292
                reduction_ratio=16,
293 294 295 296 297 298 299 300 301 302 303 304 305
                name='fc' + name)

    def forward(self, input):
        residual = input
        conv1 = self.conv1(input)
        conv2 = self.conv2(conv1)

        if self.downsample:
            residual = self.conv_down(input)

        if self.has_se:
            conv2 = self.se(conv2)

littletomatodonkey's avatar
littletomatodonkey 已提交
306
        y = paddle.elementwise_add(x=conv2, y=residual, act="relu")
307 308 309
        return y


littletomatodonkey's avatar
littletomatodonkey 已提交
310
class SELayer(nn.Layer):
311 312 313
    def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
        super(SELayer, self).__init__()

littletomatodonkey's avatar
littletomatodonkey 已提交
314
        self.pool2d_gap = AdaptiveAvgPool2d(1)
315 316 317 318 319 320 321 322 323 324

        self._num_channels = num_channels

        med_ch = int(num_channels / reduction_ratio)
        stdv = 1.0 / math.sqrt(num_channels * 1.0)
        self.squeeze = Linear(
            num_channels,
            med_ch,
            act="relu",
            param_attr=ParamAttr(
littletomatodonkey's avatar
littletomatodonkey 已提交
325
                initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
W
WuHaobo 已提交
326
            bias_attr=ParamAttr(name=name + '_sqz_offset'))
327 328 329 330 331 332 333

        stdv = 1.0 / math.sqrt(med_ch * 1.0)
        self.excitation = Linear(
            med_ch,
            num_filters,
            act="sigmoid",
            param_attr=ParamAttr(
littletomatodonkey's avatar
littletomatodonkey 已提交
334
                initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
W
WuHaobo 已提交
335
            bias_attr=ParamAttr(name=name + '_exc_offset'))
336 337 338

    def forward(self, input):
        pool = self.pool2d_gap(input)
littletomatodonkey's avatar
littletomatodonkey 已提交
339
        pool = paddle.reshape(pool, shape=[-1, self._num_channels])
340 341
        squeeze = self.squeeze(pool)
        excitation = self.excitation(squeeze)
littletomatodonkey's avatar
littletomatodonkey 已提交
342
        excitation = paddle.reshape(
343 344 345 346 347
            excitation, shape=[-1, self._num_channels, 1, 1])
        out = input * excitation
        return out


littletomatodonkey's avatar
littletomatodonkey 已提交
348
class Stage(nn.Layer):
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    def __init__(self,
                 num_channels,
                 num_modules,
                 num_filters,
                 has_se=False,
                 multi_scale_output=True,
                 name=None):
        super(Stage, self).__init__()

        self._num_modules = num_modules

        self.stage_func_list = []
        for i in range(num_modules):
            if i == num_modules - 1 and not multi_scale_output:
                stage_func = self.add_sublayer(
                    "stage_{}_{}".format(name, i + 1),
                    HighResolutionModule(
                        num_channels=num_channels,
                        num_filters=num_filters,
                        has_se=has_se,
                        multi_scale_output=False,
                        name=name + '_' + str(i + 1)))
            else:
                stage_func = self.add_sublayer(
                    "stage_{}_{}".format(name, i + 1),
                    HighResolutionModule(
                        num_channels=num_channels,
                        num_filters=num_filters,
                        has_se=has_se,
                        name=name + '_' + str(i + 1)))

            self.stage_func_list.append(stage_func)

    def forward(self, input):
        out = input
        for idx in range(self._num_modules):
            out = self.stage_func_list[idx](out)
        return out


littletomatodonkey's avatar
littletomatodonkey 已提交
389
class HighResolutionModule(nn.Layer):
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    def __init__(self,
                 num_channels,
                 num_filters,
                 has_se=False,
                 multi_scale_output=True,
                 name=None):
        super(HighResolutionModule, self).__init__()

        self.branches_func = Branches(
            block_num=4,
            in_channels=num_channels,
            out_channels=num_filters,
            has_se=has_se,
            name=name)

        self.fuse_func = FuseLayers(
            in_channels=num_filters,
            out_channels=num_filters,
            multi_scale_output=multi_scale_output,
            name=name)

    def forward(self, input):
        out = self.branches_func(input)
        out = self.fuse_func(out)
        return out


littletomatodonkey's avatar
littletomatodonkey 已提交
417
class FuseLayers(nn.Layer):
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    def __init__(self,
                 in_channels,
                 out_channels,
                 multi_scale_output=True,
                 name=None):
        super(FuseLayers, self).__init__()

        self._actual_ch = len(in_channels) if multi_scale_output else 1
        self._in_channels = in_channels

        self.residual_func_list = []
        for i in range(self._actual_ch):
            for j in range(len(in_channels)):
                residual_func = None
                if j > i:
                    residual_func = self.add_sublayer(
                        "residual_{}_layer_{}_{}".format(name, i + 1, j + 1),
                        ConvBNLayer(
                            num_channels=in_channels[j],
                            num_filters=out_channels[i],
                            filter_size=1,
                            stride=1,
                            act=None,
                            name=name + '_layer_' + str(i + 1) + '_' +
                            str(j + 1)))
                    self.residual_func_list.append(residual_func)
                elif j < i:
                    pre_num_filters = in_channels[j]
                    for k in range(i - j):
                        if k == i - j - 1:
                            residual_func = self.add_sublayer(
                                "residual_{}_layer_{}_{}_{}".format(
                                    name, i + 1, j + 1, k + 1),
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[i],
                                    filter_size=3,
                                    stride=2,
                                    act=None,
                                    name=name + '_layer_' + str(i + 1) + '_' +
                                    str(j + 1) + '_' + str(k + 1)))
                            pre_num_filters = out_channels[i]
                        else:
                            residual_func = self.add_sublayer(
                                "residual_{}_layer_{}_{}_{}".format(
                                    name, i + 1, j + 1, k + 1),
                                ConvBNLayer(
                                    num_channels=pre_num_filters,
                                    num_filters=out_channels[j],
                                    filter_size=3,
                                    stride=2,
                                    act="relu",
                                    name=name + '_layer_' + str(i + 1) + '_' +
                                    str(j + 1) + '_' + str(k + 1)))
                            pre_num_filters = out_channels[j]
                        self.residual_func_list.append(residual_func)

    def forward(self, input):
        outs = []
        residual_func_idx = 0
        for i in range(self._actual_ch):
            residual = input[i]
            for j in range(len(self._in_channels)):
                if j > i:
                    y = self.residual_func_list[residual_func_idx](input[j])
                    residual_func_idx += 1

littletomatodonkey's avatar
littletomatodonkey 已提交
485 486
                    y = F.resize_nearest(input=y, scale=2**(j - i))
                    residual = paddle.elementwise_add(
487 488 489 490 491 492 493
                        x=residual, y=y, act=None)
                elif j < i:
                    y = input[j]
                    for k in range(i - j):
                        y = self.residual_func_list[residual_func_idx](y)
                        residual_func_idx += 1

littletomatodonkey's avatar
littletomatodonkey 已提交
494
                    residual = paddle.elementwise_add(
495 496
                        x=residual, y=y, act=None)

littletomatodonkey's avatar
littletomatodonkey 已提交
497
            residual = F.relu(residual)
498 499 500 501 502
            outs.append(residual)

        return outs


littletomatodonkey's avatar
littletomatodonkey 已提交
503
class LastClsOut(nn.Layer):
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    def __init__(self,
                 num_channel_list,
                 has_se,
                 num_filters_list=[32, 64, 128, 256],
                 name=None):
        super(LastClsOut, self).__init__()

        self.func_list = []
        for idx in range(len(num_channel_list)):
            func = self.add_sublayer(
                "conv_{}_conv_{}".format(name, idx + 1),
                BottleneckBlock(
                    num_channels=num_channel_list[idx],
                    num_filters=num_filters_list[idx],
                    has_se=has_se,
                    downsample=True,
                    name=name + 'conv_' + str(idx + 1)))
            self.func_list.append(func)

    def forward(self, inputs):
        outs = []
        for idx, input in enumerate(inputs):
            out = self.func_list[idx](input)
            outs.append(out)
        return outs


littletomatodonkey's avatar
littletomatodonkey 已提交
531
class HRNet(nn.Layer):
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    def __init__(self, width=18, has_se=False, class_dim=1000):
        super(HRNet, self).__init__()

        self.width = width
        self.has_se = has_se
        self.channels = {
            18: [[18, 36], [18, 36, 72], [18, 36, 72, 144]],
            30: [[30, 60], [30, 60, 120], [30, 60, 120, 240]],
            32: [[32, 64], [32, 64, 128], [32, 64, 128, 256]],
            40: [[40, 80], [40, 80, 160], [40, 80, 160, 320]],
            44: [[44, 88], [44, 88, 176], [44, 88, 176, 352]],
            48: [[48, 96], [48, 96, 192], [48, 96, 192, 384]],
            60: [[60, 120], [60, 120, 240], [60, 120, 240, 480]],
            64: [[64, 128], [64, 128, 256], [64, 128, 256, 512]]
        }
        self._class_dim = class_dim

        channels_2, channels_3, channels_4 = self.channels[width]
        num_modules_2, num_modules_3, num_modules_4 = 1, 4, 3

        self.conv_layer1_1 = ConvBNLayer(
            num_channels=3,
            num_filters=64,
            filter_size=3,
            stride=2,
            act='relu',
            name="layer1_1")

        self.conv_layer1_2 = ConvBNLayer(
            num_channels=64,
            num_filters=64,
            filter_size=3,
            stride=2,
            act='relu',
            name="layer1_2")

        self.la1 = Layer1(num_channels=64, has_se=has_se, name="layer2")

        self.tr1 = TransitionLayer(
            in_channels=[256], out_channels=channels_2, name="tr1")

        self.st2 = Stage(
            num_channels=channels_2,
            num_modules=num_modules_2,
            num_filters=channels_2,
            has_se=self.has_se,
            name="st2")

        self.tr2 = TransitionLayer(
            in_channels=channels_2, out_channels=channels_3, name="tr2")
        self.st3 = Stage(
            num_channels=channels_3,
            num_modules=num_modules_3,
            num_filters=channels_3,
            has_se=self.has_se,
            name="st3")

        self.tr3 = TransitionLayer(
            in_channels=channels_3, out_channels=channels_4, name="tr3")
        self.st4 = Stage(
            num_channels=channels_4,
            num_modules=num_modules_4,
            num_filters=channels_4,
            has_se=self.has_se,
            name="st4")

        # classification
        num_filters_list = [32, 64, 128, 256]
        self.last_cls = LastClsOut(
            num_channel_list=channels_4,
            has_se=self.has_se,
            num_filters_list=num_filters_list,
            name="cls_head", )

        last_num_filters = [256, 512, 1024]
        self.cls_head_conv_list = []
        for idx in range(3):
            self.cls_head_conv_list.append(
                self.add_sublayer(
                    "cls_head_add{}".format(idx + 1),
                    ConvBNLayer(
                        num_channels=num_filters_list[idx] * 4,
                        num_filters=last_num_filters[idx],
                        filter_size=3,
                        stride=2,
                        name="cls_head_add" + str(idx + 1))))

        self.conv_last = ConvBNLayer(
            num_channels=1024,
            num_filters=2048,
            filter_size=1,
            stride=1,
            name="cls_head_last_conv")

littletomatodonkey's avatar
littletomatodonkey 已提交
626
        self.pool2d_avg = AdaptiveAvgPool2d(1)
627 628 629 630 631 632

        stdv = 1.0 / math.sqrt(2048 * 1.0)

        self.out = Linear(
            2048,
            class_dim,
littletomatodonkey's avatar
littletomatodonkey 已提交
633 634
            weight_attr=ParamAttr(
                initializer=Uniform(-stdv, stdv), name="fc_weights"),
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
            bias_attr=ParamAttr(name="fc_offset"))

    def forward(self, input):
        conv1 = self.conv_layer1_1(input)
        conv2 = self.conv_layer1_2(conv1)

        la1 = self.la1(conv2)

        tr1 = self.tr1([la1])
        st2 = self.st2(tr1)

        tr2 = self.tr2(st2)
        st3 = self.st3(tr2)

        tr3 = self.tr3(st3)
        st4 = self.st4(tr3)

        last_cls = self.last_cls(st4)

        y = last_cls[0]
        for idx in range(3):
            y = last_cls[idx + 1] + self.cls_head_conv_list[idx](y)

        y = self.conv_last(y)
        y = self.pool2d_avg(y)
littletomatodonkey's avatar
littletomatodonkey 已提交
660
        y = paddle.reshape(y, shape=[0, -1])
661 662
        y = self.out(y)
        return y
W
WuHaobo 已提交
663 664


littletomatodonkey's avatar
littletomatodonkey 已提交
665 666
def HRNet_W18_C(**args):
    model = HRNet(width=18, **args)
W
WuHaobo 已提交
667 668 669
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
670 671
def HRNet_W30_C(**args):
    model = HRNet(width=30, **args)
W
WuHaobo 已提交
672 673 674
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
675 676
def HRNet_W32_C(**args):
    model = HRNet(width=32, **args)
W
WuHaobo 已提交
677 678 679
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
680 681
def HRNet_W40_C(**args):
    model = HRNet(width=40, **args)
W
WuHaobo 已提交
682 683 684
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
685 686
def HRNet_W44_C(**args):
    model = HRNet(width=44, **args)
W
WuHaobo 已提交
687 688 689
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
690 691
def HRNet_W48_C(**args):
    model = HRNet(width=48, **args)
W
WuHaobo 已提交
692 693 694
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
695 696
def HRNet_W60_C(**args):
    model = HRNet(width=60, **args)
W
WuHaobo 已提交
697 698 699
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
700 701
def HRNet_W64_C(**args):
    model = HRNet(width=64, **args)
W
WuHaobo 已提交
702 703 704
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
705 706
def SE_HRNet_W18_C(**args):
    model = HRNet(width=18, has_se=True, **args)
W
WuHaobo 已提交
707 708 709
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
710 711
def SE_HRNet_W30_C(**args):
    model = HRNet(width=30, has_se=True, **args)
W
WuHaobo 已提交
712 713 714
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
715 716
def SE_HRNet_W32_C(**args):
    model = HRNet(width=32, has_se=True, **args)
W
WuHaobo 已提交
717 718 719
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
720 721
def SE_HRNet_W40_C(**args):
    model = HRNet(width=40, has_se=True, **args)
W
WuHaobo 已提交
722 723 724
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
725 726
def SE_HRNet_W44_C(**args):
    model = HRNet(width=44, has_se=True, **args)
W
WuHaobo 已提交
727 728 729
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
730 731
def SE_HRNet_W48_C(**args):
    model = HRNet(width=48, has_se=True, **args)
W
WuHaobo 已提交
732 733 734
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
735 736
def SE_HRNet_W60_C(**args):
    model = HRNet(width=60, has_se=True, **args)
W
WuHaobo 已提交
737 738 739
    return model


littletomatodonkey's avatar
littletomatodonkey 已提交
740 741
def SE_HRNet_W64_C(**args):
    model = HRNet(width=64, has_se=True, **args)
W
WuHaobo 已提交
742
    return model