faster_rcnn.py 2.3 KB
Newer Older
F
FDInSky 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from paddle import fluid
from ppdet.core.workspace import register
from .meta_arch import BaseArch

__all__ = ['FasterRCNN']


@register
class FasterRCNN(BaseArch):
    __category__ = 'architecture'
    __inject__ = [
        'anchor',
        'proposal',
        'backbone',
        'rpn_head',
        'bbox_head',
    ]

23 24 25
    def __init__(self, anchor, proposal, backbone, rpn_head, bbox_head, *args,
                 **kwargs):
        super(FasterRCNN, self).__init__(*args, **kwargs)
F
FDInSky 已提交
26 27 28 29 30 31
        self.anchor = anchor
        self.proposal = proposal
        self.backbone = backbone
        self.rpn_head = rpn_head
        self.bbox_head = bbox_head

32
    def model_arch(self, ):
F
FDInSky 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
        # Backbone
        bb_out = self.backbone(self.gbd)
        self.gbd.update(bb_out)

        # RPN
        rpn_head_out = self.rpn_head(self.gbd)
        self.gbd.update(rpn_head_out)

        # Anchor
        anchor_out = self.anchor(self.gbd)
        self.gbd.update(anchor_out)

        # Proposal BBox
46
        self.gbd['stage'] = 0
F
FDInSky 已提交
47
        proposal_out = self.proposal(self.gbd)
48
        self.gbd.update({'proposal_0': proposal_out})
F
FDInSky 已提交
49 50

        # BBox Head
51 52
        bboxhead_out = self.bbox_head(self.gbd)
        self.gbd.update({'bbox_head_0': bboxhead_out})
F
FDInSky 已提交
53 54 55 56 57

        if self.gbd['mode'] == 'infer':
            bbox_out = self.proposal.post_process(self.gbd)
            self.gbd.update(bbox_out)

58 59 60
    def loss(self, ):
        rpn_cls_loss, rpn_reg_loss = self.rpn_head.loss(self.gbd)
        bbox_cls_loss, bbox_reg_loss = self.bbox_head.loss(self.gbd)
F
FDInSky 已提交
61 62 63 64 65 66 67 68 69 70 71
        losses = [rpn_cls_loss, rpn_reg_loss, bbox_cls_loss, bbox_reg_loss]
        loss = fluid.layers.sum(losses)
        out = {
            'loss': loss,
            'loss_rpn_cls': rpn_cls_loss,
            'loss_rpn_reg': rpn_reg_loss,
            'loss_bbox_cls': bbox_cls_loss,
            'loss_bbox_reg': bbox_reg_loss,
        }
        return out

72
    def infer(self, ):
F
FDInSky 已提交
73
        outs = {
74 75 76 77
            "bbox": self.gbd['predicted_bbox'].numpy(),
            "bbox_nums": self.gbd['predicted_bbox_nums'].numpy(),
            'im_id': self.gbd['im_id'].numpy(),
            'im_shape': self.gbd['im_shape'].numpy()
F
FDInSky 已提交
78 79
        }
        return outs