faster_rcnn.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import paddle
  18. from ppdet.core.workspace import register, create
  19. from .meta_arch import BaseArch
  20. __all__ = ['FasterRCNN']
  21. @register
  22. class FasterRCNN(BaseArch):
  23. """
  24. Faster R-CNN network, see https://arxiv.org/abs/1506.01497
  25. Args:
  26. backbone (object): backbone instance
  27. rpn_head (object): `RPNHead` instance
  28. bbox_head (object): `BBoxHead` instance
  29. bbox_post_process (object): `BBoxPostProcess` instance
  30. neck (object): 'FPN' instance
  31. """
  32. __category__ = 'architecture'
  33. __inject__ = ['bbox_post_process']
  34. def __init__(self,
  35. backbone,
  36. rpn_head,
  37. bbox_head,
  38. bbox_post_process,
  39. neck=None):
  40. super(FasterRCNN, self).__init__()
  41. self.backbone = backbone
  42. self.neck = neck
  43. self.rpn_head = rpn_head
  44. self.bbox_head = bbox_head
  45. self.bbox_post_process = bbox_post_process
  46. @classmethod
  47. def from_config(cls, cfg, *args, **kwargs):
  48. backbone = create(cfg['backbone'])
  49. kwargs = {'input_shape': backbone.out_shape}
  50. neck = cfg['neck'] and create(cfg['neck'], **kwargs)
  51. out_shape = neck and neck.out_shape or backbone.out_shape
  52. kwargs = {'input_shape': out_shape}
  53. rpn_head = create(cfg['rpn_head'], **kwargs)
  54. bbox_head = create(cfg['bbox_head'], **kwargs)
  55. return {
  56. 'backbone': backbone,
  57. 'neck': neck,
  58. "rpn_head": rpn_head,
  59. "bbox_head": bbox_head,
  60. }
  61. def _forward(self):
  62. body_feats = self.backbone(self.inputs)
  63. if self.neck is not None:
  64. body_feats = self.neck(body_feats)
  65. if self.training:
  66. rois, rois_num, rpn_loss = self.rpn_head(body_feats, self.inputs)
  67. bbox_loss, _ = self.bbox_head(body_feats, rois, rois_num,
  68. self.inputs)
  69. return rpn_loss, bbox_loss
  70. else:
  71. rois, rois_num, _ = self.rpn_head(body_feats, self.inputs)
  72. preds, _ = self.bbox_head(body_feats, rois, rois_num, None)
  73. im_shape = self.inputs['im_shape']
  74. scale_factor = self.inputs['scale_factor']
  75. bbox, bbox_num = self.bbox_post_process(preds, (rois, rois_num),
  76. im_shape, scale_factor)
  77. # rescale the prediction back to origin image
  78. bboxes, bbox_pred, bbox_num = self.bbox_post_process.get_pred(
  79. bbox, bbox_num, im_shape, scale_factor)
  80. return bbox_pred, bbox_num
  81. def get_loss(self, ):
  82. rpn_loss, bbox_loss = self._forward()
  83. loss = {}
  84. loss.update(rpn_loss)
  85. loss.update(bbox_loss)
  86. total_loss = paddle.add_n(list(loss.values()))
  87. loss.update({'loss': total_loss})
  88. return loss
  89. def get_pred(self):
  90. bbox_pred, bbox_num = self._forward()
  91. output = {'bbox': bbox_pred, 'bbox_num': bbox_num}
  92. return output