resnext101_wsl.py 15.4 KB
Newer Older
W
WuHaobo 已提交
1
import paddle
littletomatodonkey's avatar
littletomatodonkey 已提交
2 3 4
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
5 6
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
littletomatodonkey's avatar
littletomatodonkey 已提交
7 8
from paddle.nn.initializer import Uniform

C
cuicheng01 已提交
9 10 11
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url

MODEL_URLS = {
littletomatodonkey's avatar
littletomatodonkey 已提交
12 13 14
    "ResNeXt101_32x8d_wsl":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x8d_wsl_pretrained.pdparams",
    "ResNeXt101_32x16d_wsl":
C
cuicheng01 已提交
15
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x16_wsl_pretrained.pdparams",
littletomatodonkey's avatar
littletomatodonkey 已提交
16 17 18 19 20
    "ResNeXt101_32x32d_wsl":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x32d_wsl_pretrained.pdparams",
    "ResNeXt101_32x48d_wsl":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x48d_wsl_pretrained.pdparams",
}
C
cuicheng01 已提交
21 22 23

__all__ = list(MODEL_URLS.keys())

littletomatodonkey's avatar
littletomatodonkey 已提交
24 25 26 27 28 29 30 31 32 33

class ConvBNLayer(nn.Layer):
    def __init__(self,
                 input_channels,
                 output_channels,
                 filter_size,
                 stride=1,
                 groups=1,
                 act=None,
                 name=None):
34
        super(ConvBNLayer, self).__init__()
W
WuHaobo 已提交
35
        if "downsample" in name:
36
            conv_name = name + ".0"
W
WuHaobo 已提交
37
        else:
littletomatodonkey's avatar
littletomatodonkey 已提交
38
            conv_name = name
39
        self._conv = Conv2D(
littletomatodonkey's avatar
littletomatodonkey 已提交
40 41 42 43 44 45 46 47
            in_channels=input_channels,
            out_channels=output_channels,
            kernel_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            weight_attr=ParamAttr(name=conv_name + ".weight"),
            bias_attr=False)
W
WuHaobo 已提交
48
        if "downsample" in name:
49
            bn_name = name[:9] + "downsample.1"
W
WuHaobo 已提交
50 51
        else:
            if "conv1" == name:
52
                bn_name = "bn" + name[-1]
W
WuHaobo 已提交
53
            else:
littletomatodonkey's avatar
littletomatodonkey 已提交
54 55 56 57 58 59 60 61 62
                bn_name = (name[:10] if name[7:9].isdigit() else name[:9]
                           ) + "bn" + name[-1]
        self._bn = BatchNorm(
            num_channels=output_channels,
            act=act,
            param_attr=ParamAttr(name=bn_name + ".weight"),
            bias_attr=ParamAttr(name=bn_name + ".bias"),
            moving_mean_name=bn_name + ".running_mean",
            moving_variance_name=bn_name + ".running_var")
63 64 65 66 67 68

    def forward(self, inputs):
        x = self._conv(inputs)
        x = self._bn(x)
        return x

littletomatodonkey's avatar
littletomatodonkey 已提交
69 70

class ShortCut(nn.Layer):
71
    def __init__(self, input_channels, output_channels, stride, name=None):
W
wqz960 已提交
72
        super(ShortCut, self).__init__()
73 74 75 76

        self.input_channels = input_channels
        self.output_channels = output_channels
        self.stride = stride
littletomatodonkey's avatar
littletomatodonkey 已提交
77
        if input_channels != output_channels or stride != 1:
78
            self._conv = ConvBNLayer(
littletomatodonkey's avatar
littletomatodonkey 已提交
79 80 81 82 83
                input_channels,
                output_channels,
                filter_size=1,
                stride=stride,
                name=name)
84 85

    def forward(self, inputs):
littletomatodonkey's avatar
littletomatodonkey 已提交
86
        if self.input_channels != self.output_channels or self.stride != 1:
87
            return self._conv(inputs)
littletomatodonkey's avatar
littletomatodonkey 已提交
88
        return inputs
89

littletomatodonkey's avatar
littletomatodonkey 已提交
90 91 92 93

class BottleneckBlock(nn.Layer):
    def __init__(self, input_channels, output_channels, stride, cardinality,
                 width, name):
W
wqz960 已提交
94
        super(BottleneckBlock, self).__init__()
95 96

        self._conv0 = ConvBNLayer(
littletomatodonkey's avatar
littletomatodonkey 已提交
97 98 99 100 101
            input_channels,
            output_channels,
            filter_size=1,
            act="relu",
            name=name + ".conv1")
102
        self._conv1 = ConvBNLayer(
littletomatodonkey's avatar
littletomatodonkey 已提交
103 104 105 106 107 108 109
            output_channels,
            output_channels,
            filter_size=3,
            act="relu",
            stride=stride,
            groups=cardinality,
            name=name + ".conv2")
110
        self._conv2 = ConvBNLayer(
littletomatodonkey's avatar
littletomatodonkey 已提交
111 112 113 114 115
            output_channels,
            output_channels // (width // 8),
            filter_size=1,
            act=None,
            name=name + ".conv3")
W
wqz960 已提交
116
        self._short = ShortCut(
littletomatodonkey's avatar
littletomatodonkey 已提交
117 118 119 120
            input_channels,
            output_channels // (width // 8),
            stride=stride,
            name=name + ".downsample")
121 122 123 124 125 126

    def forward(self, inputs):
        x = self._conv0(inputs)
        x = self._conv1(x)
        x = self._conv2(x)
        y = self._short(inputs)
127 128 129
        y = paddle.add(x, y)
        y = F.relu(y)
        return y
littletomatodonkey's avatar
littletomatodonkey 已提交
130

131

littletomatodonkey's avatar
littletomatodonkey 已提交
132
class ResNeXt101WSL(nn.Layer):
littletomatodonkey's avatar
littletomatodonkey 已提交
133
    def __init__(self, layers=101, cardinality=32, width=48, class_num=1000):
W
fix  
wqz960 已提交
134
        super(ResNeXt101WSL, self).__init__()
135

littletomatodonkey's avatar
littletomatodonkey 已提交
136
        self.class_num = class_num
137 138 139 140

        self.layers = layers
        self.cardinality = cardinality
        self.width = width
littletomatodonkey's avatar
littletomatodonkey 已提交
141
        self.scale = width // 8
142 143 144

        self.depth = [3, 4, 23, 3]
        self.base_width = cardinality * width
littletomatodonkey's avatar
littletomatodonkey 已提交
145 146
        num_filters = [self.base_width * i
                       for i in [1, 2, 4, 8]]  # [256, 512, 1024, 2048]
147 148
        self._conv_stem = ConvBNLayer(
            3, 64, 7, stride=2, act="relu", name="conv1")
149
        self._pool = MaxPool2D(kernel_size=3, stride=2, padding=1)
150

W
wqz960 已提交
151
        self._conv1_0 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
152 153 154 155 156 157
            64,
            num_filters[0],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer1.0")
W
wqz960 已提交
158
        self._conv1_1 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
159 160 161 162 163 164
            num_filters[0] // (width // 8),
            num_filters[0],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer1.1")
W
wqz960 已提交
165
        self._conv1_2 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
166 167 168 169 170 171
            num_filters[0] // (width // 8),
            num_filters[0],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer1.2")
172

W
wqz960 已提交
173
        self._conv2_0 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
174 175 176 177 178 179
            num_filters[0] // (width // 8),
            num_filters[1],
            stride=2,
            cardinality=self.cardinality,
            width=self.width,
            name="layer2.0")
W
wqz960 已提交
180
        self._conv2_1 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
181 182 183 184 185 186
            num_filters[1] // (width // 8),
            num_filters[1],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer2.1")
W
wqz960 已提交
187
        self._conv2_2 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
188 189 190 191 192 193
            num_filters[1] // (width // 8),
            num_filters[1],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer2.2")
W
wqz960 已提交
194
        self._conv2_3 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
195 196 197 198 199 200
            num_filters[1] // (width // 8),
            num_filters[1],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer2.3")
201

W
wqz960 已提交
202
        self._conv3_0 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
203 204 205 206 207 208
            num_filters[1] // (width // 8),
            num_filters[2],
            stride=2,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.0")
W
wqz960 已提交
209
        self._conv3_1 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
210 211 212 213 214 215
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.1")
W
wqz960 已提交
216
        self._conv3_2 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
217 218 219 220 221 222
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.2")
W
wqz960 已提交
223
        self._conv3_3 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
224 225 226 227 228 229
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.3")
W
wqz960 已提交
230
        self._conv3_4 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
231 232 233 234 235 236
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.4")
W
wqz960 已提交
237
        self._conv3_5 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
238 239 240 241 242 243
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.5")
W
wqz960 已提交
244
        self._conv3_6 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
245 246 247 248 249 250
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.6")
W
wqz960 已提交
251
        self._conv3_7 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
252 253 254 255 256 257
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.7")
W
wqz960 已提交
258
        self._conv3_8 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
259 260 261 262 263 264
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.8")
W
wqz960 已提交
265
        self._conv3_9 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
266 267 268 269 270 271
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.9")
W
wqz960 已提交
272
        self._conv3_10 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
273 274 275 276 277 278
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.10")
W
wqz960 已提交
279
        self._conv3_11 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
280 281 282 283 284 285
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.11")
W
wqz960 已提交
286
        self._conv3_12 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
287 288 289 290 291 292
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.12")
W
wqz960 已提交
293
        self._conv3_13 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
294 295 296 297 298 299
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.13")
W
wqz960 已提交
300
        self._conv3_14 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
301 302 303 304 305 306
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.14")
W
wqz960 已提交
307
        self._conv3_15 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
308 309 310 311 312 313
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.15")
W
wqz960 已提交
314
        self._conv3_16 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
315 316 317 318 319 320
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.16")
W
wqz960 已提交
321
        self._conv3_17 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
322 323 324 325 326 327
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.17")
W
wqz960 已提交
328
        self._conv3_18 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
329 330 331 332 333 334
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.18")
W
wqz960 已提交
335
        self._conv3_19 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
336 337 338 339 340 341
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.19")
W
wqz960 已提交
342
        self._conv3_20 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
343 344 345 346 347 348
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.20")
W
wqz960 已提交
349
        self._conv3_21 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
350 351 352 353 354 355
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.21")
W
wqz960 已提交
356
        self._conv3_22 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
357 358 359 360 361 362
            num_filters[2] // (width // 8),
            num_filters[2],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer3.22")
363

W
wqz960 已提交
364
        self._conv4_0 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
365 366 367 368 369 370
            num_filters[2] // (width // 8),
            num_filters[3],
            stride=2,
            cardinality=self.cardinality,
            width=self.width,
            name="layer4.0")
W
wqz960 已提交
371
        self._conv4_1 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
372 373 374 375 376 377
            num_filters[3] // (width // 8),
            num_filters[3],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer4.1")
W
wqz960 已提交
378
        self._conv4_2 = BottleneckBlock(
littletomatodonkey's avatar
littletomatodonkey 已提交
379 380 381 382 383 384
            num_filters[3] // (width // 8),
            num_filters[3],
            stride=1,
            cardinality=self.cardinality,
            width=self.width,
            name="layer4.2")
385

386
        self._avg_pool = AdaptiveAvgPool2D(1)
littletomatodonkey's avatar
littletomatodonkey 已提交
387 388
        self._out = Linear(
            num_filters[3] // (width // 8),
littletomatodonkey's avatar
littletomatodonkey 已提交
389
            class_num,
littletomatodonkey's avatar
littletomatodonkey 已提交
390 391
            weight_attr=ParamAttr(name="fc.weight"),
            bias_attr=ParamAttr(name="fc.bias"))
392 393 394 395 396 397 398 399

    def forward(self, inputs):
        x = self._conv_stem(inputs)
        x = self._pool(x)

        x = self._conv1_0(x)
        x = self._conv1_1(x)
        x = self._conv1_2(x)
W
WuHaobo 已提交
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        x = self._conv2_0(x)
        x = self._conv2_1(x)
        x = self._conv2_2(x)
        x = self._conv2_3(x)

        x = self._conv3_0(x)
        x = self._conv3_1(x)
        x = self._conv3_2(x)
        x = self._conv3_3(x)
        x = self._conv3_4(x)
        x = self._conv3_5(x)
        x = self._conv3_6(x)
        x = self._conv3_7(x)
        x = self._conv3_8(x)
        x = self._conv3_9(x)
        x = self._conv3_10(x)
        x = self._conv3_11(x)
        x = self._conv3_12(x)
        x = self._conv3_13(x)
        x = self._conv3_14(x)
        x = self._conv3_15(x)
        x = self._conv3_16(x)
        x = self._conv3_17(x)
        x = self._conv3_18(x)
        x = self._conv3_19(x)
        x = self._conv3_20(x)
        x = self._conv3_21(x)
        x = self._conv3_22(x)

        x = self._conv4_0(x)
        x = self._conv4_1(x)
        x = self._conv4_2(x)

        x = self._avg_pool(x)
littletomatodonkey's avatar
littletomatodonkey 已提交
435
        x = paddle.squeeze(x, axis=[2, 3])
436 437
        x = self._out(x)
        return x
W
WuHaobo 已提交
438

littletomatodonkey's avatar
littletomatodonkey 已提交
439

C
cuicheng01 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
def _load_pretrained(pretrained, model, model_url, use_ssld=False):
    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 ResNeXt101_32x8d_wsl(pretrained=False, use_ssld=False, **kwargs):
    model = ResNeXt101WSL(cardinality=32, width=8, **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
455 456 457 458 459
    _load_pretrained(
        pretrained,
        model,
        MODEL_URLS["ResNeXt101_32x8d_wsl"],
        use_ssld=use_ssld)
littletomatodonkey's avatar
littletomatodonkey 已提交
460 461
    return model

W
WuHaobo 已提交
462

C
cuicheng01 已提交
463
def ResNeXt101_32x16d_wsl(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
464
    model = ResNeXt101WSL(cardinality=32, width=16, **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
465 466 467
    _load_pretrained(
        pretrained,
        model,
C
cuicheng01 已提交
468
        MODEL_URLS["ResNeXt101_32x16d_wsl"],
littletomatodonkey's avatar
littletomatodonkey 已提交
469
        use_ssld=use_ssld)
littletomatodonkey's avatar
littletomatodonkey 已提交
470 471
    return model

W
WuHaobo 已提交
472

C
cuicheng01 已提交
473
def ResNeXt101_32x32d_wsl(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
474
    model = ResNeXt101WSL(cardinality=32, width=32, **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
475 476 477 478 479
    _load_pretrained(
        pretrained,
        model,
        MODEL_URLS["ResNeXt101_32x32d_wsl"],
        use_ssld=use_ssld)
littletomatodonkey's avatar
littletomatodonkey 已提交
480 481
    return model

W
WuHaobo 已提交
482

C
cuicheng01 已提交
483
def ResNeXt101_32x48d_wsl(pretrained=False, use_ssld=False, **kwargs):
C
cuicheng01 已提交
484
    model = ResNeXt101WSL(cardinality=32, width=48, **kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
485 486 487 488 489
    _load_pretrained(
        pretrained,
        model,
        MODEL_URLS["ResNeXt101_32x48d_wsl"],
        use_ssld=use_ssld)
littletomatodonkey's avatar
littletomatodonkey 已提交
490
    return model