inception_v4.py 15.2 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey 已提交
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/1602.07261

W
WuHaobo 已提交
17
import paddle
littletomatodonkey's avatar
littletomatodonkey 已提交
18 19 20
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
21 22
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
littletomatodonkey's avatar
littletomatodonkey 已提交
23
from paddle.nn.initializer import Uniform
24 25
import math

R
root 已提交
26
from ....utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
C
cuicheng01 已提交
27

littletomatodonkey's avatar
littletomatodonkey 已提交
28 29 30 31
MODEL_URLS = {
    "InceptionV4":
    "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/InceptionV4_pretrained.pdparams"
}
C
cuicheng01 已提交
32 33

__all__ = list(MODEL_URLS.keys())
34

littletomatodonkey's avatar
littletomatodonkey 已提交
35 36

class ConvBNLayer(nn.Layer):
37 38 39 40 41 42 43 44 45 46 47
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 padding=0,
                 groups=1,
                 act='relu',
                 name=None):
        super(ConvBNLayer, self).__init__()

48
        self._conv = Conv2D(
littletomatodonkey's avatar
littletomatodonkey 已提交
49 50 51
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
W
WuHaobo 已提交
52 53 54
            stride=stride,
            padding=padding,
            groups=groups,
littletomatodonkey's avatar
littletomatodonkey 已提交
55
            weight_attr=ParamAttr(name=name + "_weights"),
56
            bias_attr=False)
W
WuHaobo 已提交
57
        bn_name = name + "_bn"
58 59
        self._batch_norm = BatchNorm(
            num_filters,
W
WuHaobo 已提交
60 61 62 63 64 65
            act=act,
            param_attr=ParamAttr(name=bn_name + "_scale"),
            bias_attr=ParamAttr(name=bn_name + "_offset"),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')

66 67 68 69
    def forward(self, inputs):
        y = self._conv(inputs)
        y = self._batch_norm(y)
        return y
W
WuHaobo 已提交
70 71


littletomatodonkey's avatar
littletomatodonkey 已提交
72
class InceptionStem(nn.Layer):
73
    def __init__(self):
W
wqz960 已提交
74
        super(InceptionStem, self).__init__()
75 76 77 78 79
        self._conv_1 = ConvBNLayer(
            3, 32, 3, stride=2, act="relu", name="conv1_3x3_s2")
        self._conv_2 = ConvBNLayer(32, 32, 3, act="relu", name="conv2_3x3_s1")
        self._conv_3 = ConvBNLayer(
            32, 64, 3, padding=1, act="relu", name="conv3_3x3_s1")
80
        self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
81 82 83 84 85 86 87 88 89 90
        self._conv2 = ConvBNLayer(
            64, 96, 3, stride=2, act="relu", name="inception_stem1_3x3_s2")
        self._conv1_1 = ConvBNLayer(
            160, 64, 1, act="relu", name="inception_stem2_3x3_reduce")
        self._conv1_2 = ConvBNLayer(
            64, 96, 3, act="relu", name="inception_stem2_3x3")
        self._conv2_1 = ConvBNLayer(
            160, 64, 1, act="relu", name="inception_stem2_1x7_reduce")
        self._conv2_2 = ConvBNLayer(
            64,
W
WuHaobo 已提交
91 92
            64, (7, 1),
            padding=(3, 0),
93
            act="relu",
W
WuHaobo 已提交
94
            name="inception_stem2_1x7")
95 96
        self._conv2_3 = ConvBNLayer(
            64,
W
WuHaobo 已提交
97 98
            64, (1, 7),
            padding=(0, 3),
99
            act="relu",
W
WuHaobo 已提交
100
            name="inception_stem2_7x1")
101 102 103 104 105 106 107 108 109 110 111 112
        self._conv2_4 = ConvBNLayer(
            64, 96, 3, act="relu", name="inception_stem2_3x3_2")
        self._conv3 = ConvBNLayer(
            192, 192, 3, stride=2, act="relu", name="inception_stem3_3x3_s2")

    def forward(self, inputs):
        conv = self._conv_1(inputs)
        conv = self._conv_2(conv)
        conv = self._conv_3(conv)

        pool1 = self._pool(conv)
        conv2 = self._conv2(conv)
littletomatodonkey's avatar
littletomatodonkey 已提交
113
        concat = paddle.concat([pool1, conv2], axis=1)
114 115 116 117 118 119 120 121

        conv1 = self._conv1_1(concat)
        conv1 = self._conv1_2(conv1)

        conv2 = self._conv2_1(concat)
        conv2 = self._conv2_2(conv2)
        conv2 = self._conv2_3(conv2)
        conv2 = self._conv2_4(conv2)
W
WuHaobo 已提交
122

littletomatodonkey's avatar
littletomatodonkey 已提交
123
        concat = paddle.concat([conv1, conv2], axis=1)
W
WuHaobo 已提交
124

125 126
        conv1 = self._conv3(concat)
        pool1 = self._pool(concat)
W
WuHaobo 已提交
127

littletomatodonkey's avatar
littletomatodonkey 已提交
128
        concat = paddle.concat([conv1, pool1], axis=1)
W
WuHaobo 已提交
129 130 131
        return concat


littletomatodonkey's avatar
littletomatodonkey 已提交
132
class InceptionA(nn.Layer):
133 134
    def __init__(self, name):
        super(InceptionA, self).__init__()
135
        self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
136 137 138 139 140 141 142 143
        self._conv1 = ConvBNLayer(
            384, 96, 1, act="relu", name="inception_a" + name + "_1x1")
        self._conv2 = ConvBNLayer(
            384, 96, 1, act="relu", name="inception_a" + name + "_1x1_2")
        self._conv3_1 = ConvBNLayer(
            384, 64, 1, act="relu", name="inception_a" + name + "_3x3_reduce")
        self._conv3_2 = ConvBNLayer(
            64,
W
WuHaobo 已提交
144 145 146
            96,
            3,
            padding=1,
147
            act="relu",
W
WuHaobo 已提交
148
            name="inception_a" + name + "_3x3")
149 150
        self._conv4_1 = ConvBNLayer(
            384,
W
WuHaobo 已提交
151 152
            64,
            1,
153
            act="relu",
W
WuHaobo 已提交
154
            name="inception_a" + name + "_3x3_2_reduce")
155 156
        self._conv4_2 = ConvBNLayer(
            64,
W
WuHaobo 已提交
157 158 159
            96,
            3,
            padding=1,
160
            act="relu",
W
WuHaobo 已提交
161
            name="inception_a" + name + "_3x3_2")
162 163
        self._conv4_3 = ConvBNLayer(
            96,
W
WuHaobo 已提交
164 165 166
            96,
            3,
            padding=1,
167
            act="relu",
W
WuHaobo 已提交
168 169
            name="inception_a" + name + "_3x3_3")

170 171 172
    def forward(self, inputs):
        pool1 = self._pool(inputs)
        conv1 = self._conv1(pool1)
W
WuHaobo 已提交
173

174
        conv2 = self._conv2(inputs)
W
WuHaobo 已提交
175

176 177
        conv3 = self._conv3_1(inputs)
        conv3 = self._conv3_2(conv3)
W
WuHaobo 已提交
178

179 180 181
        conv4 = self._conv4_1(inputs)
        conv4 = self._conv4_2(conv4)
        conv4 = self._conv4_3(conv4)
W
WuHaobo 已提交
182

littletomatodonkey's avatar
littletomatodonkey 已提交
183
        concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
184
        return concat
W
WuHaobo 已提交
185 186


littletomatodonkey's avatar
littletomatodonkey 已提交
187
class ReductionA(nn.Layer):
188 189
    def __init__(self):
        super(ReductionA, self).__init__()
190
        self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        self._conv2 = ConvBNLayer(
            384, 384, 3, stride=2, act="relu", name="reduction_a_3x3")
        self._conv3_1 = ConvBNLayer(
            384, 192, 1, act="relu", name="reduction_a_3x3_2_reduce")
        self._conv3_2 = ConvBNLayer(
            192, 224, 3, padding=1, act="relu", name="reduction_a_3x3_2")
        self._conv3_3 = ConvBNLayer(
            224, 256, 3, stride=2, act="relu", name="reduction_a_3x3_3")

    def forward(self, inputs):
        pool1 = self._pool(inputs)
        conv2 = self._conv2(inputs)
        conv3 = self._conv3_1(inputs)
        conv3 = self._conv3_2(conv3)
        conv3 = self._conv3_3(conv3)
littletomatodonkey's avatar
littletomatodonkey 已提交
206
        concat = paddle.concat([pool1, conv2, conv3], axis=1)
W
WuHaobo 已提交
207 208 209
        return concat


littletomatodonkey's avatar
littletomatodonkey 已提交
210
class InceptionB(nn.Layer):
211 212
    def __init__(self, name=None):
        super(InceptionB, self).__init__()
213
        self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
214 215 216 217 218 219
        self._conv1 = ConvBNLayer(
            1024, 128, 1, act="relu", name="inception_b" + name + "_1x1")
        self._conv2 = ConvBNLayer(
            1024, 384, 1, act="relu", name="inception_b" + name + "_1x1_2")
        self._conv3_1 = ConvBNLayer(
            1024,
W
WuHaobo 已提交
220 221
            192,
            1,
222
            act="relu",
W
WuHaobo 已提交
223
            name="inception_b" + name + "_1x7_reduce")
224 225
        self._conv3_2 = ConvBNLayer(
            192,
W
WuHaobo 已提交
226 227
            224, (1, 7),
            padding=(0, 3),
228
            act="relu",
W
WuHaobo 已提交
229
            name="inception_b" + name + "_1x7")
230 231
        self._conv3_3 = ConvBNLayer(
            224,
W
WuHaobo 已提交
232 233
            256, (7, 1),
            padding=(3, 0),
234
            act="relu",
W
WuHaobo 已提交
235
            name="inception_b" + name + "_7x1")
236 237
        self._conv4_1 = ConvBNLayer(
            1024,
W
WuHaobo 已提交
238 239
            192,
            1,
240
            act="relu",
W
WuHaobo 已提交
241
            name="inception_b" + name + "_7x1_2_reduce")
242 243
        self._conv4_2 = ConvBNLayer(
            192,
W
WuHaobo 已提交
244 245
            192, (1, 7),
            padding=(0, 3),
246
            act="relu",
W
WuHaobo 已提交
247
            name="inception_b" + name + "_1x7_2")
248 249
        self._conv4_3 = ConvBNLayer(
            192,
W
WuHaobo 已提交
250 251
            224, (7, 1),
            padding=(3, 0),
252
            act="relu",
W
WuHaobo 已提交
253
            name="inception_b" + name + "_7x1_2")
254 255
        self._conv4_4 = ConvBNLayer(
            224,
W
WuHaobo 已提交
256 257
            224, (1, 7),
            padding=(0, 3),
258
            act="relu",
W
WuHaobo 已提交
259
            name="inception_b" + name + "_1x7_3")
260 261
        self._conv4_5 = ConvBNLayer(
            224,
W
WuHaobo 已提交
262 263
            256, (7, 1),
            padding=(3, 0),
264
            act="relu",
W
WuHaobo 已提交
265 266
            name="inception_b" + name + "_7x1_3")

267 268 269
    def forward(self, inputs):
        pool1 = self._pool(inputs)
        conv1 = self._conv1(pool1)
W
WuHaobo 已提交
270

271 272 273 274 275
        conv2 = self._conv2(inputs)

        conv3 = self._conv3_1(inputs)
        conv3 = self._conv3_2(conv3)
        conv3 = self._conv3_3(conv3)
W
WuHaobo 已提交
276

277 278 279 280 281
        conv4 = self._conv4_1(inputs)
        conv4 = self._conv4_2(conv4)
        conv4 = self._conv4_3(conv4)
        conv4 = self._conv4_4(conv4)
        conv4 = self._conv4_5(conv4)
W
WuHaobo 已提交
282

littletomatodonkey's avatar
littletomatodonkey 已提交
283
        concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
284
        return concat
W
WuHaobo 已提交
285

286

littletomatodonkey's avatar
littletomatodonkey 已提交
287
class ReductionB(nn.Layer):
288 289
    def __init__(self):
        super(ReductionB, self).__init__()
290
        self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
291 292 293 294 295 296 297 298
        self._conv2_1 = ConvBNLayer(
            1024, 192, 1, act="relu", name="reduction_b_3x3_reduce")
        self._conv2_2 = ConvBNLayer(
            192, 192, 3, stride=2, act="relu", name="reduction_b_3x3")
        self._conv3_1 = ConvBNLayer(
            1024, 256, 1, act="relu", name="reduction_b_1x7_reduce")
        self._conv3_2 = ConvBNLayer(
            256,
W
WuHaobo 已提交
299 300
            256, (1, 7),
            padding=(0, 3),
301
            act="relu",
W
WuHaobo 已提交
302
            name="reduction_b_1x7")
303 304
        self._conv3_3 = ConvBNLayer(
            256,
W
WuHaobo 已提交
305 306
            320, (7, 1),
            padding=(3, 0),
307
            act="relu",
W
WuHaobo 已提交
308
            name="reduction_b_7x1")
309 310 311 312 313 314 315 316 317 318 319 320 321
        self._conv3_4 = ConvBNLayer(
            320, 320, 3, stride=2, act="relu", name="reduction_b_3x3_2")

    def forward(self, inputs):
        pool1 = self._pool(inputs)

        conv2 = self._conv2_1(inputs)
        conv2 = self._conv2_2(conv2)

        conv3 = self._conv3_1(inputs)
        conv3 = self._conv3_2(conv3)
        conv3 = self._conv3_3(conv3)
        conv3 = self._conv3_4(conv3)
W
WuHaobo 已提交
322

littletomatodonkey's avatar
littletomatodonkey 已提交
323
        concat = paddle.concat([pool1, conv2, conv3], axis=1)
W
WuHaobo 已提交
324 325 326 327

        return concat


littletomatodonkey's avatar
littletomatodonkey 已提交
328
class InceptionC(nn.Layer):
329 330
    def __init__(self, name=None):
        super(InceptionC, self).__init__()
331
        self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
332 333 334 335 336 337 338 339
        self._conv1 = ConvBNLayer(
            1536, 256, 1, act="relu", name="inception_c" + name + "_1x1")
        self._conv2 = ConvBNLayer(
            1536, 256, 1, act="relu", name="inception_c" + name + "_1x1_2")
        self._conv3_0 = ConvBNLayer(
            1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_3")
        self._conv3_1 = ConvBNLayer(
            384,
W
WuHaobo 已提交
340 341
            256, (1, 3),
            padding=(0, 1),
342
            act="relu",
W
WuHaobo 已提交
343
            name="inception_c" + name + "_1x3")
344 345
        self._conv3_2 = ConvBNLayer(
            384,
W
WuHaobo 已提交
346 347
            256, (3, 1),
            padding=(1, 0),
348
            act="relu",
W
WuHaobo 已提交
349
            name="inception_c" + name + "_3x1")
350 351 352 353
        self._conv4_0 = ConvBNLayer(
            1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_4")
        self._conv4_00 = ConvBNLayer(
            384,
W
WuHaobo 已提交
354 355
            448, (1, 3),
            padding=(0, 1),
356
            act="relu",
W
WuHaobo 已提交
357
            name="inception_c" + name + "_1x3_2")
358 359
        self._conv4_000 = ConvBNLayer(
            448,
W
WuHaobo 已提交
360 361
            512, (3, 1),
            padding=(1, 0),
362
            act="relu",
W
WuHaobo 已提交
363
            name="inception_c" + name + "_3x1_2")
364 365
        self._conv4_1 = ConvBNLayer(
            512,
W
WuHaobo 已提交
366 367
            256, (1, 3),
            padding=(0, 1),
368
            act="relu",
W
WuHaobo 已提交
369
            name="inception_c" + name + "_1x3_3")
370 371
        self._conv4_2 = ConvBNLayer(
            512,
W
WuHaobo 已提交
372 373
            256, (3, 1),
            padding=(1, 0),
374
            act="relu",
W
WuHaobo 已提交
375 376
            name="inception_c" + name + "_3x1_3")

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    def forward(self, inputs):
        pool1 = self._pool(inputs)
        conv1 = self._conv1(pool1)

        conv2 = self._conv2(inputs)

        conv3 = self._conv3_0(inputs)
        conv3_1 = self._conv3_1(conv3)
        conv3_2 = self._conv3_2(conv3)

        conv4 = self._conv4_0(inputs)
        conv4 = self._conv4_00(conv4)
        conv4 = self._conv4_000(conv4)
        conv4_1 = self._conv4_1(conv4)
        conv4_2 = self._conv4_2(conv4)

littletomatodonkey's avatar
littletomatodonkey 已提交
393
        concat = paddle.concat(
W
WuHaobo 已提交
394 395 396
            [conv1, conv2, conv3_1, conv3_2, conv4_1, conv4_2], axis=1)

        return concat
397 398


littletomatodonkey's avatar
littletomatodonkey 已提交
399
class InceptionV4DY(nn.Layer):
littletomatodonkey's avatar
littletomatodonkey 已提交
400
    def __init__(self, class_num=1000):
W
fix  
wqz960 已提交
401
        super(InceptionV4DY, self).__init__()
W
wqz960 已提交
402
        self._inception_stem = InceptionStem()
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

        self._inceptionA_1 = InceptionA(name="1")
        self._inceptionA_2 = InceptionA(name="2")
        self._inceptionA_3 = InceptionA(name="3")
        self._inceptionA_4 = InceptionA(name="4")
        self._reductionA = ReductionA()

        self._inceptionB_1 = InceptionB(name="1")
        self._inceptionB_2 = InceptionB(name="2")
        self._inceptionB_3 = InceptionB(name="3")
        self._inceptionB_4 = InceptionB(name="4")
        self._inceptionB_5 = InceptionB(name="5")
        self._inceptionB_6 = InceptionB(name="6")
        self._inceptionB_7 = InceptionB(name="7")
        self._reductionB = ReductionB()

        self._inceptionC_1 = InceptionC(name="1")
        self._inceptionC_2 = InceptionC(name="2")
        self._inceptionC_3 = InceptionC(name="3")

423
        self.avg_pool = AdaptiveAvgPool2D(1)
littletomatodonkey's avatar
littletomatodonkey 已提交
424
        self._drop = Dropout(p=0.2, mode="downscale_in_infer")
425 426 427
        stdv = 1.0 / math.sqrt(1536 * 1.0)
        self.out = Linear(
            1536,
littletomatodonkey's avatar
littletomatodonkey 已提交
428
            class_num,
littletomatodonkey's avatar
littletomatodonkey 已提交
429 430
            weight_attr=ParamAttr(
                initializer=Uniform(-stdv, stdv), name="final_fc_weights"),
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
            bias_attr=ParamAttr(name="final_fc_offset"))

    def forward(self, inputs):
        x = self._inception_stem(inputs)

        x = self._inceptionA_1(x)
        x = self._inceptionA_2(x)
        x = self._inceptionA_3(x)
        x = self._inceptionA_4(x)
        x = self._reductionA(x)

        x = self._inceptionB_1(x)
        x = self._inceptionB_2(x)
        x = self._inceptionB_3(x)
        x = self._inceptionB_4(x)
        x = self._inceptionB_5(x)
        x = self._inceptionB_6(x)
        x = self._inceptionB_7(x)
        x = self._reductionB(x)

        x = self._inceptionC_1(x)
        x = self._inceptionC_2(x)
        x = self._inceptionC_3(x)

        x = self.avg_pool(x)
littletomatodonkey's avatar
littletomatodonkey 已提交
456
        x = paddle.squeeze(x, axis=[2, 3])
457 458 459 460 461
        x = self._drop(x)
        x = self.out(x)
        return x


C
cuicheng01 已提交
462 463 464 465 466 467 468 469 470 471 472 473
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."
        )

littletomatodonkey's avatar
littletomatodonkey 已提交
474

C
cuicheng01 已提交
475 476
def InceptionV4(pretrained=False, use_ssld=False, **kwargs):
    model = InceptionV4DY(**kwargs)
littletomatodonkey's avatar
littletomatodonkey 已提交
477 478
    _load_pretrained(
        pretrained, model, MODEL_URLS["InceptionV4"], use_ssld=use_ssld)
littletomatodonkey's avatar
littletomatodonkey 已提交
479
    return model