inception_block.py 21.0 KB
Newer Older
C
update  
ceci3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from .search_space_base import SearchSpaceBase
from .base_layer import conv_bn_layer
from .search_space_registry import SEARCHSPACE
C
update  
ceci3 已提交
25
from .utils import compute_downsample_num, check_points
C
update  
ceci3 已提交
26

C
ceci3 已提交
27
__all__ = ["InceptionABlockSpace", "InceptionCBlockSpace"]
C
update  
ceci3 已提交
28
### TODO add asymmetric kernel of conv when paddle-lite support 
C
ceci3 已提交
29
### inceptionB is same as inceptionA if asymmetric kernel is not support
C
update  
ceci3 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47


@SEARCHSPACE.register
class InceptionABlockSpace(SearchSpaceBase):
    def __init__(self, input_size, output_size, block_num, block_mask):
        super(InceptionABlockSpace, self).__init__(input_size, output_size,
                                                   block_num, block_mask)
        if self.block_mask == None:
            # use input_size and output_size to compute self.downsample_num
            self.downsample_num = compute_downsample_num(self.input_size,
                                                         self.output_size)
        if self.block_num != None:
            assert self.downsample_num <= self.block_num, 'downsample numeber must be LESS THAN OR EQUAL TO block_num, but NOW: downsample numeber is {}, block_num is {}'.format(
                self.downsample_num, self.block_num)

        ### self.filter_num means filter nums
        self.filter_num = np.array([
            3, 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 144, 160, 192, 224,
C
ceci3 已提交
48
            256, 320, 384, 448, 480, 512, 1024
C
update  
ceci3 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        ])
        ### self.k_size means kernel_size
        self.k_size = np.array([3, 5])
        ### self.pool_type means pool type, 0 means avg, 1 means max
        self.pool_type = np.array([0, 1])
        ### self.repeat means repeat of 1x1 conv in branch of inception
        ### self.repeat = np.array([0,1])

    def init_tokens(self):
        """
        The initial token.
        """
        if self.block_mask != None:
            return [0] * (len(self.block_mask) * 9)
        else:
            return [0] * (self.block_num * 9)

    def range_table(self):
        """
        Get range table of current search space, constrains the range of tokens.
        """
        range_table_base = []
        if self.block_mask != None:
            range_table_length = len(self.block_mask)
        else:
C
ceci3 已提交
74
            range_table_length = self.block_num
C
update  
ceci3 已提交
75 76 77 78 79 80 81 82 83 84

        for i in range(range_table_length):
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.k_size))
C
fix  
ceci3 已提交
85
            range_table_base.append(len(self.pool_type))
C
update  
ceci3 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        return range_table_base

    def token2arch(self, tokens=None):
        """
        return net_arch function
        """
        #assert self.block_num
        if tokens is None:
            tokens = self.init_tokens()

        self.bottleneck_params_list = []
        if self.block_mask != None:
            for i in range(len(self.block_mask)):
                self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
101 102 103 104 105 106 107 108 109
                    (self.filter_num[tokens[i * 9]],
                     self.filter_num[tokens[i * 9 + 1]],
                     self.filter_num[tokens[i * 9 + 2]],
                     self.filter_num[tokens[i * 9 + 3]],
                     self.filter_num[tokens[i * 9 + 4]],
                     self.filter_num[tokens[i * 9 + 5]],
                     self.filter_num[tokens[i * 9 + 6]],
                     self.k_size[tokens[i * 9 + 7]], 2 if self.block_mask == 1
                     else 1, self.pool_type[tokens[i * 9 + 8]]))
C
update  
ceci3 已提交
110
        else:
C
ceci3 已提交
111
            repeat_num = int(self.block_num / self.downsample_num)
C
update  
ceci3 已提交
112 113 114 115
            num_minus = self.block_num % self.downsample_num
            ### if block_num > downsample_num, add stride=1 block at last (block_num-downsample_num) layers
            for i in range(self.downsample_num):
                self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
116 117 118 119 120 121 122 123 124
                    (self.filter_num[tokens[i * 9]],
                     self.filter_num[tokens[i * 9 + 1]],
                     self.filter_num[tokens[i * 9 + 2]],
                     self.filter_num[tokens[i * 9 + 3]],
                     self.filter_num[tokens[i * 9 + 4]],
                     self.filter_num[tokens[i * 9 + 5]],
                     self.filter_num[tokens[i * 9 + 6]],
                     self.k_size[tokens[i * 9 + 7]], 2,
                     self.pool_type[tokens[i * 9 + 8]]))
C
update  
ceci3 已提交
125 126 127 128
                ### if block_num / downsample_num > 1, add (block_num / downsample_num) times stride=1 block 
                for k in range(repeat_num - 1):
                    kk = k * self.downsample_num + i
                    self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
129 130 131 132 133 134 135 136 137
                        (self.filter_num[tokens[kk * 9]],
                         self.filter_num[tokens[kk * 9 + 1]],
                         self.filter_num[tokens[kk * 9 + 2]],
                         self.filter_num[tokens[kk * 9 + 3]],
                         self.filter_num[tokens[kk * 9 + 4]],
                         self.filter_num[tokens[kk * 9 + 5]],
                         self.filter_num[tokens[kk * 9 + 6]],
                         self.k_size[tokens[kk * 9 + 7]], 1,
                         self.pool_type[tokens[kk * 9 + 8]]))
C
update  
ceci3 已提交
138 139

                if self.downsample_num - i <= num_minus:
C
ceci3 已提交
140
                    j = self.downsample_num * (repeat_num - 1) + i
C
fix  
ceci3 已提交
141 142 143 144 145 146 147 148 149 150
                    self.bottleneck_params_list.append(
                        (self.filter_num[tokens[j * 9]],
                         self.filter_num[tokens[j * 9 + 1]],
                         self.filter_num[tokens[j * 9 + 2]],
                         self.filter_num[tokens[j * 9 + 3]],
                         self.filter_num[tokens[j * 9 + 4]],
                         self.filter_num[tokens[j * 9 + 5]],
                         self.filter_num[tokens[j * 9 + 6]],
                         self.k_size[tokens[j * 9 + 7]], 1,
                         self.pool_type[tokens[j * 9 + 8]]))
C
update  
ceci3 已提交
151 152 153

            if self.downsample_num == 0 and self.block_num != 0:
                for i in range(len(self.block_num)):
C
fix  
ceci3 已提交
154 155 156 157 158 159 160 161 162 163
                    self.bottleneck_params_list.append(
                        (self.filter_num[tokens[i * 9]],
                         self.filter_num[tokens[i * 9 + 1]],
                         self.filter_num[tokens[i * 9 + 2]],
                         self.filter_num[tokens[i * 9 + 3]],
                         self.filter_num[tokens[i * 9 + 4]],
                         self.filter_num[tokens[i * 9 + 5]],
                         self.filter_num[tokens[i * 9 + 6]],
                         self.k_size[tokens[i * 9 + 7]], 1,
                         self.pool_type[tokens[i * 9 + 8]]))
C
update  
ceci3 已提交
164

C
update  
ceci3 已提交
165
        def net_arch(input, return_mid_layer=False, return_block=None):
C
update  
ceci3 已提交
166 167 168 169 170 171 172 173 174
            layer_count = 0
            mid_layer = dict()
            for i, layer_setting in enumerate(self.bottleneck_params_list):
                filter_nums = layer_setting[0:7]
                filter_size = layer_setting[7]
                stride = layer_setting[8]
                pool_type = 'avg' if layer_setting[9] == 0 else 'max'
                if stride == 2:
                    layer_count += 1
C
update  
ceci3 已提交
175
                if check_points((layer_count - 1), return_block):
C
update  
ceci3 已提交
176 177 178 179
                    mid_layer[layer_count - 1] = input

                input = self._inceptionA(
                    input,
C
ceci3 已提交
180
                    A_tokens=filter_nums,
C
update  
ceci3 已提交
181 182 183 184 185 186 187 188
                    filter_size=filter_size,
                    stride=stride,
                    pool_type=pool_type,
                    name='inceptionA_{}'.format(i + 1))

            if return_mid_layer:
                return input, mid_layer
            else:
C
fix  
ceci3 已提交
189
                return input,
C
update  
ceci3 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

        return net_arch

    def _inceptionA(self,
                    data,
                    A_tokens,
                    filter_size,
                    stride,
                    pool_type,
                    name=None):
        pool1 = fluid.layers.pool2d(
            input=data,
            pool_size=filter_size,
            pool_padding='SAME',
            pool_type=pool_type,
            name=name + '_pool2d')
        conv1 = conv_bn_layer(
            input=pool1,
            filter_size=1,
            num_filters=A_tokens[0],
            stride=stride,
            act='relu',
            name=name + '_conv1')

        conv2 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=A_tokens[1],
            stride=stride,
            act='relu',
            name=name + '_conv2')

        conv3 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=A_tokens[2],
            stride=1,
            act='relu',
            name=name + '_conv3_1')
        conv3 = conv_bn_layer(
            input=conv3,
            filter_size=filter_size,
            num_filters=A_tokens[3],
            stride=stride,
            act='relu',
            name=name + '_conv3_2')

        conv4 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=A_tokens[4],
            stride=1,
            act='relu',
            name=name + '_conv4_1')
        conv4 = conv_bn_layer(
            input=conv4,
            filter_size=filter_size,
            num_filters=A_tokens[5],
            stride=1,
            act='relu',
            name=name + '_conv4_2')
        conv4 = conv_bn_layer(
            input=conv4,
            filter_size=filter_size,
            num_filters=A_tokens[6],
            stride=stride,
            act='relu',
            name=name + '_conv4_3')

        concat = fluid.layers.concat(
            [conv1, conv2, conv3, conv4], axis=1, name=name + '_concat')
        return concat


C
ceci3 已提交
264 265 266
@SEARCHSPACE.register
class InceptionCBlockSpace(SearchSpaceBase):
    def __init__(self, input_size, output_size, block_num, block_mask):
C
fix  
ceci3 已提交
267
        super(InceptionCBlockSpace, self).__init__(input_size, output_size,
C
ceci3 已提交
268 269 270 271 272 273 274 275
                                                   block_num, block_mask)
        if self.block_mask == None:
            # use input_size and output_size to compute self.downsample_num
            self.downsample_num = compute_downsample_num(self.input_size,
                                                         self.output_size)
        if self.block_num != None:
            assert self.downsample_num <= self.block_num, 'downsample numeber must be LESS THAN OR EQUAL TO block_num, but NOW: downsample numeber is {}, block_num is {}'.format(
                self.downsample_num, self.block_num)
C
update  
ceci3 已提交
276

C
ceci3 已提交
277 278 279 280 281 282 283 284 285 286 287
        ### self.filter_num means filter nums
        self.filter_num = np.array([
            3, 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 144, 160, 192, 224,
            256, 320, 384, 448, 480, 512, 1024
        ])
        ### self.k_size means kernel_size
        self.k_size = np.array([3, 5])
        ### self.pool_type means pool type, 0 means avg, 1 means max
        self.pool_type = np.array([0, 1])
        ### self.repeat means repeat of 1x1 conv in branch of inception
        ### self.repeat = np.array([0,1])
C
update  
ceci3 已提交
288

C
ceci3 已提交
289 290 291 292 293
    def init_tokens(self):
        """
        The initial token.
        """
        if self.block_mask != None:
C
fix  
ceci3 已提交
294
            return [0] * (len(self.block_mask) * 11)
C
ceci3 已提交
295
        else:
C
fix  
ceci3 已提交
296
            return [0] * (self.block_num * 11)
C
ceci3 已提交
297 298 299 300 301 302 303 304 305

    def range_table(self):
        """
        Get range table of current search space, constrains the range of tokens.
        """
        range_table_base = []
        if self.block_mask != None:
            range_table_length = len(self.block_mask)
        else:
C
ceci3 已提交
306
            range_table_length = self.block_num
C
ceci3 已提交
307 308 309 310 311 312 313 314 315 316

        for i in range(range_table_length):
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.filter_num))
            range_table_base.append(len(self.k_size))
C
fix  
ceci3 已提交
317
            range_table_base.append(len(self.pool_type))
C
ceci3 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

        return range_table_base

    def token2arch(self, tokens=None):
        """
        return net_arch function
        """
        #assert self.block_num
        if tokens is None:
            tokens = self.init_tokens()

        self.bottleneck_params_list = []
        if self.block_mask != None:
            for i in range(len(self.block_mask)):
                self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
333 334 335 336 337 338 339 340 341 342 343
                    (self.filter_num[tokens[i * 11]],
                     self.filter_num[tokens[i * 11 + 1]],
                     self.filter_num[tokens[i * 11 + 2]],
                     self.filter_num[tokens[i * 11 + 3]],
                     self.filter_num[tokens[i * 11 + 4]],
                     self.filter_num[tokens[i * 11 + 5]],
                     self.filter_num[tokens[i * 11 + 6]],
                     self.filter_num[tokens[i * 11 + 7]],
                     self.filter_num[tokens[i * 11 + 8]],
                     self.k_size[tokens[i * 11 + 9]], 2 if self.block_mask == 1
                     else 1, self.pool_type[tokens[i * 11 + 10]]))
C
ceci3 已提交
344
        else:
C
ceci3 已提交
345
            repeat_num = int(self.block_num / self.downsample_num)
C
ceci3 已提交
346 347 348 349
            num_minus = self.block_num % self.downsample_num
            ### if block_num > downsample_num, add stride=1 block at last (block_num-downsample_num) layers
            for i in range(self.downsample_num):
                self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
350 351 352 353 354 355 356 357 358 359 360
                    (self.filter_num[tokens[i * 11]],
                     self.filter_num[tokens[i * 11 + 1]],
                     self.filter_num[tokens[i * 11 + 2]],
                     self.filter_num[tokens[i * 11 + 3]],
                     self.filter_num[tokens[i * 11 + 4]],
                     self.filter_num[tokens[i * 11 + 5]],
                     self.filter_num[tokens[i * 11 + 6]],
                     self.filter_num[tokens[i * 11 + 7]],
                     self.filter_num[tokens[i * 11 + 8]],
                     self.k_size[tokens[i * 11 + 9]], 2,
                     self.pool_type[tokens[i * 11 + 10]]))
C
ceci3 已提交
361 362 363
                ### if block_num / downsample_num > 1, add (block_num / downsample_num) times stride=1 block 
                for k in range(repeat_num - 1):
                    kk = k * self.downsample_num + i
C
fix  
ceci3 已提交
364 365 366 367 368 369 370 371 372 373 374 375
                    self.bottleneck_params_list.append(
                        (self.filter_num[tokens[kk * 11]],
                         self.filter_num[tokens[kk * 11 + 1]],
                         self.filter_num[tokens[kk * 11 + 2]],
                         self.filter_num[tokens[kk * 11 + 3]],
                         self.filter_num[tokens[kk * 11 + 4]],
                         self.filter_num[tokens[kk * 11 + 5]],
                         self.filter_num[tokens[kk * 11 + 6]],
                         self.filter_num[tokens[kk * 11 + 7]],
                         self.filter_num[tokens[kk * 11 + 8]],
                         self.k_size[tokens[kk * 11 + 9]], 1,
                         self.pool_type[tokens[kk * 11 + 10]]))
C
ceci3 已提交
376 377

                if self.downsample_num - i <= num_minus:
C
ceci3 已提交
378
                    j = self.downsample_num * (repeat_num - 1) + i
C
ceci3 已提交
379
                    self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
380 381 382 383 384 385 386 387 388 389 390
                        (self.filter_num[tokens[j * 11]],
                         self.filter_num[tokens[j * 11 + 1]],
                         self.filter_num[tokens[j * 11 + 2]],
                         self.filter_num[tokens[j * 11 + 3]],
                         self.filter_num[tokens[j * 11 + 4]],
                         self.filter_num[tokens[j * 11 + 5]],
                         self.filter_num[tokens[j * 11 + 6]],
                         self.filter_num[tokens[j * 11 + 7]],
                         self.filter_num[tokens[j * 11 + 8]],
                         self.k_size[tokens[j * 11 + 9]], 1,
                         self.pool_type[tokens[j * 11 + 10]]))
C
ceci3 已提交
391 392 393 394

            if self.downsample_num == 0 and self.block_num != 0:
                for i in range(len(self.block_num)):
                    self.bottleneck_params_list.append(
C
fix  
ceci3 已提交
395 396 397 398 399 400 401 402 403 404 405
                        (self.filter_num[tokens[i * 11]],
                         self.filter_num[tokens[i * 11 + 1]],
                         self.filter_num[tokens[i * 11 + 2]],
                         self.filter_num[tokens[i * 11 + 3]],
                         self.filter_num[tokens[i * 11 + 4]],
                         self.filter_num[tokens[i * 11 + 5]],
                         self.filter_num[tokens[i * 11 + 6]],
                         self.filter_num[tokens[i * 11 + 7]],
                         self.filter_num[tokens[i * 11 + 8]],
                         self.k_size[tokens[i * 11 + 9]], 1,
                         self.pool_type[tokens[i * 11 + 10]]))
C
ceci3 已提交
406

C
update  
ceci3 已提交
407
        def net_arch(input, return_mid_layer=False, return_block=None):
C
ceci3 已提交
408 409 410 411 412 413 414 415 416
            layer_count = 0
            mid_layer = dict()
            for i, layer_setting in enumerate(self.bottleneck_params_list):
                filter_nums = layer_setting[0:9]
                filter_size = layer_setting[9]
                stride = layer_setting[10]
                pool_type = 'avg' if layer_setting[11] == 0 else 'max'
                if stride == 2:
                    layer_count += 1
C
update  
ceci3 已提交
417
                if check_points((layer_count - 1) in return_block):
C
ceci3 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
                    mid_layer[layer_count - 1] = input

                input = self._inceptionC(
                    input,
                    C_tokens=filter_nums,
                    filter_size=filter_size,
                    stride=stride,
                    pool_type=pool_type,
                    name='inceptionC_{}'.format(i + 1))

            if return_mid_layer:
                return input, mid_layer
            else:
C
fix  
ceci3 已提交
431
                return input,
C
ceci3 已提交
432 433

        return net_arch
C
update  
ceci3 已提交
434 435 436

    def _inceptionC(self,
                    data,
C
ceci3 已提交
437
                    C_tokens,
C
update  
ceci3 已提交
438 439
                    filter_size,
                    stride,
C
ceci3 已提交
440
                    pool_type,
C
update  
ceci3 已提交
441 442 443 444 445
                    name=None):
        pool1 = fluid.layers.pool2d(
            input=data,
            pool_size=filter_size,
            pool_padding='SAME',
C
ceci3 已提交
446 447
            pool_type=pool_type,
            name=name + '_pool2d')
C
update  
ceci3 已提交
448 449 450 451 452 453
        conv1 = conv_bn_layer(
            input=pool1,
            filter_size=1,
            num_filters=C_tokens[0],
            stride=stride,
            act='relu',
C
ceci3 已提交
454
            name=name + '_conv1')
C
update  
ceci3 已提交
455 456 457 458 459 460 461

        conv2 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=C_tokens[1],
            stride=stride,
            act='relu',
C
ceci3 已提交
462
            name=name + '_conv2')
C
update  
ceci3 已提交
463 464 465 466 467 468 469

        conv3 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=C_tokens[2],
            stride=1,
            act='relu',
C
ceci3 已提交
470
            name=name + '_conv3_1')
C
update  
ceci3 已提交
471 472 473 474 475 476
        conv3_1 = conv_bn_layer(
            input=conv3,
            filter_size=filter_size,
            num_filters=C_tokens[3],
            stride=stride,
            act='relu',
C
ceci3 已提交
477
            name=name + '_conv3_2_1')
C
update  
ceci3 已提交
478 479 480 481 482 483
        conv3_2 = conv_bn_layer(
            input=conv3,
            filter_size=filter_size,
            num_filters=C_tokens[4],
            stride=stride,
            act='relu',
C
ceci3 已提交
484
            name=name + '_conv3_2_2')
C
update  
ceci3 已提交
485 486 487 488 489 490 491

        conv4 = conv_bn_layer(
            input=data,
            filter_size=1,
            num_filters=C_tokens[5],
            stride=1,
            act='relu',
C
ceci3 已提交
492
            name=name + '_conv4_1')
C
update  
ceci3 已提交
493 494 495 496 497 498
        conv4 = conv_bn_layer(
            input=conv4,
            filter_size=filter_size,
            num_filters=C_tokens[6],
            stride=1,
            act='relu',
C
ceci3 已提交
499
            name=name + '_conv4_2')
C
update  
ceci3 已提交
500 501 502 503 504 505
        conv4_1 = conv_bn_layer(
            input=conv4,
            filter_size=filter_size,
            num_filters=C_tokens[7],
            stride=stride,
            act='relu',
C
ceci3 已提交
506
            name=name + '_conv4_3_1')
C
update  
ceci3 已提交
507 508 509 510 511 512
        conv4_2 = conv_bn_layer(
            input=conv4,
            filter_size=filter_size,
            num_filters=C_tokens[8],
            stride=stride,
            act='relu',
C
ceci3 已提交
513
            name=name + '_conv4_3_2')
C
update  
ceci3 已提交
514 515 516 517

        concat = fluid.layers.concat(
            [conv1, conv2, conv3_1, conv3_2, conv4_1, conv4_2],
            axis=1,
C
ceci3 已提交
518
            name=name + '_concat')
C
update  
ceci3 已提交
519
        return concat