3
0

cascade_rcnn.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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__ = ['CascadeRCNN']
  21. @register
  22. class CascadeRCNN(BaseArch):
  23. """
  24. Cascade R-CNN network, see https://arxiv.org/abs/1712.00726
  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. mask_head (object): `MaskHead` instance
  32. mask_post_process (object): `MaskPostProcess` instance
  33. """
  34. __category__ = 'architecture'
  35. __inject__ = [
  36. 'bbox_post_process',
  37. 'mask_post_process',
  38. ]
  39. def __init__(self,
  40. backbone,
  41. rpn_head,
  42. bbox_head,
  43. bbox_post_process,
  44. neck=None,
  45. mask_head=None,
  46. mask_post_process=None):
  47. super(CascadeRCNN, self).__init__()
  48. self.backbone = backbone
  49. self.rpn_head = rpn_head
  50. self.bbox_head = bbox_head
  51. self.bbox_post_process = bbox_post_process
  52. self.neck = neck
  53. self.mask_head = mask_head
  54. self.mask_post_process = mask_post_process
  55. self.with_mask = mask_head is not None
  56. @classmethod
  57. def from_config(cls, cfg, *args, **kwargs):
  58. backbone = create(cfg['backbone'])
  59. kwargs = {'input_shape': backbone.out_shape}
  60. neck = cfg['neck'] and create(cfg['neck'], **kwargs)
  61. out_shape = neck and neck.out_shape or backbone.out_shape
  62. kwargs = {'input_shape': out_shape}
  63. rpn_head = create(cfg['rpn_head'], **kwargs)
  64. bbox_head = create(cfg['bbox_head'], **kwargs)
  65. out_shape = neck and out_shape or bbox_head.get_head().out_shape
  66. kwargs = {'input_shape': out_shape}
  67. mask_head = cfg['mask_head'] and create(cfg['mask_head'], **kwargs)
  68. return {
  69. 'backbone': backbone,
  70. 'neck': neck,
  71. "rpn_head": rpn_head,
  72. "bbox_head": bbox_head,
  73. "mask_head": mask_head,
  74. }
  75. def _forward(self):
  76. body_feats = self.backbone(self.inputs)
  77. if self.neck is not None:
  78. body_feats = self.neck(body_feats)
  79. if self.training:
  80. rois, rois_num, rpn_loss = self.rpn_head(body_feats, self.inputs)
  81. bbox_loss, bbox_feat = self.bbox_head(body_feats, rois, rois_num,
  82. self.inputs)
  83. rois, rois_num = self.bbox_head.get_assigned_rois()
  84. bbox_targets = self.bbox_head.get_assigned_targets()
  85. if self.with_mask:
  86. mask_loss = self.mask_head(body_feats, rois, rois_num,
  87. self.inputs, bbox_targets, bbox_feat)
  88. return rpn_loss, bbox_loss, mask_loss
  89. else:
  90. return rpn_loss, bbox_loss, {}
  91. else:
  92. rois, rois_num, _ = self.rpn_head(body_feats, self.inputs)
  93. preds, _ = self.bbox_head(body_feats, rois, rois_num, self.inputs)
  94. refined_rois = self.bbox_head.get_refined_rois()
  95. im_shape = self.inputs['im_shape']
  96. scale_factor = self.inputs['scale_factor']
  97. bbox, bbox_num = self.bbox_post_process(
  98. preds, (refined_rois, rois_num), im_shape, scale_factor)
  99. # rescale the prediction back to origin image
  100. bbox, bbox_pred, bbox_num = self.bbox_post_process.get_pred(
  101. bbox, bbox_num, im_shape, scale_factor)
  102. if not self.with_mask:
  103. return bbox_pred, bbox_num, None
  104. mask_out = self.mask_head(body_feats, bbox, bbox_num, self.inputs)
  105. origin_shape = self.bbox_post_process.get_origin_shape()
  106. mask_pred = self.mask_post_process(mask_out, bbox_pred, bbox_num,
  107. origin_shape)
  108. return bbox_pred, bbox_num, mask_pred
  109. def get_loss(self, ):
  110. rpn_loss, bbox_loss, mask_loss = self._forward()
  111. loss = {}
  112. loss.update(rpn_loss)
  113. loss.update(bbox_loss)
  114. if self.with_mask:
  115. loss.update(mask_loss)
  116. total_loss = paddle.add_n(list(loss.values()))
  117. loss.update({'loss': total_loss})
  118. return loss
  119. def get_pred(self):
  120. bbox_pred, bbox_num, mask_pred = self._forward()
  121. output = {
  122. 'bbox': bbox_pred,
  123. 'bbox_num': bbox_num,
  124. }
  125. if self.with_mask:
  126. output.update({'mask': mask_pred})
  127. return output