mask_rcnn.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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__ = ['MaskRCNN']
  21. @register
  22. class MaskRCNN(BaseArch):
  23. """
  24. Mask R-CNN network, see https://arxiv.org/abs/1703.06870
  25. Args:
  26. backbone (object): backbone instance
  27. rpn_head (object): `RPNHead` instance
  28. bbox_head (object): `BBoxHead` instance
  29. mask_head (object): `MaskHead` instance
  30. bbox_post_process (object): `BBoxPostProcess` instance
  31. mask_post_process (object): `MaskPostProcess` instance
  32. neck (object): 'FPN' 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. mask_head,
  44. bbox_post_process,
  45. mask_post_process,
  46. neck=None):
  47. super(MaskRCNN, self).__init__()
  48. self.backbone = backbone
  49. self.neck = neck
  50. self.rpn_head = rpn_head
  51. self.bbox_head = bbox_head
  52. self.mask_head = mask_head
  53. self.bbox_post_process = bbox_post_process
  54. self.mask_post_process = mask_post_process
  55. @classmethod
  56. def from_config(cls, cfg, *args, **kwargs):
  57. backbone = create(cfg['backbone'])
  58. kwargs = {'input_shape': backbone.out_shape}
  59. neck = cfg['neck'] and create(cfg['neck'], **kwargs)
  60. out_shape = neck and neck.out_shape or backbone.out_shape
  61. kwargs = {'input_shape': out_shape}
  62. rpn_head = create(cfg['rpn_head'], **kwargs)
  63. bbox_head = create(cfg['bbox_head'], **kwargs)
  64. out_shape = neck and out_shape or bbox_head.get_head().out_shape
  65. kwargs = {'input_shape': out_shape}
  66. mask_head = create(cfg['mask_head'], **kwargs)
  67. return {
  68. 'backbone': backbone,
  69. 'neck': neck,
  70. "rpn_head": rpn_head,
  71. "bbox_head": bbox_head,
  72. "mask_head": mask_head,
  73. }
  74. def _forward(self):
  75. body_feats = self.backbone(self.inputs)
  76. if self.neck is not None:
  77. body_feats = self.neck(body_feats)
  78. if self.training:
  79. rois, rois_num, rpn_loss = self.rpn_head(body_feats, self.inputs)
  80. bbox_loss, bbox_feat = self.bbox_head(body_feats, rois, rois_num,
  81. self.inputs)
  82. rois, rois_num = self.bbox_head.get_assigned_rois()
  83. bbox_targets = self.bbox_head.get_assigned_targets()
  84. # Mask Head needs bbox_feat in Mask RCNN
  85. mask_loss = self.mask_head(body_feats, rois, rois_num, self.inputs,
  86. bbox_targets, bbox_feat)
  87. return rpn_loss, bbox_loss, mask_loss
  88. else:
  89. rois, rois_num, _ = self.rpn_head(body_feats, self.inputs)
  90. preds, feat_func = self.bbox_head(body_feats, rois, rois_num, None)
  91. im_shape = self.inputs['im_shape']
  92. scale_factor = self.inputs['scale_factor']
  93. bbox, bbox_num = self.bbox_post_process(preds, (rois, rois_num),
  94. im_shape, scale_factor)
  95. mask_out = self.mask_head(
  96. body_feats, bbox, bbox_num, self.inputs, feat_func=feat_func)
  97. # rescale the prediction back to origin image
  98. bbox, bbox_pred, bbox_num = self.bbox_post_process.get_pred(
  99. bbox, bbox_num, im_shape, scale_factor)
  100. origin_shape = self.bbox_post_process.get_origin_shape()
  101. mask_pred = self.mask_post_process(mask_out, bbox_pred, bbox_num,
  102. origin_shape)
  103. return bbox_pred, bbox_num, mask_pred
  104. def get_loss(self, ):
  105. bbox_loss, mask_loss, rpn_loss = self._forward()
  106. loss = {}
  107. loss.update(rpn_loss)
  108. loss.update(bbox_loss)
  109. loss.update(mask_loss)
  110. total_loss = paddle.add_n(list(loss.values()))
  111. loss.update({'loss': total_loss})
  112. return loss
  113. def get_pred(self):
  114. bbox_pred, bbox_num, mask_pred = self._forward()
  115. output = {'bbox': bbox_pred, 'bbox_num': bbox_num, 'mask': mask_pred}
  116. return output