faster_rcnn_task.py 8.0 KB
Newer Older
W
wuzewu 已提交
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 27 28 29 30 31
#coding:utf-8
#  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 paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.regularizer import L2Decay
from paddle.fluid.initializer import Normal

from paddlehub.common.paddle_helper import clone_program
from paddlehub.finetune.task.detection_task import DetectionTask


class FasterRCNNTask(DetectionTask):
    def __init__(self,
                 num_classes,
W
wuzewu 已提交
32 33 34
                 data_reader,
                 feature=None,
                 feed_list=None,
W
wuzewu 已提交
35
                 predict_feature=None,
W
wuzewu 已提交
36
                 predict_feed_list=None,
W
wuzewu 已提交
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
                 startup_program=None,
                 config=None,
                 metrics_choices="default"):
        super(FasterRCNNTask, self).__init__(
            data_reader=data_reader,
            num_classes=num_classes,
            feed_list=feed_list,
            feature=feature,
            model_type='rcnn',
            startup_program=startup_program,
            config=config,
            metrics_choices=metrics_choices)

        self._base_feed_list = feed_list
        self._base_predict_feed_list = predict_feed_list
        self.feature = feature
        self.predict_feature = predict_feature
        self.num_classes = num_classes
        if predict_feature:
            self._base_predict_main_program = clone_program(
                predict_feature[0].block.program, for_test=False)
        else:
            self._base_predict_main_program = None

    def _build_net(self):
        if self.is_train_phase:
            head_feat = self.feature[0]
        else:
            if self.is_predict_phase:
                self.env.labels = self._add_label()
            head_feat = self.main_program.global_block().vars[
                self.predict_feature[0].name]

        cls_score = fluid.layers.fc(
            input=head_feat,
            size=self.num_classes,
            act=None,
W
wuzewu 已提交
74
            name='paddlehub_rcnn_cls_score',
W
wuzewu 已提交
75
            param_attr=ParamAttr(
W
wuzewu 已提交
76 77
                name='paddlehub_rcnn_cls_score_weights',
                initializer=Normal(loc=0.0, scale=0.01)),
W
wuzewu 已提交
78
            bias_attr=ParamAttr(
W
wuzewu 已提交
79
                name='paddlehub_rcnn_cls_score_bias',
W
wuzewu 已提交
80 81 82 83 84 85
                learning_rate=2.,
                regularizer=L2Decay(0.)))
        bbox_pred = fluid.layers.fc(
            input=head_feat,
            size=4 * self.num_classes,
            act=None,
W
wuzewu 已提交
86
            name='paddlehub_rcnn_bbox_pred',
W
wuzewu 已提交
87
            param_attr=ParamAttr(
W
wuzewu 已提交
88 89
                name='paddlehub_rcnn_bbox_pred_weights',
                initializer=Normal(loc=0.0, scale=0.001)),
W
wuzewu 已提交
90
            bias_attr=ParamAttr(
W
wuzewu 已提交
91
                name='paddlehub_rcnn_bbox_pred_bias',
W
wuzewu 已提交
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 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
                learning_rate=2.,
                regularizer=L2Decay(0.)))

        if self.is_train_phase:
            rpn_cls_loss, rpn_reg_loss, outs = self.feature[1:]
            labels_int32 = outs[1]
            bbox_targets = outs[2]
            bbox_inside_weights = outs[3]
            bbox_outside_weights = outs[4]
            labels_int64 = fluid.layers.cast(x=labels_int32, dtype='int64')
            labels_int64.stop_gradient = True
            loss_cls = fluid.layers.softmax_with_cross_entropy(
                logits=cls_score, label=labels_int64, numeric_stable_mode=True)
            loss_cls = fluid.layers.reduce_mean(loss_cls)
            loss_bbox = fluid.layers.smooth_l1(
                x=bbox_pred,
                y=bbox_targets,
                inside_weight=bbox_inside_weights,
                outside_weight=bbox_outside_weights,
                sigma=1.0)
            loss_bbox = fluid.layers.reduce_mean(loss_bbox)
            total_loss = fluid.layers.sum(
                [loss_bbox, loss_cls, rpn_cls_loss, rpn_reg_loss])
            return [total_loss]
        else:
            rois = self.main_program.global_block().vars[
                self.predict_feature[1].name]
            im_info = self.feed_var_list[1]
            im_shape = self.feed_var_list[3]
            im_scale = fluid.layers.slice(im_info, [1], starts=[2], ends=[3])
            im_scale = fluid.layers.sequence_expand(im_scale, rois)
            boxes = rois / im_scale
            cls_prob = fluid.layers.softmax(cls_score, use_cudnn=False)
            bbox_pred = fluid.layers.reshape(bbox_pred,
                                             (-1, self.num_classes, 4))
            # decoded_box = self.box_coder(prior_box=boxes, target_box=bbox_pred)
            decoded_box = fluid.layers.box_coder(
                prior_box=boxes,
                prior_box_var=[0.1, 0.1, 0.2, 0.2],
                target_box=bbox_pred,
                code_type='decode_center_size',
                box_normalized=False,
                axis=1)
            cliped_box = fluid.layers.box_clip(
                input=decoded_box, im_info=im_shape)
            # pred_result = self.nms(bboxes=cliped_box, scores=cls_prob)
            pred_result = fluid.layers.multiclass_nms(
                bboxes=decoded_box,
                scores=cls_prob,
                score_threshold=.05,
                nms_top_k=-1,
                keep_top_k=100,
                nms_threshold=.5,
                normalized=False,
                nms_eta=1.0,
                background_label=0)
            return [pred_result]

    def _add_label(self):
        if self.is_train_phase:
            # 'im_id'
            idx_list = [2]
        elif self.is_test_phase:
            # 'im_id', 'gt_box', 'gt_label', 'is_difficult'
            idx_list = [2, 4, 5, 6]
        else:  # predict
            idx_list = [2]
        return self._add_label_by_fields(idx_list)

    def _add_loss(self):
        if self.is_train_phase:
            loss = self.env.outputs[-1]
        else:
            loss = fluid.layers.fill_constant(
                shape=[1], value=-1, dtype='float32')
        return loss

    def _feed_list(self, for_export=False):
        if self.is_train_phase:
            feed_list = [varname for varname in self._base_feed_list]
        else:
            feed_list = [varname for varname in self._base_predict_feed_list]

        if self.is_train_phase:
            # feed_list is ['image', 'im_info', 'gt_box', 'gt_label', 'is_crowd']
            return feed_list[:2] + [self.labels[0].name] + feed_list[2:]
        elif self.is_test_phase:
            # feed list is ['image', 'im_info', 'im_shape']
            return feed_list[:2] + [self.labels[0].name] + feed_list[2:] + \
                   [label.name for label in self.labels[1:]]
        if for_export:
            # skip im_id
            return feed_list[:2] + feed_list[3:]
        else:
            return feed_list[:2] + [self.labels[0].name] + feed_list[2:]

    def _fetch_list(self, for_export=False):
        # ensure fetch 'im_shape', 'im_id', 'bbox' at first three elements in test phase
        if self.is_train_phase:
            return [self.loss.name]
        elif self.is_test_phase:
            # im_shape, im_id, bbox
            return [
                self.feed_list[2], self.labels[0].name, self.outputs[0].name,
                self.loss.name
            ]

        # im_shape, im_id, bbox
        if for_export:
            return [self.outputs[0].name]
        else:
            return [
                self.feed_list[2], self.labels[0].name, self.outputs[0].name
            ]

    @property
    def base_main_program(self):
        if self.is_train_phase:
            return self._base_main_program
        return self._base_predict_main_program