ghostnet.py 10.9 KB
Newer Older
W
wqz960 已提交
1
from __future__ import absolute_import
W
wqz960 已提交
2 3 4 5 6
from __future__ import division
from __future__ import print_function

import math

W
wqz960 已提交
7
import paddle.fluid as fluid
W
wqz960 已提交
8 9 10 11
from paddle.fluid.param_attr import ParamAttr

__all__ = ["GhostNet", "GhostNet_0_5", "GhostNet_1_0", "GhostNet_1_3"]

W
wqz960 已提交
12

W
wqz960 已提交
13 14 15
class GhostNet():
    def __init__(self, width_mult):
        cfgs = [
W
wqz960 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
            # k, t, c, SE, s
            [3, 16, 16, 0, 1],
            [3, 48, 24, 0, 2],
            [3, 72, 24, 0, 1],
            [5, 72, 40, 1, 2],
            [5, 120, 40, 1, 1],
            [3, 240, 80, 0, 2],
            [3, 200, 80, 0, 1],
            [3, 184, 80, 0, 1],
            [3, 184, 80, 0, 1],
            [3, 480, 112, 1, 1],
            [3, 672, 112, 1, 1],
            [5, 672, 160, 1, 2],
            [5, 960, 160, 0, 1],
            [5, 960, 160, 1, 1],
            [5, 960, 160, 0, 1],
            [5, 960, 160, 1, 1]
        ]
W
wqz960 已提交
34 35
        self.cfgs = cfgs
        self.width_mult = width_mult
W
wqz960 已提交
36

W
wqz960 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    def _make_divisible(self, v, divisor, min_value=None):
        """
        This function is taken from the original tf repo.
        It ensures that all layers have a channel number that is divisible by 8
        It can be seen here:
        https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
        """
        if min_value is None:
            min_value = divisor
        new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
        # Make sure that round down does not go down by more than 10%.
        if new_v < 0.9 * v:
            new_v += divisor
        return new_v
W
wqz960 已提交
51 52 53

    def conv_bn_layer(self,
                      input,
W
wqz960 已提交
54 55 56 57 58 59 60
                      num_filters,
                      filter_size,
                      stride=1,
                      groups=1,
                      act=None,
                      name=None,
                      data_format="NCHW"):
W
wqz960 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        x = fluid.layers.conv2d(
            input=input,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            act=None,
            param_attr=ParamAttr(
                initializer=fluid.initializer.MSRA(), name=name + "_weights"),
            bias_attr=False,
            name=name + "_conv_op",
            data_format=data_format)

        x = fluid.layers.batch_norm(
            input=x,
            act=act,
            name=name + "_bn",
            param_attr=ParamAttr(
                name=name + "_bn_scale",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            bias_attr=ParamAttr(
                name=name + "_bn_offset",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            moving_mean_name=name + "_bn_mean",
            moving_variance_name=name + "_bn_variance",
            data_layout=data_format)
W
wqz960 已提交
90
        return x
W
wqz960 已提交
91 92

    def SElayer(self, input, num_channels, reduction_ratio=4, name=None):
W
wqz960 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        pool = fluid.layers.pool2d(
            input=input, pool_size=0, pool_type='avg', global_pooling=True)
        stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
        squeeze = fluid.layers.fc(
            input=pool,
            size=num_channels // reduction_ratio,
            act='relu',
            param_attr=fluid.param_attr.ParamAttr(
                initializer=fluid.initializer.Uniform(-stdv, stdv),
                name=name + '_sqz_weights'),
            bias_attr=ParamAttr(name=name + '_sqz_offset'))
        stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)
        excitation = fluid.layers.fc(
            input=squeeze,
            size=num_channels,
            act=None,
            param_attr=fluid.param_attr.ParamAttr(
                initializer=fluid.initializer.Uniform(-stdv, stdv),
                name=name + '_exc_weights'),
            bias_attr=ParamAttr(name=name + '_exc_offset'))
W
wqz960 已提交
113 114
        excitation = fluid.layers.clip(
            x=excitation, min=0, max=1, name=name + '_clip')
W
wqz960 已提交
115 116
        scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
        return scale
W
wqz960 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    def depthwise_conv(self,
                       inp,
                       oup,
                       kernel_size,
                       stride=1,
                       relu=False,
                       name=None,
                       data_format="NCHW"):
        return self.conv_bn_layer(
            input=inp,
            num_filters=oup,
            filter_size=kernel_size,
            stride=stride,
            groups=inp.shape[1] if data_format == "NCHW" else inp.shape[-1],
            act="relu" if relu else None,
            name=name + "_dw",
            data_format=data_format)

W
wqz960 已提交
136 137 138 139 140 141 142 143 144 145
    def GhostModule(self,
                    inp,
                    oup,
                    kernel_size=1,
                    ratio=2,
                    dw_size=3,
                    stride=1,
                    relu=True,
                    name=None,
                    data_format="NCHW"):
W
wqz960 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        self.oup = oup
        init_channels = int(math.ceil(oup / ratio))
        new_channels = int(init_channels * (ratio - 1))
        primary_conv = self.conv_bn_layer(
            input=inp,
            num_filters=init_channels,
            filter_size=kernel_size,
            stride=stride,
            groups=1,
            act="relu" if relu else None,
            name=name + "_primary_conv",
            data_format="NCHW")
        cheap_operation = self.conv_bn_layer(
            input=primary_conv,
            num_filters=new_channels,
            filter_size=dw_size,
            stride=1,
            groups=init_channels,
            act="relu" if relu else None,
            name=name + "_cheap_operation",
            data_format=data_format)
        out = fluid.layers.concat(
            [primary_conv, cheap_operation], axis=1, name=name + "_concat")
W
wqz960 已提交
169
        return out
W
wqz960 已提交
170

W
wqz960 已提交
171 172 173 174
    def GhostBottleneck(self,
                        inp,
                        hidden_dim,
                        oup,
W
wqz960 已提交
175
                        kernel_size,
W
wqz960 已提交
176 177 178 179 180
                        stride,
                        use_se,
                        name=None,
                        data_format="NCHW"):
        inp_channels = inp.shape[1]
W
wqz960 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        x = self.GhostModule(
            inp=inp,
            oup=hidden_dim,
            kernel_size=1,
            stride=1,
            relu=True,
            name=name + "GhostBottle_1",
            data_format="NCHW")
        if stride == 2:
            x = self.depthwise_conv(
                inp=x,
                oup=hidden_dim,
                kernel_size=kernel_size,
                stride=stride,
                relu=False,
                name=name + "_dw2",
                data_format="NCHW")
W
wqz960 已提交
198
        if use_se:
W
wqz960 已提交
199 200 201 202 203 204 205 206 207
            x = self.SElayer(
                input=x, num_channels=hidden_dim, name=name + "SElayer")
        x = self.GhostModule(
            inp=x,
            oup=oup,
            kernel_size=1,
            relu=False,
            name=name + "GhostModule_2")
        if stride == 1 and inp_channels == oup:
W
wqz960 已提交
208 209
            shortcut = inp
        else:
W
wqz960 已提交
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
            shortcut = self.depthwise_conv(
                inp=inp,
                oup=inp_channels,
                kernel_size=kernel_size,
                stride=stride,
                relu=False,
                name=name + "shortcut_depthwise_conv",
                data_format="NCHW")
            shortcut = self.conv_bn_layer(
                input=shortcut,
                num_filters=oup,
                filter_size=1,
                stride=1,
                groups=1,
                act=None,
                name=name + "shortcut_conv_bn",
                data_format="NCHW")
        return fluid.layers.elementwise_add(
            x=x, y=shortcut, axis=-1, act=None, name=name + "elementwise_add")

    def net(self, input, class_dim=1000):
        # build first layer:
        output_channel = int(self._make_divisible(16 * self.width_mult, 4))
        x = self.conv_bn_layer(
            input=input,
            num_filters=output_channel,
            filter_size=3,
            stride=2,
            groups=1,
            act="relu",
            name="firstlayer",
            data_format="NCHW")
        # build inverted residual blocks
W
wqz960 已提交
243 244
        idx = 0
        for k, exp_size, c, use_se, s in self.cfgs:
W
wqz960 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            output_channel = int(self._make_divisible(c * self.width_mult, 4))
            hidden_channel = int(
                self._make_divisible(exp_size * self.width_mult, 4))
            x = self.GhostBottleneck(
                inp=x,
                hidden_dim=hidden_channel,
                oup=output_channel,
                kernel_size=k,
                stride=s,
                use_se=use_se,
                name="GhostBottle_" + str(idx),
                data_format="NCHW")
            idx += 1
        # build last several layers
        output_channel = int(
            self._make_divisible(exp_size * self.width_mult, 4))
        x = self.conv_bn_layer(
            input=x,
            num_filters=output_channel,
            filter_size=1,
            stride=1,
            groups=1,
            act="relu",
            name="lastlayer",
            data_format="NCHW")
        x = fluid.layers.pool2d(
            input=x, pool_type='avg', global_pooling=True, data_format="NCHW")
W
wqz960 已提交
272
        output_channel = 1280
W
wqz960 已提交
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

        stdv = 1.0 / math.sqrt(x.shape[1] * 1.0)
        out = fluid.layers.conv2d(
            input=x,
            num_filters=output_channel,
            filter_size=1,
            groups=1,
            param_attr=ParamAttr(
                name="fc_0_w",
                initializer=fluid.initializer.Uniform(-stdv, stdv)),
            bias_attr=False,
            name="fc_0")
        out = fluid.layers.batch_norm(
            input=out,
            act="relu",
            name="fc_0_bn",
            param_attr=ParamAttr(
                name="fc_0_bn_scale",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            bias_attr=ParamAttr(
                name="fc_0_bn_offset",
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=0.0)),
            moving_mean_name="fc_0_bn_mean",
            moving_variance_name="fc_0_bn_variance",
            data_layout="NCHW")
W
wqz960 已提交
300
        out = fluid.layers.dropout(x=out, dropout_prob=0.2)
W
wqz960 已提交
301 302 303 304 305 306 307 308 309 310 311
        stdv = 1.0 / math.sqrt(out.shape[1] * 1.0)
        out = fluid.layers.fc(
            input=out,
            size=class_dim,
            param_attr=ParamAttr(
                name="fc_1_w",
                initializer=fluid.initializer.Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="fc_1_bias"))

        return out

W
wqz960 已提交
312 313 314 315 316

def GhostNet_0_5():
    model = GhostNet(width_mult=0.5)
    return model

W
wqz960 已提交
317

W
wqz960 已提交
318 319
def GhostNet_1_0():
    model = GhostNet(width_mult=1.0)
W
wqz960 已提交
320 321
    return model

W
wqz960 已提交
322 323 324 325

def GhostNet_1_3():
    model = GhostNet(width_mult=1.3)
    return model