ssd.py 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 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.

Q
qingqing01 已提交
15 16 17 18
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

19
from ppdet.core.workspace import register, create
Q
qingqing01 已提交
20 21 22 23 24 25 26 27
from .meta_arch import BaseArch

__all__ = ['SSD']


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

30
    def __init__(self, backbone, ssd_head, post_process):
Q
qingqing01 已提交
31 32 33 34 35
        super(SSD, self).__init__()
        self.backbone = backbone
        self.ssd_head = ssd_head
        self.post_process = post_process

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    @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 已提交
51 52 53 54
        # Backbone
        body_feats = self.backbone(self.inputs)

        # SSD Head
55 56 57 58 59
        if self.training:
            return self.ssd_head(body_feats, self.inputs['image'],
                                 self.inputs['gt_bbox'],
                                 self.inputs['gt_class'])
        else:
K
Kaipeng Deng 已提交
60 61
            preds, anchors = self.ssd_head(body_feats, self.inputs['image'])
            bbox, bbox_num = self.post_process(preds, anchors,
62 63 64
                                               self.inputs['im_shape'],
                                               self.inputs['scale_factor'])
            return bbox, bbox_num
Q
qingqing01 已提交
65 66

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

G
Guanghua Yu 已提交
69
    def get_pred(self):
70 71
        bbox_pred, bbox_num = self._forward()
        output = {
72 73
            "bbox": bbox_pred,
            "bbox_num": bbox_num,
74 75
        }
        return output