inception_v3.py 18.1 KB
Newer Older
C
cuicheng01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle
import paddle.fluid as fluid
import math
from paddle.fluid.param_attr import ParamAttr

__all__ = ["InceptionV3"]

class InceptionV3():
L
Leo Chen 已提交
27
    def __init__(self, data_format="NCHW"):
C
cuicheng01 已提交
28 29
        self.inception_a_list = [32, 64, 64]
        self.inception_c_list = [128, 160, 160, 192]
L
Leo Chen 已提交
30 31
        self.data_format = data_format
        self.concat_axis = 3 if self.data_format=="NHWC" else 1
C
cuicheng01 已提交
32 33

    def net(self, input, class_dim=1000):
L
Leo Chen 已提交
34
        
C
cuicheng01 已提交
35 36 37 38 39 40 41 42 43 44
        x = self.inception_stem(input)
        for i, pool_features in enumerate(self.inception_a_list):
            x = self.inceptionA(x, pool_features, name=str(i+1))
        x = self.inceptionB(x, name="1")
        for i, channels_7x7 in enumerate(self.inception_c_list):
             x = self.inceptionC(x, channels_7x7, name=str(i+1))
        x = self.inceptionD(x, name="1")
        x = self.inceptionE(x, name="1")
        x = self.inceptionE(x, name="2")

L
Leo Chen 已提交
45
        pool = fluid.layers.pool2d(input=x, pool_type="avg", global_pooling=True, data_format=self.data_format)
C
cuicheng01 已提交
46 47 48 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 74 75

        drop = fluid.layers.dropout(x=pool, dropout_prob=0.2)

        stdv = 1.0 / math.sqrt(drop.shape[1] * 1.0)
        out = fluid.layers.fc(
            input=drop,
            size=class_dim,
            param_attr=ParamAttr(initializer=fluid.initializer.Uniform(-stdv, stdv), name="fc_weights"),
            bias_attr=ParamAttr(initializer=fluid.initializer.Uniform(-stdv, stdv), name="fc_offset"))
        return out

    def conv_bn_layer(self,
                      data,
                      num_filters,
                      filter_size,
                      stride=1,
                      padding=0,
                      groups=1,
                      act="relu",
                      name=None):
        conv = fluid.layers.conv2d(
            input=data,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=groups,
            act=None,
            param_attr=ParamAttr(name=name+"_weights"),
            bias_attr=False,
L
Leo Chen 已提交
76 77
            name=name, 
            data_format=self.data_format)
C
cuicheng01 已提交
78 79 80 81 82
        return fluid.layers.batch_norm(input=conv, 
                                       act=act,
                                       param_attr = ParamAttr(name=name+"_bn_scale"),
                                       bias_attr=ParamAttr(name=name+"_bn_offset"),
                                       moving_mean_name=name+"_bn_mean",
L
Leo Chen 已提交
83 84
                                       moving_variance_name=name+"_bn_variance", 
                                       data_layout=self.data_format)
C
cuicheng01 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    
    def inception_stem(self, x):
        x = self.conv_bn_layer(x, 
                               num_filters=32, 
                               filter_size=3, 
                               stride=2,
                               act="relu",
                               name="conv_1a_3x3")
        x = self.conv_bn_layer(x, 
                               num_filters=32, 
                               filter_size=3, 
                               stride=1,
                               act="relu",
                               name="conv_2a_3x3")        
        x = self.conv_bn_layer(x, 
                               num_filters=64, 
                               filter_size=3, 
                               padding=1,
                               act="relu",
                               name="conv_2b_3x3")         

L
Leo Chen 已提交
106
        x = fluid.layers.pool2d(input=x, pool_size=3, pool_stride=2, pool_type="max", data_format=self.data_format)
C
cuicheng01 已提交
107 108 109 110 111 112 113 114 115 116 117 118
        
        x = self.conv_bn_layer(x, 
                               num_filters=80, 
                               filter_size=1, 
                               act="relu",
                               name="conv_3b_1x1")  
        x = self.conv_bn_layer(x, 
                               num_filters=192, 
                               filter_size=3, 
                               act="relu",
                               name="conv_4a_3x3")  

L
Leo Chen 已提交
119
        x = fluid.layers.pool2d(input=x, pool_size=3, pool_stride=2, pool_type="max", data_format=self.data_format)
C
cuicheng01 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        return x

    def inceptionA(self, x, pool_features, name=None):
        branch1x1 = self.conv_bn_layer(x, 
                                       num_filters=64, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_a_branch1x1_"+name)
        branch5x5 = self.conv_bn_layer(x, 
                                       num_filters=48, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_a_branch5x5_1_"+name)
        branch5x5 = self.conv_bn_layer(branch5x5, 
                                       num_filters=64, 
                                       filter_size=5, 
                                       padding=2, 
                                       act="relu",
                                       name="inception_a_branch5x5_2_"+name)

        branch3x3dbl = self.conv_bn_layer(x, 
                                       num_filters=64, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_a_branch3x3dbl_1_"+name)
        branch3x3dbl = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=96, 
                                       filter_size=3, 
                                       padding=1,
                                       act="relu",
                                       name="inception_a_branch3x3dbl_2_"+name)
        branch3x3dbl = self.conv_bn_layer(branch3x3dbl, 
                               num_filters=96, 
                               filter_size=3, 
                               padding=1,
                               act="relu",
                               name="inception_a_branch3x3dbl_3_"+name)
L
Leo Chen 已提交
158
        branch_pool = fluid.layers.pool2d(x, pool_size=3, pool_padding=1, pool_type="avg", data_format=self.data_format)
C
cuicheng01 已提交
159 160 161 162 163
        branch_pool = self.conv_bn_layer(branch_pool, 
                               num_filters=pool_features, 
                               filter_size=1, 
                               act="relu",
                               name="inception_a_branch_pool_"+name)
L
Leo Chen 已提交
164 165

        concat = fluid.layers.concat([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=self.concat_axis)
C
cuicheng01 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        
        return concat

    
    
    def inceptionB(self, x, name=None):
        branch3x3 = self.conv_bn_layer(x, 
                                       num_filters=384, 
                                       filter_size=3, 
                                       stride=2,
                                       act="relu",
                                       name="inception_b_branch3x3_"+name)
        branch3x3dbl = self.conv_bn_layer(x, 
                                       num_filters=64, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_b_branch3x3dbl_1_"+name)
        branch3x3dbl = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=96, 
                                       filter_size=3, 
                                       padding=1,
                                       act="relu",
                                       name="inception_b_branch3x3dbl_2_"+name)
        branch3x3dbl = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=96, 
                                       filter_size=3,
                                       stride=2,
                                       act="relu",
                                       name="inception_b_branch3x3dbl_3_"+name)
L
Leo Chen 已提交
195
        branch_pool = fluid.layers.pool2d(x, pool_size=3, pool_stride=2, pool_type="max", data_format=self.data_format)
C
cuicheng01 已提交
196
        
L
Leo Chen 已提交
197
        concat = fluid.layers.concat([branch3x3, branch3x3dbl, branch_pool], axis=self.concat_axis)
C
cuicheng01 已提交
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
        
        return concat
    
    
    def inceptionC(self, x, channels_7x7, name=None):
        branch1x1 = self.conv_bn_layer(x, 
                                       num_filters=192, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_c_branch1x1_"+name)
        branch7x7 = self.conv_bn_layer(x, 
                                       num_filters=channels_7x7, 
                                       filter_size=1, 
                                       stride=1,
                                       act="relu",
                                       name="inception_c_branch7x7_1_"+name)
        branch7x7 = self.conv_bn_layer(branch7x7, 
                                       num_filters=channels_7x7, 
                                       filter_size=(1, 7), 
                                       stride=1,
                                       padding=(0, 3),
                                       act="relu",
                                       name="inception_c_branch7x7_2_"+name)
        branch7x7 = self.conv_bn_layer(branch7x7, 
                                       num_filters=192, 
                                       filter_size=(7, 1), 
                                       stride=1,
                                       padding=(3, 0),
                                       act="relu",
                                       name="inception_c_branch7x7_3_"+name)
        
        branch7x7dbl = self.conv_bn_layer(x, 
                                       num_filters=channels_7x7, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_c_branch7x7dbl_1_"+name)
        branch7x7dbl = self.conv_bn_layer(branch7x7dbl, 
                                       num_filters=channels_7x7, 
                                       filter_size=(7, 1), 
                                       padding = (3, 0),
                                       act="relu",
                                       name="inception_c_branch7x7dbl_2_"+name)
        branch7x7dbl = self.conv_bn_layer(branch7x7dbl, 
                                       num_filters=channels_7x7, 
                                       filter_size=(1, 7), 
                                       padding = (0, 3),
                                       act="relu",
                                       name="inception_c_branch7x7dbl_3_"+name)
        branch7x7dbl = self.conv_bn_layer(branch7x7dbl, 
                                       num_filters=channels_7x7, 
                                       filter_size=(7, 1), 
                                       padding = (3, 0),
                                       act="relu",
                                       name="inception_c_branch7x7dbl_4_"+name)
        branch7x7dbl = self.conv_bn_layer(branch7x7dbl, 
                                       num_filters=192, 
                                       filter_size=(1, 7), 
                                       padding = (0, 3),
                                       act="relu",
                                       name="inception_c_branch7x7dbl_5_"+name)
       
L
Leo Chen 已提交
259
        branch_pool = fluid.layers.pool2d(x, pool_size=3, pool_stride=1, pool_padding=1, pool_type="avg", data_format=self.data_format)
C
cuicheng01 已提交
260 261 262 263
        branch_pool = self.conv_bn_layer(branch_pool, 
                                       num_filters=192, 
                                       filter_size=1, 
                                       act="relu",
L
Leo Chen 已提交
264 265
                                       name="inception_c_branch_pool_"+name)    
        concat = fluid.layers.concat([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=self.concat_axis)
C
cuicheng01 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        
        return concat

    
    def inceptionD(self, x, name=None):
        branch3x3 = self.conv_bn_layer(x, 
                                       num_filters=192, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_d_branch3x3_1_"+name)
        branch3x3 = self.conv_bn_layer(branch3x3, 
                                       num_filters=320, 
                                       filter_size=3, 
                                       stride=2,
                                       act="relu",
                                       name="inception_d_branch3x3_2_"+name)
        branch7x7x3 = self.conv_bn_layer(x, 
                                       num_filters=192, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_d_branch7x7x3_1_"+name)
        branch7x7x3 = self.conv_bn_layer(branch7x7x3, 
                                       num_filters=192, 
                                       filter_size=(1, 7), 
                                       padding=(0, 3),
                                       act="relu",
                                       name="inception_d_branch7x7x3_2_"+name)
        branch7x7x3 = self.conv_bn_layer(branch7x7x3, 
                                       num_filters=192, 
                                       filter_size=(7, 1), 
                                       padding=(3, 0),
                                       act="relu",
                                       name="inception_d_branch7x7x3_3_"+name)
        branch7x7x3 = self.conv_bn_layer(branch7x7x3, 
                                       num_filters=192, 
                                       filter_size=3, 
                                       stride=2,
                                       act="relu",
                                       name="inception_d_branch7x7x3_4_"+name)
L
Leo Chen 已提交
305 306 307
        branch_pool = fluid.layers.pool2d(x, pool_size=3, pool_stride=2, pool_type="max", data_format=self.data_format)
        
        concat = fluid.layers.concat([branch3x3, branch7x7x3, branch_pool], axis=self.concat_axis)
C
cuicheng01 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        
        return concat
    
    
    def inceptionE(self, x, name=None):
        branch1x1 = self.conv_bn_layer(x, 
                                       num_filters=320, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_e_branch1x1_"+name)
        branch3x3 = self.conv_bn_layer(x, 
                                       num_filters=384, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_e_branch3x3_1_"+name)
        branch3x3_2a = self.conv_bn_layer(branch3x3, 
                                       num_filters=384, 
                                       filter_size=(1, 3), 
                                       padding=(0, 1),
                                       act="relu",
                                       name="inception_e_branch3x3_2a_"+name)
        branch3x3_2b = self.conv_bn_layer(branch3x3, 
                                       num_filters=384, 
                                       filter_size=(3, 1), 
                                       padding=(1, 0),
                                       act="relu",
                                       name="inception_e_branch3x3_2b_"+name)
        
L
Leo Chen 已提交
336
        branch3x3 = fluid.layers.concat([branch3x3_2a, branch3x3_2b], axis=self.concat_axis)
C
cuicheng01 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        branch3x3dbl = self.conv_bn_layer(x, 
                                       num_filters=448, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_e_branch3x3dbl_1_"+name)
        branch3x3dbl = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=384, 
                                       filter_size=3, 
                                       padding=1,
                                       act="relu",
                                       name="inception_e_branch3x3dbl_2_"+name)
        branch3x3dbl_3a = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=384, 
                                       filter_size=(1, 3), 
                                       padding=(0, 1),
                                       act="relu",
                                       name="inception_e_branch3x3dbl_3a_"+name)
        branch3x3dbl_3b = self.conv_bn_layer(branch3x3dbl, 
                                       num_filters=384, 
                                       filter_size=(3, 1), 
                                       padding=(1, 0),
                                       act="relu",
                                       name="inception_e_branch3x3dbl_3b_"+name)
L
Leo Chen 已提交
360 361
        branch3x3dbl = fluid.layers.concat([branch3x3dbl_3a, branch3x3dbl_3b], axis=self.concat_axis)
        branch_pool = fluid.layers.pool2d(x, pool_size=3, pool_stride=1, pool_padding=1, pool_type="avg", data_format=self.data_format)
C
cuicheng01 已提交
362 363 364 365 366
        branch_pool = self.conv_bn_layer(branch_pool, 
                                       num_filters=192, 
                                       filter_size=1, 
                                       act="relu",
                                       name="inception_e_branch_pool_"+name)
L
Leo Chen 已提交
367
        concat = fluid.layers.concat([branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=self.concat_axis)
C
cuicheng01 已提交
368
        
L
Leo Chen 已提交
369
        return concat