ssd.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from ppdet.core.workspace import register, create
  18. from .meta_arch import BaseArch
  19. __all__ = ['SSD']
  20. @register
  21. class SSD(BaseArch):
  22. """
  23. Single Shot MultiBox Detector, see https://arxiv.org/abs/1512.02325
  24. Args:
  25. backbone (nn.Layer): backbone instance
  26. ssd_head (nn.Layer): `SSDHead` instance
  27. post_process (object): `BBoxPostProcess` instance
  28. """
  29. __category__ = 'architecture'
  30. __inject__ = ['post_process']
  31. def __init__(self, backbone, ssd_head, post_process, r34_backbone=False):
  32. super(SSD, self).__init__()
  33. self.backbone = backbone
  34. self.ssd_head = ssd_head
  35. self.post_process = post_process
  36. self.r34_backbone = r34_backbone
  37. if self.r34_backbone:
  38. from ppdet.modeling.backbones.resnet import ResNet
  39. assert isinstance(self.backbone, ResNet) and \
  40. self.backbone.depth == 34, \
  41. "If you set r34_backbone=True, please use ResNet-34 as backbone."
  42. self.backbone.res_layers[2].blocks[0].branch2a.conv._stride = [1, 1]
  43. self.backbone.res_layers[2].blocks[0].short.conv._stride = [1, 1]
  44. @classmethod
  45. def from_config(cls, cfg, *args, **kwargs):
  46. # backbone
  47. backbone = create(cfg['backbone'])
  48. # head
  49. kwargs = {'input_shape': backbone.out_shape}
  50. ssd_head = create(cfg['ssd_head'], **kwargs)
  51. return {
  52. 'backbone': backbone,
  53. "ssd_head": ssd_head,
  54. }
  55. def _forward(self):
  56. # Backbone
  57. body_feats = self.backbone(self.inputs)
  58. # SSD Head
  59. if self.training:
  60. return self.ssd_head(body_feats, self.inputs['image'],
  61. self.inputs['gt_bbox'],
  62. self.inputs['gt_class'])
  63. else:
  64. preds, anchors = self.ssd_head(body_feats, self.inputs['image'])
  65. bbox, bbox_num = self.post_process(preds, anchors,
  66. self.inputs['im_shape'],
  67. self.inputs['scale_factor'])
  68. return bbox, bbox_num
  69. def get_loss(self, ):
  70. return {"loss": self._forward()}
  71. def get_pred(self):
  72. bbox_pred, bbox_num = self._forward()
  73. output = {
  74. "bbox": bbox_pred,
  75. "bbox_num": bbox_num,
  76. }
  77. return output