s2anet.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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__ = ['S2ANet']
  21. @register
  22. class S2ANet(BaseArch):
  23. __category__ = 'architecture'
  24. __inject__ = [
  25. 's2anet_head',
  26. 's2anet_bbox_post_process',
  27. ]
  28. def __init__(self, backbone, neck, s2anet_head, s2anet_bbox_post_process):
  29. """
  30. S2ANet, see https://arxiv.org/pdf/2008.09397.pdf
  31. Args:
  32. backbone (object): backbone instance
  33. neck (object): `FPN` instance
  34. s2anet_head (object): `S2ANetHead` instance
  35. s2anet_bbox_post_process (object): `S2ANetBBoxPostProcess` instance
  36. """
  37. super(S2ANet, self).__init__()
  38. self.backbone = backbone
  39. self.neck = neck
  40. self.s2anet_head = s2anet_head
  41. self.s2anet_bbox_post_process = s2anet_bbox_post_process
  42. @classmethod
  43. def from_config(cls, cfg, *args, **kwargs):
  44. backbone = create(cfg['backbone'])
  45. kwargs = {'input_shape': backbone.out_shape}
  46. neck = cfg['neck'] and create(cfg['neck'], **kwargs)
  47. out_shape = neck and neck.out_shape or backbone.out_shape
  48. kwargs = {'input_shape': out_shape}
  49. s2anet_head = create(cfg['s2anet_head'], **kwargs)
  50. s2anet_bbox_post_process = create(cfg['s2anet_bbox_post_process'],
  51. **kwargs)
  52. return {
  53. 'backbone': backbone,
  54. 'neck': neck,
  55. "s2anet_head": s2anet_head,
  56. "s2anet_bbox_post_process": s2anet_bbox_post_process,
  57. }
  58. def _forward(self):
  59. body_feats = self.backbone(self.inputs)
  60. if self.neck is not None:
  61. body_feats = self.neck(body_feats)
  62. self.s2anet_head(body_feats)
  63. if self.training:
  64. loss = self.s2anet_head.get_loss(self.inputs)
  65. total_loss = paddle.add_n(list(loss.values()))
  66. loss.update({'loss': total_loss})
  67. return loss
  68. else:
  69. im_shape = self.inputs['im_shape']
  70. scale_factor = self.inputs['scale_factor']
  71. nms_pre = self.s2anet_bbox_post_process.nms_pre
  72. pred_scores, pred_bboxes = self.s2anet_head.get_prediction(nms_pre)
  73. # post_process
  74. pred_bboxes, bbox_num = self.s2anet_bbox_post_process(pred_scores,
  75. pred_bboxes)
  76. # rescale the prediction back to origin image
  77. pred_bboxes = self.s2anet_bbox_post_process.get_pred(
  78. pred_bboxes, bbox_num, im_shape, scale_factor)
  79. # output
  80. output = {'bbox': pred_bboxes, 'bbox_num': bbox_num}
  81. return output
  82. def get_loss(self, ):
  83. loss = self._forward()
  84. return loss
  85. def get_pred(self):
  86. output = self._forward()
  87. return output