rpn_head.py 4.0 KB
Newer Older
F
FDInSky 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
import paddle.fluid as fluid
from paddle.fluid.dygraph import Layer
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.initializer import Normal
from paddle.fluid.regularizer import L2Decay
from paddle.fluid.dygraph.nn import Conv2D
from ppdet.core.workspace import register


@register
class RPNFeat(Layer):
    def __init__(self, feat_in=1024, feat_out=1024):
        super(RPNFeat, self).__init__()
14
        # rpn feat is shared with each level
15
        self.rpn_conv = Conv2D(
16 17
            num_channels=feat_in,
            num_filters=feat_out,
F
FDInSky 已提交
18 19 20 21
            filter_size=3,
            padding=1,
            act='relu',
            param_attr=ParamAttr(
22 23
                #name="conv_rpn_fpn2_w", 
                initializer=Normal(
F
FDInSky 已提交
24 25
                    loc=0., scale=0.01)),
            bias_attr=ParamAttr(
26 27 28
                #name="conv_rpn_fpn2_b",
                learning_rate=2.,
                regularizer=L2Decay(0.)))
F
FDInSky 已提交
29

30 31 32 33 34
    def forward(self, inputs, feats):
        rpn_feats = []
        for feat in feats:
            rpn_feats.append(self.rpn_conv(feat))
        return rpn_feats
F
FDInSky 已提交
35 36 37 38 39 40


@register
class RPNHead(Layer):
    __inject__ = ['rpn_feat']

41
    def __init__(self, rpn_feat, anchor_per_position=15, rpn_channel=1024):
F
FDInSky 已提交
42 43
        super(RPNHead, self).__init__()
        self.rpn_feat = rpn_feat
44 45 46
        if isinstance(rpn_feat, dict):
            self.rpn_feat = RPNFeat(**rpn_feat)
        # rpn head is shared with each level
F
FDInSky 已提交
47
        # rpn roi classification scores
48
        self.rpn_rois_score = Conv2D(
49 50
            num_channels=rpn_channel,
            num_filters=anchor_per_position,
F
FDInSky 已提交
51 52 53 54
            filter_size=1,
            padding=0,
            act=None,
            param_attr=ParamAttr(
55 56
                #name="rpn_cls_logits_fpn2_w",
                initializer=Normal(
F
FDInSky 已提交
57 58
                    loc=0., scale=0.01)),
            bias_attr=ParamAttr(
59
                #name="rpn_cls_logits_fpn2_b",
F
FDInSky 已提交
60 61 62 63
                learning_rate=2.,
                regularizer=L2Decay(0.)))

        # rpn roi bbox regression deltas
64
        self.rpn_rois_delta = Conv2D(
65 66
            num_channels=rpn_channel,
            num_filters=4 * anchor_per_position,
F
FDInSky 已提交
67 68 69 70
            filter_size=1,
            padding=0,
            act=None,
            param_attr=ParamAttr(
71 72
                #name="rpn_bbox_pred_fpn2_w", 
                initializer=Normal(
F
FDInSky 已提交
73 74
                    loc=0., scale=0.01)),
            bias_attr=ParamAttr(
75
                #name="rpn_bbox_pred_fpn2_b",
F
FDInSky 已提交
76 77 78
                learning_rate=2.,
                regularizer=L2Decay(0.)))

79 80 81 82 83 84 85 86
    def forward(self, inputs, feats):
        rpn_feats = self.rpn_feat(inputs, feats)
        rpn_head_out = []
        for rpn_feat in rpn_feats:
            rrs = self.rpn_rois_score(rpn_feat)
            rrd = self.rpn_rois_delta(rpn_feat)
            rpn_head_out.append((rrs, rrd))
        return rpn_feats, rpn_head_out
F
FDInSky 已提交
87

K
Kaipeng Deng 已提交
88
    def get_loss(self, loss_inputs):
F
FDInSky 已提交
89 90
        # cls loss
        score_tgt = fluid.layers.cast(
91 92 93 94 95 96
            x=loss_inputs['rpn_score_target'], dtype='float32')
        score_tgt.stop_gradient = True
        loss_rpn_cls = fluid.layers.sigmoid_cross_entropy_with_logits(
            x=loss_inputs['rpn_score_pred'], label=score_tgt)
        loss_rpn_cls = fluid.layers.reduce_mean(
            loss_rpn_cls, name='loss_rpn_cls')
F
FDInSky 已提交
97 98

        # reg loss
99 100 101 102 103 104
        loc_tgt = fluid.layers.cast(
            x=loss_inputs['rpn_rois_target'], dtype='float32')
        loc_tgt.stop_gradient = True
        loss_rpn_reg = fluid.layers.smooth_l1(
            x=loss_inputs['rpn_rois_pred'],
            y=loc_tgt,
F
FDInSky 已提交
105
            sigma=3.0,
106 107 108 109 110 111 112 113
            inside_weight=loss_inputs['rpn_rois_weight'],
            outside_weight=loss_inputs['rpn_rois_weight'])
        loss_rpn_reg = fluid.layers.reduce_sum(loss_rpn_reg)
        score_shape = fluid.layers.shape(score_tgt)
        score_shape = fluid.layers.cast(x=score_shape, dtype='float32')
        norm = fluid.layers.reduce_prod(score_shape)
        norm.stop_gradient = True
        loss_rpn_reg = loss_rpn_reg / norm
F
FDInSky 已提交
114

115
        return {'loss_rpn_cls': loss_rpn_cls, 'loss_rpn_reg': loss_rpn_reg}