retinanet.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import paddle
  20. __all__ = ['RetinaNet']
  21. @register
  22. class RetinaNet(BaseArch):
  23. __category__ = 'architecture'
  24. def __init__(self, backbone, neck, head):
  25. super(RetinaNet, self).__init__()
  26. self.backbone = backbone
  27. self.neck = neck
  28. self.head = head
  29. @classmethod
  30. def from_config(cls, cfg, *args, **kwargs):
  31. backbone = create(cfg['backbone'])
  32. kwargs = {'input_shape': backbone.out_shape}
  33. neck = create(cfg['neck'], **kwargs)
  34. kwargs = {'input_shape': neck.out_shape}
  35. head = create(cfg['head'], **kwargs)
  36. return {
  37. 'backbone': backbone,
  38. 'neck': neck,
  39. 'head': head,
  40. }
  41. def _forward(self):
  42. body_feats = self.backbone(self.inputs)
  43. neck_feats = self.neck(body_feats)
  44. if self.training:
  45. return self.head(neck_feats, self.inputs)
  46. else:
  47. head_outs = self.head(neck_feats)
  48. bbox, bbox_num = self.head.post_process(
  49. head_outs, self.inputs['im_shape'], self.inputs['scale_factor'])
  50. return {'bbox': bbox, 'bbox_num': bbox_num}
  51. def get_loss(self):
  52. return self._forward()
  53. def get_pred(self):
  54. return self._forward()