proposal_generator.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. import numpy as np
  15. import paddle
  16. import paddle.nn as nn
  17. import paddle.nn.functional as F
  18. from paddlex.ppdet.core.workspace import register, serializable
  19. from .. import ops
  20. @register
  21. @serializable
  22. class ProposalGenerator(object):
  23. """
  24. Proposal generation module
  25. For more details, please refer to the document of generate_proposals
  26. in ppdet/modeing/ops.py
  27. Args:
  28. pre_nms_top_n (int): Number of total bboxes to be kept per
  29. image before NMS. default 6000
  30. post_nms_top_n (int): Number of total bboxes to be kept per
  31. image after NMS. default 1000
  32. nms_thresh (float): Threshold in NMS. default 0.5
  33. min_size (flaot): Remove predicted boxes with either height or
  34. width < min_size. default 0.1
  35. eta (float): Apply in adaptive NMS, if adaptive `threshold > 0.5`,
  36. `adaptive_threshold = adaptive_threshold * eta` in each iteration.
  37. default 1.
  38. topk_after_collect (bool): whether to adopt topk after batch
  39. collection. If topk_after_collect is true, box filter will not be
  40. used after NMS at each image in proposal generation. default false
  41. """
  42. def __init__(self,
  43. pre_nms_top_n=12000,
  44. post_nms_top_n=2000,
  45. nms_thresh=.5,
  46. min_size=.1,
  47. eta=1.,
  48. topk_after_collect=False):
  49. super(ProposalGenerator, self).__init__()
  50. self.pre_nms_top_n = pre_nms_top_n
  51. self.post_nms_top_n = post_nms_top_n
  52. self.nms_thresh = nms_thresh
  53. self.min_size = min_size
  54. self.eta = eta
  55. self.topk_after_collect = topk_after_collect
  56. def __call__(self, scores, bbox_deltas, anchors, im_shape):
  57. top_n = self.pre_nms_top_n if self.topk_after_collect else self.post_nms_top_n
  58. variances = paddle.ones_like(anchors)
  59. rpn_rois, rpn_rois_prob, rpn_rois_num = ops.generate_proposals(
  60. scores,
  61. bbox_deltas,
  62. im_shape,
  63. anchors,
  64. variances,
  65. pre_nms_top_n=self.pre_nms_top_n,
  66. post_nms_top_n=top_n,
  67. nms_thresh=self.nms_thresh,
  68. min_size=self.min_size,
  69. eta=self.eta,
  70. return_rois_num=True)
  71. return rpn_rois, rpn_rois_prob, rpn_rois_num, self.post_nms_top_n