picodet.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (c) 2021 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. import paddle
  18. from ppdet.core.workspace import register, create
  19. from .meta_arch import BaseArch
  20. __all__ = ['PicoDet']
  21. @register
  22. class PicoDet(BaseArch):
  23. """
  24. Generalized Focal Loss network, see https://arxiv.org/abs/2006.04388
  25. Args:
  26. backbone (object): backbone instance
  27. neck (object): 'FPN' instance
  28. head (object): 'PicoHead' instance
  29. """
  30. __category__ = 'architecture'
  31. def __init__(self, backbone, neck, head='PicoHead'):
  32. super(PicoDet, self).__init__()
  33. self.backbone = backbone
  34. self.neck = neck
  35. self.head = head
  36. self.export_post_process = True
  37. self.export_nms = True
  38. @classmethod
  39. def from_config(cls, cfg, *args, **kwargs):
  40. backbone = create(cfg['backbone'])
  41. kwargs = {'input_shape': backbone.out_shape}
  42. neck = create(cfg['neck'], **kwargs)
  43. kwargs = {'input_shape': neck.out_shape}
  44. head = create(cfg['head'], **kwargs)
  45. return {
  46. 'backbone': backbone,
  47. 'neck': neck,
  48. "head": head,
  49. }
  50. def _forward(self):
  51. body_feats = self.backbone(self.inputs)
  52. fpn_feats = self.neck(body_feats)
  53. head_outs = self.head(fpn_feats, self.export_post_process)
  54. if self.training or not self.export_post_process:
  55. return head_outs, None
  56. else:
  57. scale_factor = self.inputs['scale_factor']
  58. bboxes, bbox_num = self.head.post_process(
  59. head_outs, scale_factor, export_nms=self.export_nms)
  60. return bboxes, bbox_num
  61. def get_loss(self, ):
  62. loss = {}
  63. head_outs, _ = self._forward()
  64. loss_gfl = self.head.get_loss(head_outs, self.inputs)
  65. loss.update(loss_gfl)
  66. total_loss = paddle.add_n(list(loss.values()))
  67. loss.update({'loss': total_loss})
  68. return loss
  69. def get_pred(self):
  70. if not self.export_post_process:
  71. return {'picodet': self._forward()[0]}
  72. elif self.export_nms:
  73. bbox_pred, bbox_num = self._forward()
  74. output = {'bbox': bbox_pred, 'bbox_num': bbox_num}
  75. return output
  76. else:
  77. bboxes, mlvl_scores = self._forward()
  78. output = {'bbox': bboxes, 'scores': mlvl_scores}
  79. return output