slimfacenet.py 12.1 KB
Newer Older
X
xiteng1988 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# ================================================================
#   Copyright (c) 2020  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.
import math
import datetime
import numpy as np

import paddle
import paddle.fluid as fluid
W
whs 已提交
21
from paddle.nn.initializer import KaimingUniform
X
xiteng1988 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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


class SlimFaceNet():
    def __init__(self, class_dim, scale=0.6, arch=None):

        assert arch is not None
        self.arch = arch
        self.class_dim = class_dim
        kernels = [3]
        expansions = [2, 4, 6]
        SE = [0, 1]
        self.table = []
        for k in kernels:
            for e in expansions:
                for se in SE:
                    self.table.append((k, e, se))

        if scale == 1.0:
            # 100% - channel
            self.Slimfacenet_bottleneck_setting = [
                # t, c , n ,s
                [2, 64, 5, 2],
                [4, 128, 1, 2],
                [2, 128, 6, 1],
                [4, 128, 1, 2],
                [2, 128, 2, 1]
            ]
        elif scale == 0.9:
            # 90% - channel
            self.Slimfacenet_bottleneck_setting = [
                # t, c , n ,s
                [2, 56, 5, 2],
                [4, 116, 1, 2],
                [2, 116, 6, 1],
                [4, 116, 1, 2],
                [2, 116, 2, 1]
            ]
        elif scale == 0.75:
            # 75% - channel
            self.Slimfacenet_bottleneck_setting = [
                # t, c , n ,s
                [2, 48, 5, 2],
                [4, 96, 1, 2],
                [2, 96, 6, 1],
                [4, 96, 1, 2],
                [2, 96, 2, 1]
            ]
        elif scale == 0.6:
            # 60% - channel
            self.Slimfacenet_bottleneck_setting = [
                # t, c , n ,s
                [2, 40, 5, 2],
                [4, 76, 1, 2],
                [2, 76, 6, 1],
                [4, 76, 1, 2],
                [2, 76, 2, 1]
            ]
        else:
            print('WRONG scale')
            exit()
        self.extract_feature = True

    def set_extract_feature_flag(self, flag):
        self.extract_feature = flag

    def net(self, input, label=None):
        x = self.conv_bn_layer(
            input,
            filter_size=3,
            num_filters=64,
            stride=2,
            padding=1,
            num_groups=1,
            if_act=True,
            name='conv3x3')
        x = self.conv_bn_layer(
            x,
            filter_size=3,
            num_filters=64,
            stride=1,
            padding=1,
            num_groups=64,
            if_act=True,
            name='dw_conv3x3')

        in_c = 64
        cnt = 0
        for _exp, out_c, times, _stride in self.Slimfacenet_bottleneck_setting:
            for i in range(times):
                stride = _stride if i == 0 else 1
                filter_size, exp, se = self.table[self.arch[cnt]]
                se = False if se == 0 else True
                x = self.residual_unit(
                    x,
                    num_in_filter=in_c,
                    num_out_filter=out_c,
                    stride=stride,
                    filter_size=filter_size,
                    expansion_factor=exp,
                    use_se=se,
                    name='residual_unit' + str(cnt + 1))
                cnt += 1
                in_c = out_c

        out_c = 512
        x = self.conv_bn_layer(
            x,
            filter_size=1,
            num_filters=out_c,
            stride=1,
            padding=0,
            num_groups=1,
            if_act=True,
            name='conv1x1')
        x = self.conv_bn_layer(
            x,
            filter_size=(7, 6),
            num_filters=out_c,
            stride=1,
            padding=0,
            num_groups=out_c,
            if_act=False,
            name='global_dw_conv7x7')
W
whs 已提交
145
        x = paddle.static.nn.conv2d(
X
xiteng1988 已提交
146 147 148 149 150 151 152 153
            x,
            num_filters=128,
            filter_size=1,
            stride=1,
            padding=0,
            groups=1,
            act=None,
            use_cudnn=True,
W
whs 已提交
154
            param_attr=paddle.ParamAttr(
X
xiteng1988 已提交
155
                name='linear_conv1x1_weights',
W
whs 已提交
156
                initializer=KaimingUniform(),
X
xiteng1988 已提交
157 158 159
                regularizer=fluid.regularizer.L2Decay(4e-4)),
            bias_attr=False)
        bn_name = 'linear_conv1x1_bn'
W
whs 已提交
160
        x = paddle.static.nn.batch_norm(
X
xiteng1988 已提交
161
            x,
W
whs 已提交
162 163
            param_attr=paddle.ParamAttr(name=bn_name + "_scale"),
            bias_attr=paddle.ParamAttr(name=bn_name + "_offset"),
X
xiteng1988 已提交
164 165 166
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')

W
whs 已提交
167
        x = paddle.reshape(x, shape=[x.shape[0], x.shape[1]])
X
xiteng1988 已提交
168 169 170 171 172 173

        if self.extract_feature:
            return x

        out = self.arc_margin_product(
            x, label, self.class_dim, s=32.0, m=0.50, mode=2)
W
whs 已提交
174
        softmax = paddle.nn.functional.softmax(input=out)
W
whs 已提交
175 176
        cost = paddle.nn.functional.cross_entropy(input=softmax, label=label)
        loss = paddle.mean(x=cost)
W
whs 已提交
177
        acc = paddle.static.accuracy(input=out, label=label, k=1)
X
xiteng1988 已提交
178 179 180 181 182 183 184 185 186 187 188 189 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
        return loss, acc

    def residual_unit(self,
                      input,
                      num_in_filter,
                      num_out_filter,
                      stride,
                      filter_size,
                      expansion_factor,
                      use_se=False,
                      name=None):

        num_expfilter = int(round(num_in_filter * expansion_factor))
        input_data = input

        expand_conv = self.conv_bn_layer(
            input=input,
            filter_size=1,
            num_filters=num_expfilter,
            stride=1,
            padding=0,
            if_act=True,
            name=name + '_expand')

        depthwise_conv = self.conv_bn_layer(
            input=expand_conv,
            filter_size=filter_size,
            num_filters=num_expfilter,
            stride=stride,
            padding=int((filter_size - 1) // 2),
            if_act=True,
            num_groups=num_expfilter,
            use_cudnn=True,
            name=name + '_depthwise')

        if use_se:
            depthwise_conv = self.se_block(
                input=depthwise_conv,
                num_out_filter=num_expfilter,
                name=name + '_se')

        linear_conv = self.conv_bn_layer(
            input=depthwise_conv,
            filter_size=1,
            num_filters=num_out_filter,
            stride=1,
            padding=0,
            if_act=False,
            name=name + '_linear')
        if num_in_filter != num_out_filter or stride != 1:
            return linear_conv
        else:
W
whs 已提交
230 231 232

            out = paddle.add(x=input_data, y=linear_conv, act=None)
            return paddle.nn.functional.relu(out)
X
xiteng1988 已提交
233 234 235 236 237

    def se_block(self, input, num_out_filter, ratio=4, name=None):
        num_mid_filter = int(num_out_filter // ratio)
        pool = fluid.layers.pool2d(
            input=input, pool_type='avg', global_pooling=True, use_cudnn=False)
W
whs 已提交
238
        conv1 = paddle.static.nn.conv2d(
X
xiteng1988 已提交
239 240 241 242
            input=pool,
            filter_size=1,
            num_filters=num_mid_filter,
            act=None,
W
whs 已提交
243 244
            param_attr=paddle.ParamAttr(name=name + '_1_weights'),
            bias_attr=paddle.ParamAttr(name=name + '_1_offset'))
W
whs 已提交
245
        conv1 = paddle.static.nn.prelu(
X
xiteng1988 已提交
246 247
            conv1,
            mode='channel',
W
whs 已提交
248
            param_attr=paddle.ParamAttr(
X
xiteng1988 已提交
249 250
                name=name + '_prelu',
                regularizer=fluid.regularizer.L2Decay(0.0)))
W
whs 已提交
251
        conv2 = paddle.static.nn.conv2d(
X
xiteng1988 已提交
252 253 254 255
            input=conv1,
            filter_size=1,
            num_filters=num_out_filter,
            act='hard_sigmoid',
W
whs 已提交
256 257
            param_attr=paddle.ParamAttr(name=name + '_2_weights'),
            bias_attr=paddle.ParamAttr(name=name + '_2_offset'))
W
whs 已提交
258
        scale = paddle.multiply(x=input, y=conv2)
X
xiteng1988 已提交
259 260 261 262 263 264 265 266 267 268 269 270
        return scale

    def conv_bn_layer(self,
                      input,
                      filter_size,
                      num_filters,
                      stride,
                      padding,
                      num_groups=1,
                      if_act=True,
                      name=None,
                      use_cudnn=True):
W
whs 已提交
271
        conv = paddle.static.nn.conv2d(
X
xiteng1988 已提交
272 273 274 275 276 277 278 279
            input=input,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=num_groups,
            act=None,
            use_cudnn=use_cudnn,
W
whs 已提交
280 281
            param_attr=paddle.ParamAttr(
                name=name + '_weights', initializer=KaimingUniform()),
X
xiteng1988 已提交
282 283
            bias_attr=False)
        bn_name = name + '_bn'
W
whs 已提交
284
        bn = paddle.static.nn.batch_norm(
X
xiteng1988 已提交
285
            input=conv,
W
whs 已提交
286 287
            param_attr=paddle.ParamAttr(name=bn_name + "_scale"),
            bias_attr=paddle.ParamAttr(name=bn_name + "_offset"),
X
xiteng1988 已提交
288 289 290
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
        if if_act:
W
whs 已提交
291
            return paddle.static.nn.prelu(
X
xiteng1988 已提交
292 293
                bn,
                mode='channel',
W
whs 已提交
294
                param_attr=paddle.ParamAttr(
X
xiteng1988 已提交
295 296 297 298 299
                    name=name + '_prelu',
                    regularizer=fluid.regularizer.L2Decay(0.0)))
        else:
            return bn

W
whs 已提交
300
    def arc_margin_product(self, input, label, out_dim, s=32.0, m=0.50, mode=2):
W
whs 已提交
301 302
        input_norm = paddle.sqrt(paddle.sum(paddle.square(input), dim=1))
        input = paddle.divide(input, input_norm, axis=0)
X
xiteng1988 已提交
303

W
whs 已提交
304
        weight = paddle.static.create_parameter(
X
xiteng1988 已提交
305 306 307
            shape=[out_dim, input.shape[1]],
            dtype='float32',
            name='weight_norm',
W
whs 已提交
308 309
            attr=paddle.ParamAttr(
                initializer=paddle.nn.initializer.Xavier(),
X
xiteng1988 已提交
310 311
                regularizer=fluid.regularizer.L2Decay(4e-4)))

W
whs 已提交
312 313 314
        weight_norm = paddle.sqrt(paddle.sum(paddle.square(weight), dim=1))
        weight = paddle.divide(weight, weight_norm, axis=0)
        weight = paddle.transpose(weight, perm=[1, 0])
X
xiteng1988 已提交
315
        cosine = fluid.layers.mul(input, weight)
W
whs 已提交
316
        sine = paddle.sqrt(1.0 - paddle.square(cosine))
X
xiteng1988 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

        cos_m = math.cos(m)
        sin_m = math.sin(m)
        phi = cosine * cos_m - sine * sin_m

        th = math.cos(math.pi - m)
        mm = math.sin(math.pi - m) * m

        if mode == 1:
            phi = self.paddle_where_more_than(cosine, 0, phi, cosine)
        elif mode == 2:
            phi = self.paddle_where_more_than(cosine, th, phi, cosine - mm)
        else:
            pass

332
        one_hot = fluid.layers.one_hot(input=label, depth=out_dim)
W
whs 已提交
333 334
        output = paddle.multiply(one_hot, phi) + paddle.multiply(
            (1.0 - one_hot), cosine)
X
xiteng1988 已提交
335 336 337 338
        output = output * s
        return output

    def paddle_where_more_than(self, target, limit, x, y):
W
whs 已提交
339 340
        mask = paddle.cast(x=(target > limit), dtype='float32')
        output = paddle.multiply(mask, x) + paddle.multiply((1.0 - mask), y)
X
xiteng1988 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        return output


def SlimFaceNet_A_x0_60(class_dim=None, scale=0.6, arch=None):
    scale = 0.6
    arch = [0, 1, 5, 1, 0, 2, 1, 2, 0, 1, 2, 1, 1, 0, 1]
    return SlimFaceNet(class_dim=class_dim, scale=scale, arch=arch)


def SlimFaceNet_B_x0_75(class_dim=None, scale=0.6, arch=None):
    scale = 0.75
    arch = [1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 3, 2, 2, 3]
    return SlimFaceNet(class_dim=class_dim, scale=scale, arch=arch)


def SlimFaceNet_C_x0_75(class_dim=None, scale=0.6, arch=None):
    scale = 0.75
    arch = [1, 1, 2, 1, 0, 2, 1, 0, 1, 0, 1, 1, 2, 2, 3]
    return SlimFaceNet(class_dim=class_dim, scale=scale, arch=arch)


if __name__ == "__main__":
363
    paddle.enable_static()
W
whs 已提交
364
    x = paddle.static.data(name='x', shape=[-1, 3, 112, 112], dtype='float32')
X
xiteng1988 已提交
365
    print(x.shape)
W
whs 已提交
366 367
    model = SlimFaceNet(
        10000, arch=[1, 3, 3, 1, 1, 0, 0, 1, 0, 1, 1, 0, 5, 5, 3])
X
xiteng1988 已提交
368
    y = model.net(x)