rec_mobilenet_v3.py 5.4 KB
Newer Older
W
WenmuZhou 已提交
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
L
LDOUBLEV 已提交
2
#
W
WenmuZhou 已提交
3 4 5
# 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
L
LDOUBLEV 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
W
WenmuZhou 已提交
9 10 11 12 13
# 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.
L
LDOUBLEV 已提交
14

W
WenmuZhou 已提交
15
from paddle import nn
L
LDOUBLEV 已提交
16

W
WenmuZhou 已提交
17
from ppocr.modeling.backbones.det_mobilenet_v3 import ResidualUnit, ConvBNLayer, make_divisible
L
LDOUBLEV 已提交
18

W
WenmuZhou 已提交
19
__all__ = ['MobileNetV3']
L
LDOUBLEV 已提交
20 21


W
WenmuZhou 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34
class MobileNetV3(nn.Layer):
    def __init__(self,
                 in_channels=3,
                 model_name='small',
                 scale=0.5,
                 large_stride=None,
                 small_stride=None,
                 **kwargs):
        super(MobileNetV3, self).__init__()
        if small_stride is None:
            small_stride = [2, 2, 2, 2]
        if large_stride is None:
            large_stride = [1, 2, 2, 2]
35 36

        assert isinstance(large_stride, list), "large_stride type must " \
W
WenmuZhou 已提交
37
                                               "be list but got {}".format(type(large_stride))
38
        assert isinstance(small_stride, list), "small_stride type must " \
W
WenmuZhou 已提交
39
                                               "be list but got {}".format(type(small_stride))
40
        assert len(large_stride) == 4, "large_stride length must be " \
W
WenmuZhou 已提交
41
                                       "4 but got {}".format(len(large_stride))
42
        assert len(small_stride) == 4, "small_stride length must be " \
W
WenmuZhou 已提交
43
                                       "4 but got {}".format(len(small_stride))
44

L
LDOUBLEV 已提交
45
        if model_name == "large":
W
WenmuZhou 已提交
46
            cfg = [
L
LDOUBLEV 已提交
47
                # k, exp, c,  se,     nl,  s,
48 49
                [3, 16, 16, False, 'relu', large_stride[0]],
                [3, 64, 24, False, 'relu', (large_stride[1], 1)],
L
LDOUBLEV 已提交
50
                [3, 72, 24, False, 'relu', 1],
51
                [5, 72, 40, True, 'relu', (large_stride[2], 1)],
L
LDOUBLEV 已提交
52 53 54 55 56 57 58 59
                [5, 120, 40, True, 'relu', 1],
                [5, 120, 40, True, 'relu', 1],
                [3, 240, 80, False, 'hard_swish', 1],
                [3, 200, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 184, 80, False, 'hard_swish', 1],
                [3, 480, 112, True, 'hard_swish', 1],
                [3, 672, 112, True, 'hard_swish', 1],
60
                [5, 672, 160, True, 'hard_swish', (large_stride[3], 1)],
L
LDOUBLEV 已提交
61 62 63
                [5, 960, 160, True, 'hard_swish', 1],
                [5, 960, 160, True, 'hard_swish', 1],
            ]
W
WenmuZhou 已提交
64
            cls_ch_squeeze = 960
L
LDOUBLEV 已提交
65
        elif model_name == "small":
W
WenmuZhou 已提交
66
            cfg = [
L
LDOUBLEV 已提交
67
                # k, exp, c,  se,     nl,  s,
68 69
                [3, 16, 16, True, 'relu', (small_stride[0], 1)],
                [3, 72, 24, False, 'relu', (small_stride[1], 1)],
L
LDOUBLEV 已提交
70
                [3, 88, 24, False, 'relu', 1],
71
                [5, 96, 40, True, 'hard_swish', (small_stride[2], 1)],
L
LDOUBLEV 已提交
72 73 74 75
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 240, 40, True, 'hard_swish', 1],
                [5, 120, 48, True, 'hard_swish', 1],
                [5, 144, 48, True, 'hard_swish', 1],
76
                [5, 288, 96, True, 'hard_swish', (small_stride[3], 1)],
L
LDOUBLEV 已提交
77 78 79
                [5, 576, 96, True, 'hard_swish', 1],
                [5, 576, 96, True, 'hard_swish', 1],
            ]
W
WenmuZhou 已提交
80
            cls_ch_squeeze = 576
L
LDOUBLEV 已提交
81 82 83 84 85
        else:
            raise NotImplementedError("mode[" + model_name +
                                      "_model] is not implemented!")

        supported_scale = [0.35, 0.5, 0.75, 1.0, 1.25]
W
WenmuZhou 已提交
86 87 88 89 90 91 92 93 94
        assert scale in supported_scale, \
            "supported scales are {} but input scale is {}".format(supported_scale, scale)

        inplanes = 16
        # conv1
        self.conv1 = ConvBNLayer(
            in_channels=in_channels,
            out_channels=make_divisible(inplanes * scale),
            kernel_size=3,
L
LDOUBLEV 已提交
95 96
            stride=2,
            padding=1,
W
WenmuZhou 已提交
97
            groups=1,
L
LDOUBLEV 已提交
98 99 100 101
            if_act=True,
            act='hard_swish',
            name='conv1')
        i = 0
W
WenmuZhou 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115
        block_list = []
        inplanes = make_divisible(inplanes * scale)
        for (k, exp, c, se, nl, s) in cfg:
            block_list.append(
                ResidualUnit(
                    in_channels=inplanes,
                    mid_channels=make_divisible(scale * exp),
                    out_channels=make_divisible(scale * c),
                    kernel_size=k,
                    stride=s,
                    use_se=se,
                    act=nl,
                    name='conv' + str(i + 2)))
            inplanes = make_divisible(scale * c)
L
LDOUBLEV 已提交
116
            i += 1
W
WenmuZhou 已提交
117
        self.blocks = nn.Sequential(*block_list)
L
LDOUBLEV 已提交
118

W
WenmuZhou 已提交
119 120 121 122
        self.conv2 = ConvBNLayer(
            in_channels=inplanes,
            out_channels=make_divisible(scale * cls_ch_squeeze),
            kernel_size=1,
L
LDOUBLEV 已提交
123 124
            stride=1,
            padding=0,
W
WenmuZhou 已提交
125
            groups=1,
L
LDOUBLEV 已提交
126 127 128 129
            if_act=True,
            act='hard_swish',
            name='conv_last')

D
dyning 已提交
130
        self.pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
W
WenmuZhou 已提交
131 132 133 134 135 136 137 138
        self.out_channels = make_divisible(scale * cls_ch_squeeze)

    def forward(self, x):
        x = self.conv1(x)
        x = self.blocks(x)
        x = self.conv2(x)
        x = self.pool(x)
        return x