target_assigners.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2019 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. from paddle import fluid
  18. from ppdet.core.workspace import register
  19. from ppdet.modeling.ops import BBoxAssigner, MaskAssigner
  20. __all__ = [
  21. 'BBoxAssigner',
  22. 'MaskAssigner',
  23. 'CascadeBBoxAssigner',
  24. ]
  25. @register
  26. class CascadeBBoxAssigner(object):
  27. __shared__ = ['num_classes']
  28. def __init__(self,
  29. batch_size_per_im=512,
  30. fg_fraction=.25,
  31. fg_thresh=[0.5, 0.6, 0.7],
  32. bg_thresh_hi=[0.5, 0.6, 0.7],
  33. bg_thresh_lo=[0., 0., 0.],
  34. bbox_reg_weights=[10, 20, 30],
  35. shuffle_before_sample=True,
  36. num_classes=81,
  37. class_aware=False):
  38. super(CascadeBBoxAssigner, self).__init__()
  39. self.batch_size_per_im = batch_size_per_im
  40. self.fg_fraction = fg_fraction
  41. self.fg_thresh = fg_thresh
  42. self.bg_thresh_hi = bg_thresh_hi
  43. self.bg_thresh_lo = bg_thresh_lo
  44. self.bbox_reg_weights = bbox_reg_weights
  45. self.class_nums = num_classes
  46. self.use_random = shuffle_before_sample
  47. self.class_aware = class_aware
  48. def __call__(self, input_rois, feed_vars, curr_stage, max_overlap=None):
  49. curr_bbox_reg_w = [
  50. 1. / self.bbox_reg_weights[curr_stage],
  51. 1. / self.bbox_reg_weights[curr_stage],
  52. 2. / self.bbox_reg_weights[curr_stage],
  53. 2. / self.bbox_reg_weights[curr_stage],
  54. ]
  55. outs = fluid.layers.generate_proposal_labels(
  56. rpn_rois=input_rois,
  57. gt_classes=feed_vars['gt_class'],
  58. is_crowd=feed_vars['is_crowd'],
  59. gt_boxes=feed_vars['gt_bbox'],
  60. im_info=feed_vars['im_info'],
  61. batch_size_per_im=self.batch_size_per_im,
  62. fg_thresh=self.fg_thresh[curr_stage],
  63. bg_thresh_hi=self.bg_thresh_hi[curr_stage],
  64. bg_thresh_lo=self.bg_thresh_lo[curr_stage],
  65. bbox_reg_weights=curr_bbox_reg_w,
  66. use_random=self.use_random,
  67. class_nums=self.class_nums if self.class_aware else 2,
  68. is_cls_agnostic=not self.class_aware,
  69. is_cascade_rcnn=True
  70. if curr_stage > 0 and not self.class_aware else False,
  71. max_overlap=max_overlap,
  72. return_max_overlap=True)
  73. return outs