ssd.py 1.7 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 46 47 48 49 50 51
        if self.training:
            return self.ssd_head(body_feats, self.inputs['image'],
                                 self.inputs['gt_bbox'],
                                 self.inputs['gt_class'])
        else:
            boxes, scores, anchors = self.ssd_head(body_feats,
                                                   self.inputs['image'])
            bbox, bbox_num = self.post_process((boxes, scores), anchors,
                                               self.inputs['im_shape'],
                                               self.inputs['scale_factor'])
            return bbox, bbox_num
Q
qingqing01 已提交
52 53

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

G
Guanghua Yu 已提交
56
    def get_pred(self):
57
        return dict(zip(['bbox', 'bbox_num'], self._forward()))