blazeface.py 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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

19
from collections import OrderedDict
20

21
from paddle import fluid
22 23 24 25 26
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.regularizer import L2Decay

from ppdet.core.workspace import register
from ppdet.modeling.ops import SSDOutputDecoder
27
from ppdet.modeling.losses import SSDWithLmkLoss
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

__all__ = ['BlazeFace']


@register
class BlazeFace(object):
    """
    BlazeFace: Sub-millisecond Neural Face Detection on Mobile GPUs,
               see https://arxiv.org/abs/1907.05047

    Args:
        backbone (object): backbone instance
        output_decoder (object): `SSDOutputDecoder` instance
        min_sizes (list|None): min sizes of generated prior boxes.
        max_sizes (list|None): max sizes of generated prior boxes. Default: None.
43
        steps (list|None): step size of adjacent prior boxes on each feature map.
44 45 46 47 48 49 50 51 52
        num_classes (int): number of output classes
        use_density_prior_box (bool): whether or not use density_prior_box
            instead of prior_box
        densities (list|None): the densities of generated density prior boxes,
            this attribute should be a list or tuple of integers
    """

    __category__ = 'architecture'
    __inject__ = ['backbone', 'output_decoder']
53
    __shared__ = ['num_classes', 'with_lmk']
54 55 56 57 58 59 60 61 62

    def __init__(self,
                 backbone="BlazeNet",
                 output_decoder=SSDOutputDecoder().__dict__,
                 min_sizes=[[16., 24.], [32., 48., 64., 80., 96., 128.]],
                 max_sizes=None,
                 steps=[8., 16.],
                 num_classes=2,
                 use_density_prior_box=False,
63 64 65
                 densities=[[2, 2], [2, 1, 1, 1, 1, 1]],
                 with_lmk=False,
                 lmk_loss=SSDWithLmkLoss().__dict__):
66 67 68
        super(BlazeFace, self).__init__()
        self.backbone = backbone
        self.num_classes = num_classes
69
        self.with_lmk = with_lmk
70 71
        self.output_decoder = output_decoder
        if isinstance(output_decoder, dict):
72 73
            if self.with_lmk:
                output_decoder['return_index'] = True
74 75 76 77 78 79
            self.output_decoder = SSDOutputDecoder(**output_decoder)
        self.min_sizes = min_sizes
        self.max_sizes = max_sizes
        self.steps = steps
        self.use_density_prior_box = use_density_prior_box
        self.densities = densities
80 81 82
        self.landmark = None
        if self.with_lmk and isinstance(lmk_loss, dict):
            self.lmk_loss = SSDWithLmkLoss(**lmk_loss)
83 84 85 86 87 88 89 90 91 92 93 94

    def build(self, feed_vars, mode='train'):
        im = feed_vars['image']

        body_feats = self.backbone(im)
        locs, confs, box, box_var = self._multi_box_head(
            inputs=body_feats,
            image=im,
            num_classes=self.num_classes,
            use_density_prior_box=self.use_density_prior_box)

        if mode == 'train':
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            gt_bbox = feed_vars['gt_bbox']
            gt_class = feed_vars['gt_class']
            if self.with_lmk:
                lmk_labels = feed_vars['gt_keypoint']
                lmk_ignore_flag = feed_vars["keypoint_ignore"]
                loss = self.lmk_loss(locs, confs, gt_bbox, gt_class,
                                     self.landmark, lmk_labels, lmk_ignore_flag,
                                     box, box_var)
            else:
                loss = fluid.layers.ssd_loss(
                    locs,
                    confs,
                    gt_bbox,
                    gt_class,
                    box,
                    box_var,
                    overlap_threshold=0.35,
                    neg_overlap=0.35)

114 115 116
            loss = fluid.layers.reduce_sum(loss)
            return {'loss': loss}
        else:
117 118 119 120 121 122 123 124 125 126 127 128
            if self.with_lmk:
                pred, face_index = self.output_decoder(locs, confs, box,
                                                       box_var)
                return {
                    'bbox': pred,
                    'face_index': face_index,
                    'prior_boxes': box,
                    'landmark': self.landmark
                }
            else:
                pred = self.output_decoder(locs, confs, box, box_var)
                return {'bbox': pred}
129 130 131 132 133 134 135 136

    def _multi_box_head(self,
                        inputs,
                        image,
                        num_classes=2,
                        use_density_prior_box=False):
        def permute_and_reshape(input, last_dim):
            trans = fluid.layers.transpose(input, perm=[0, 2, 3, 1])
137
            compile_shape = [0, -1, last_dim]
138 139 140 141
            return fluid.layers.reshape(trans, shape=compile_shape)

        locs, confs = [], []
        boxes, vars = [], []
142
        lmk_locs = []
143 144 145 146 147 148 149 150 151 152 153 154 155 156
        b_attr = ParamAttr(learning_rate=2., regularizer=L2Decay(0.))

        for i, input in enumerate(inputs):
            min_size = self.min_sizes[i]

            if use_density_prior_box:
                densities = self.densities[i]
                box, var = fluid.layers.density_prior_box(
                    input,
                    image,
                    densities=densities,
                    fixed_sizes=min_size,
                    fixed_ratios=[1.],
                    clip=False,
W
wangguanzhong 已提交
157 158
                    offset=0.5,
                    steps=[self.steps[i]] * 2)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            else:
                box, var = fluid.layers.prior_box(
                    input,
                    image,
                    min_sizes=min_size,
                    max_sizes=None,
                    steps=[self.steps[i]] * 2,
                    aspect_ratios=[1.],
                    clip=False,
                    flip=False,
                    offset=0.5)

            num_boxes = box.shape[2]

            box = fluid.layers.reshape(box, shape=[-1, 4])
            var = fluid.layers.reshape(var, shape=[-1, 4])
            num_loc_output = num_boxes * 4
            num_conf_output = num_boxes * num_classes
            # get loc
            mbox_loc = fluid.layers.conv2d(
                input, num_loc_output, 3, 1, 1, bias_attr=b_attr)
            loc = permute_and_reshape(mbox_loc, 4)
            # get conf
            mbox_conf = fluid.layers.conv2d(
                input, num_conf_output, 3, 1, 1, bias_attr=b_attr)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            conf = permute_and_reshape(mbox_conf, num_classes)

            if self.with_lmk:
                # get landmark
                lmk_loc_output = num_boxes * 10
                lmk_box_loc = fluid.layers.conv2d(
                    input,
                    lmk_loc_output,
                    3,
                    1,
                    1,
                    param_attr=ParamAttr(name='lmk' + str(i) + '_weights'),
                    bias_attr=False)
                lmk_loc = permute_and_reshape(lmk_box_loc, 10)
                lmk_locs.append(lmk_loc)
199 200 201 202 203 204 205 206 207 208

            locs.append(loc)
            confs.append(conf)
            boxes.append(box)
            vars.append(var)

        face_mbox_loc = fluid.layers.concat(locs, axis=1)
        face_mbox_conf = fluid.layers.concat(confs, axis=1)
        prior_boxes = fluid.layers.concat(boxes)
        box_vars = fluid.layers.concat(vars)
209 210
        if self.with_lmk:
            self.landmark = fluid.layers.concat(lmk_locs, axis=1)
211 212
        return face_mbox_loc, face_mbox_conf, prior_boxes, box_vars

213 214 215 216 217
    def _inputs_def(self, image_shape):
        im_shape = [None] + image_shape
        # yapf: disable
        inputs_def = {
            'image':    {'shape': im_shape,  'dtype': 'float32', 'lod_level': 0},
Q
qingqing01 已提交
218
            'im_id':    {'shape': [None, 1], 'dtype': 'int64',   'lod_level': 0},
219 220 221
            'gt_bbox':  {'shape': [None, 4], 'dtype': 'float32', 'lod_level': 1},
            'gt_class': {'shape': [None, 1], 'dtype': 'int32',   'lod_level': 1},
            'im_shape': {'shape': [None, 3], 'dtype': 'int32',   'lod_level': 0},
222 223
            'gt_keypoint':  {'shape': [None, 10], 'dtype': 'float32', 'lod_level': 1},
            'keypoint_ignore': {'shape': [None, 1], 'dtype': 'float32',   'lod_level': 1},
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        }
        # yapf: enable
        return inputs_def

    def build_inputs(
            self,
            image_shape=[3, None, None],
            fields=['image', 'im_id', 'gt_bbox', 'gt_class'],  # for train
            use_dataloader=True,
            iterable=False):
        inputs_def = self._inputs_def(image_shape)
        feed_vars = OrderedDict([(key, fluid.data(
            name=key,
            shape=inputs_def[key]['shape'],
            dtype=inputs_def[key]['dtype'],
            lod_level=inputs_def[key]['lod_level'])) for key in fields])
        loader = fluid.io.DataLoader.from_generator(
            feed_list=list(feed_vars.values()),
242
            capacity=16,
243 244 245 246
            use_double_buffer=True,
            iterable=iterable) if use_dataloader else None
        return feed_vars, loader

247 248 249 250 251 252
    def train(self, feed_vars):
        return self.build(feed_vars, 'train')

    def eval(self, feed_vars):
        return self.build(feed_vars, 'eval')

K
Kaipeng Deng 已提交
253 254 255
    def test(self, feed_vars, exclude_nms=False):
        assert not exclude_nms, "exclude_nms for {} is not support currently".format(
            self.__class__.__name__)
256 257 258 259
        return self.build(feed_vars, 'test')

    def is_bbox_normalized(self):
        return True