ssd.py 1.9 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

5
from ppdet.core.workspace import register, create
Q
qingqing01 已提交
6 7 8 9 10 11 12 13
from .meta_arch import BaseArch

__all__ = ['SSD']


@register
class SSD(BaseArch):
    __category__ = 'architecture'
14
    __inject__ = ['post_process']
Q
qingqing01 已提交
15

16
    def __init__(self, backbone, ssd_head, post_process):
Q
qingqing01 已提交
17 18 19 20 21
        super(SSD, self).__init__()
        self.backbone = backbone
        self.ssd_head = ssd_head
        self.post_process = post_process

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    @classmethod
    def from_config(cls, cfg, *args, **kwargs):
        # backbone
        backbone = create(cfg['backbone'])

        # head
        kwargs = {'input_shape': backbone.out_shape}
        ssd_head = create(cfg['ssd_head'], **kwargs)

        return {
            'backbone': backbone,
            "ssd_head": ssd_head,
        }

    def _forward(self):
Q
qingqing01 已提交
37 38 39 40
        # Backbone
        body_feats = self.backbone(self.inputs)

        # SSD Head
41 42 43 44 45
        if self.training:
            return self.ssd_head(body_feats, self.inputs['image'],
                                 self.inputs['gt_bbox'],
                                 self.inputs['gt_class'])
        else:
K
Kaipeng Deng 已提交
46 47
            preds, anchors = self.ssd_head(body_feats, self.inputs['image'])
            bbox, bbox_num = self.post_process(preds, anchors,
48 49 50
                                               self.inputs['im_shape'],
                                               self.inputs['scale_factor'])
            return bbox, bbox_num
Q
qingqing01 已提交
51 52

    def get_loss(self, ):
53
        return {"loss": self._forward()}
Q
qingqing01 已提交
54

G
Guanghua Yu 已提交
55
    def get_pred(self):
56 57 58 59 60 61 62 63 64 65 66
        bbox_pred, bbox_num = self._forward()
        label = bbox_pred[:, 0]
        score = bbox_pred[:, 1]
        bbox = bbox_pred[:, 2:]
        output = {
            'bbox': bbox,
            'score': score,
            'label': label,
            'bbox_num': bbox_num
        }
        return output