mobilenetv2.py 12.3 KB
Newer Older
C
ceci3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# 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

import numpy as np
C
ceci3 已提交
20 21
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
22
from .search_space_base import SearchSpaceBase
C
ceci3 已提交
23
from .base_layer import conv_bn_layer
24
from .search_space_registry import SEARCHSPACE
C
update  
ceci3 已提交
25
from .utils import check_points
C
ceci3 已提交
26

27 28
__all__ = ["MobileNetV2Space"]

29 30

@SEARCHSPACE.register
C
ceci3 已提交
31
class MobileNetV2Space(SearchSpaceBase):
C
update  
ceci3 已提交
32
    def __init__(self, input_size, output_size, block_num, block_mask=None):
33
        super(MobileNetV2Space, self).__init__(input_size, output_size,
C
ceci3 已提交
34 35
                                               block_num, block_mask)
        # self.head_num means the first convolution channel
36
        self.head_num = np.array([3, 4, 8, 12, 16, 24, 32])  #7
C
ceci3 已提交
37
        # self.filter_num1 ~ self.filter_num6 means following convlution channel
38 39 40 41 42 43 44 45 46
        self.filter_num1 = np.array([3, 4, 8, 12, 16, 24, 32, 48])  #8
        self.filter_num2 = np.array([8, 12, 16, 24, 32, 48, 64, 80])  #8
        self.filter_num3 = np.array([16, 24, 32, 48, 64, 80, 96, 128])  #8
        self.filter_num4 = np.array(
            [24, 32, 48, 64, 80, 96, 128, 144, 160, 192])  #10
        self.filter_num5 = np.array(
            [32, 48, 64, 80, 96, 128, 144, 160, 192, 224])  #10
        self.filter_num6 = np.array(
            [64, 80, 96, 128, 144, 160, 192, 224, 256, 320, 384, 512])  #12
C
ceci3 已提交
47
        # self.k_size means kernel size
48
        self.k_size = np.array([3, 5])  #2
C
ceci3 已提交
49
        # self.multiply means expansion_factor of each _inverted_residual_unit
50
        self.multiply = np.array([1, 2, 3, 4, 6])  #5
C
ceci3 已提交
51
        # self.repeat means repeat_num _inverted_residual_unit in each _invresi_blocks 
52
        self.repeat = np.array([1, 2, 3, 4, 5, 6])  #6
C
ceci3 已提交
53 54 55

    def init_tokens(self):
        """
C
ceci3 已提交
56
        The initial token.
C
ceci3 已提交
57 58
        The first one is the index of the first layers' channel in self.head_num,
        each line in the following represent the index of the [expansion_factor, filter_num, repeat_num, kernel_size]
C
ceci3 已提交
59 60
        """
        # original MobileNetV2
W
wanghaoshuang 已提交
61
        # yapf: disable
C
ceci3 已提交
62
        init_token_base =  [4,          # 1, 16, 1
W
wanghaoshuang 已提交
63 64 65 66 67 68 69
                4, 5, 1, 0, # 6, 24, 2
                4, 4, 2, 0, # 6, 32, 3
                4, 4, 3, 0, # 6, 64, 4
                4, 5, 2, 0, # 6, 96, 3
                4, 7, 2, 0, # 6, 160, 3
                4, 9, 0, 0] # 6, 320, 1
        # yapf: enable
C
ceci3 已提交
70

C
ceci3 已提交
71
        return init_token_base
C
ceci3 已提交
72 73 74

    def range_table(self):
        """
C
ceci3 已提交
75
        Get range table of current search space, constrains the range of tokens. 
C
ceci3 已提交
76
        """
C
ceci3 已提交
77
        # head_num + 6 * [multiple(expansion_factor), filter_num, repeat, kernel_size]
W
wanghaoshuang 已提交
78
        # yapf: disable
C
ceci3 已提交
79 80 81 82 83 84 85
        range_table_base =  [len(self.head_num),
                len(self.multiply), len(self.filter_num1), len(self.repeat), len(self.k_size),
                len(self.multiply), len(self.filter_num2), len(self.repeat), len(self.k_size),
                len(self.multiply), len(self.filter_num3), len(self.repeat), len(self.k_size),
                len(self.multiply), len(self.filter_num4), len(self.repeat), len(self.k_size),
                len(self.multiply), len(self.filter_num5), len(self.repeat), len(self.k_size),
                len(self.multiply), len(self.filter_num6), len(self.repeat), len(self.k_size)]
W
wanghaoshuang 已提交
86
        # yapf: enable
C
ceci3 已提交
87
        return range_table_base
C
ceci3 已提交
88 89 90

    def token2arch(self, tokens=None):
        """
C
ceci3 已提交
91
        return net_arch function
C
ceci3 已提交
92
        """
C
ceci3 已提交
93

C
ceci3 已提交
94 95
        if tokens is None:
            tokens = self.init_tokens()
C
ceci3 已提交
96

C
ceci3 已提交
97
        self.bottleneck_params_list = []
C
ceci3 已提交
98 99 100 101 102 103
        self.bottleneck_params_list.append(
            (1, self.head_num[tokens[0]], 1, 1, 3))
        self.bottleneck_params_list.append(
            (self.multiply[tokens[1]], self.filter_num1[tokens[2]],
             self.repeat[tokens[3]], 2, self.k_size[tokens[4]]))
        self.bottleneck_params_list.append(
C
ceci3 已提交
104
            (self.multiply[tokens[5]], self.filter_num2[tokens[6]],
C
ceci3 已提交
105 106
             self.repeat[tokens[7]], 2, self.k_size[tokens[8]]))
        self.bottleneck_params_list.append(
C
ceci3 已提交
107
            (self.multiply[tokens[9]], self.filter_num3[tokens[10]],
C
ceci3 已提交
108 109
             self.repeat[tokens[11]], 2, self.k_size[tokens[12]]))
        self.bottleneck_params_list.append(
C
ceci3 已提交
110 111
            (self.multiply[tokens[13]], self.filter_num4[tokens[14]],
             self.repeat[tokens[15]], 1, self.k_size[tokens[16]]))
C
ceci3 已提交
112
        self.bottleneck_params_list.append(
C
ceci3 已提交
113 114
            (self.multiply[tokens[17]], self.filter_num5[tokens[18]],
             self.repeat[tokens[19]], 2, self.k_size[tokens[20]]))
C
ceci3 已提交
115
        self.bottleneck_params_list.append(
C
ceci3 已提交
116 117
            (self.multiply[tokens[21]], self.filter_num6[tokens[22]],
             self.repeat[tokens[23]], 1, self.k_size[tokens[24]]))
W
wanghaoshuang 已提交
118

C
ceci3 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        def _modify_bottle_params(output_stride=None):
            if output_stride is not None and output_stride % 2 != 0:
                raise Exception("output stride must to be even number")
            if output_stride is None:
                return
            else:
                stride = 2
                for i, layer_setting in enumerate(self.bottleneck_params_list):
                    t, c, n, s, ks = layer_setting
                    stride = stride * s
                    if stride > output_stride:
                        s = 1
                    self.bottleneck_params_list[i] = (t, c, n, s, ks)

        def net_arch(input,
C
update  
ceci3 已提交
134
                     scale=1.0,
C
update  
ceci3 已提交
135
                     return_block=None,
C
ceci3 已提交
136 137
                     end_points=None,
                     output_stride=None):
C
ceci3 已提交
138
            self.scale = scale
C
ceci3 已提交
139 140
            _modify_bottle_params(output_stride)

C
ceci3 已提交
141 142 143 144 145 146 147 148 149 150 151
            decode_ends = dict()

            def check_points(count, points):
                if points is None:
                    return False
                else:
                    if isinstance(points, list):
                        return (True if count in points else False)
                    else:
                        return (True if count == points else False)

C
ceci3 已提交
152
            #conv1
C
ceci3 已提交
153
            # all padding is 'SAME' in the conv2d, can compute the actual padding automatic. 
C
ceci3 已提交
154
            input = conv_bn_layer(
C
ceci3 已提交
155 156 157 158
                input,
                num_filters=int(32 * self.scale),
                filter_size=3,
                stride=2,
C
ceci3 已提交
159 160
                padding='SAME',
                act='relu6',
C
ceci3 已提交
161
                name='mobilenetv2_conv1')
C
ceci3 已提交
162
            layer_count = 1
C
ceci3 已提交
163

C
update  
ceci3 已提交
164
            depthwise_output = None
C
ceci3 已提交
165 166
            # bottleneck sequences
            in_c = int(32 * self.scale)
C
ceci3 已提交
167
            for i, layer_setting in enumerate(self.bottleneck_params_list):
C
ceci3 已提交
168
                t, c, n, s, k = layer_setting
C
update  
ceci3 已提交
169 170 171 172 173 174 175 176 177
                if s == 2:
                    layer_count += 1
                ### return_block and end_points means block num
                if check_points((layer_count - 1), return_block):
                    decode_ends[layer_count - 1] = depthwise_output

                if check_points((layer_count - 1), end_points):
                    return input, decode_ends

C
ceci3 已提交
178
                input, depthwise_output = self._invresi_blocks(
C
ceci3 已提交
179 180 181 182 183 184
                    input=input,
                    in_c=in_c,
                    t=t,
                    c=int(c * self.scale),
                    n=n,
                    s=s,
C
ceci3 已提交
185
                    k=int(k),
C
ceci3 已提交
186
                    name='mobilenetv2_conv' + str(i))
C
ceci3 已提交
187
                in_c = int(c * self.scale)
C
ceci3 已提交
188

C
update  
ceci3 已提交
189 190 191
            ### return_block and end_points means block num
            if check_points(layer_count, return_block):
                decode_ends[layer_count] = depthwise_output
C
ceci3 已提交
192

C
update  
ceci3 已提交
193 194
            if check_points(layer_count, end_points):
                return input, decode_ends
C
ceci3 已提交
195

C
ceci3 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            # last conv
            input = conv_bn_layer(
                input=input,
                num_filters=int(1280 * self.scale)
                if self.scale > 1.0 else 1280,
                filter_size=1,
                stride=1,
                padding='SAME',
                act='relu6',
                name='mobilenetv2_conv' + str(i + 1))

            input = fluid.layers.pool2d(
                input=input,
                pool_type='avg',
                global_pooling=True,
                name='mobilenetv2_last_pool')

C
ceci3 已提交
213 214
            return input

C
ceci3 已提交
215
        return net_arch
C
ceci3 已提交
216

C
ceci3 已提交
217
    def _shortcut(self, input, data_residual):
C
ceci3 已提交
218 219
        """Build shortcut layer.
        Args:
C
ceci3 已提交
220 221
            input(Variable): input.
            data_residual(Variable): residual layer.
C
ceci3 已提交
222 223 224 225 226
        Returns:
            Variable, layer output.
        """
        return fluid.layers.elementwise_add(input, data_residual)

C
ceci3 已提交
227
    def _inverted_residual_unit(self,
W
wanghaoshuang 已提交
228 229 230 231 232 233 234 235 236
                                input,
                                num_in_filter,
                                num_filters,
                                ifshortcut,
                                stride,
                                filter_size,
                                expansion_factor,
                                reduction_ratio=4,
                                name=None):
C
ceci3 已提交
237 238
        """Build inverted residual unit.
        Args:
C
ceci3 已提交
239 240 241 242 243 244
            input(Variable), input.
            num_in_filter(int), number of in filters.
            num_filters(int), number of filters.
            ifshortcut(bool), whether using shortcut.
            stride(int), stride.
            filter_size(int), filter size.
C
ceci3 已提交
245
            padding(str|int|list), padding.
C
ceci3 已提交
246 247
            expansion_factor(float), expansion factor.
            name(str), name.
C
ceci3 已提交
248 249 250 251
        Returns:
            Variable, layers output.
        """
        num_expfilter = int(round(num_in_filter * expansion_factor))
C
ceci3 已提交
252
        channel_expand = conv_bn_layer(
C
ceci3 已提交
253 254 255 256
            input=input,
            num_filters=num_expfilter,
            filter_size=1,
            stride=1,
C
ceci3 已提交
257
            padding='SAME',
C
ceci3 已提交
258
            num_groups=1,
C
ceci3 已提交
259
            act='relu6',
C
ceci3 已提交
260 261
            name=name + '_expand')

C
ceci3 已提交
262
        bottleneck_conv = conv_bn_layer(
C
ceci3 已提交
263 264 265 266
            input=channel_expand,
            num_filters=num_expfilter,
            filter_size=filter_size,
            stride=stride,
C
ceci3 已提交
267
            padding='SAME',
C
ceci3 已提交
268
            num_groups=num_expfilter,
C
ceci3 已提交
269
            act='relu6',
C
ceci3 已提交
270 271 272
            name=name + '_dwise',
            use_cudnn=False)

C
ceci3 已提交
273 274
        depthwise_output = bottleneck_conv

C
ceci3 已提交
275
        linear_out = conv_bn_layer(
C
ceci3 已提交
276 277 278 279
            input=bottleneck_conv,
            num_filters=num_filters,
            filter_size=1,
            stride=1,
C
ceci3 已提交
280
            padding='SAME',
C
ceci3 已提交
281
            num_groups=1,
C
ceci3 已提交
282
            act=None,
C
ceci3 已提交
283 284 285
            name=name + '_linear')
        out = linear_out
        if ifshortcut:
C
ceci3 已提交
286
            out = self._shortcut(input=input, data_residual=out)
C
ceci3 已提交
287
        return out, depthwise_output
C
ceci3 已提交
288

C
ceci3 已提交
289
    def _invresi_blocks(self, input, in_c, t, c, n, s, k, name=None):
C
ceci3 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302
        """Build inverted residual blocks.
        Args:
            input: Variable, input.
            in_c: int, number of in filters.
            t: float, expansion factor.
            c: int, number of filters.
            n: int, number of layers.
            s: int, stride.
            k: int, filter size.
            name: str, name.
        Returns:
            Variable, layers output.
        """
C
ceci3 已提交
303
        first_block, depthwise_output = self._inverted_residual_unit(
C
ceci3 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
            input=input,
            num_in_filter=in_c,
            num_filters=c,
            ifshortcut=False,
            stride=s,
            filter_size=k,
            expansion_factor=t,
            name=name + '_1')

        last_residual_block = first_block
        last_c = c

        for i in range(1, n):
C
ceci3 已提交
317
            last_residual_block, depthwise_output = self._inverted_residual_unit(
C
ceci3 已提交
318 319 320 321 322 323 324 325
                input=last_residual_block,
                num_in_filter=last_c,
                num_filters=c,
                ifshortcut=True,
                stride=1,
                filter_size=k,
                expansion_factor=t,
                name=name + '_' + str(i + 1))
C
ceci3 已提交
326
        return last_residual_block, depthwise_output