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
class MobileNetV3(nn.Layer):
    def __init__(self,
                 in_channels=3,
                 model_name='small',
                 scale=0.5,
                 large_stride=None,
                 small_stride=None,
文幕地方's avatar
文幕地方 已提交
29
                 disable_se=False,
W
WenmuZhou 已提交
30 31
                 **kwargs):
        super(MobileNetV3, self).__init__()
文幕地方's avatar
文幕地方 已提交
32
        self.disable_se = disable_se
W
WenmuZhou 已提交
33 34 35 36
        if small_stride is None:
            small_stride = [2, 2, 2, 2]
        if large_stride is None:
            large_stride = [1, 2, 2, 2]
37 38

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

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

        supported_scale = [0.35, 0.5, 0.75, 1.0, 1.25]
W
WenmuZhou 已提交
88 89 90 91 92 93 94 95 96
        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 已提交
97 98
            stride=2,
            padding=1,
W
WenmuZhou 已提交
99
            groups=1,
L
LDOUBLEV 已提交
100
            if_act=True,
littletomatodonkey's avatar
littletomatodonkey 已提交
101
            act='hardswish')
L
LDOUBLEV 已提交
102
        i = 0
W
WenmuZhou 已提交
103 104 105
        block_list = []
        inplanes = make_divisible(inplanes * scale)
        for (k, exp, c, se, nl, s) in cfg:
文幕地方's avatar
文幕地方 已提交
106
            se = se and not self.disable_se
W
WenmuZhou 已提交
107 108 109 110 111 112 113 114
            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,
littletomatodonkey's avatar
littletomatodonkey 已提交
115
                    act=nl))
W
WenmuZhou 已提交
116
            inplanes = make_divisible(scale * c)
L
LDOUBLEV 已提交
117
            i += 1
W
WenmuZhou 已提交
118
        self.blocks = nn.Sequential(*block_list)
L
LDOUBLEV 已提交
119

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

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