roi_extractor.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.fluid as fluid
  18. __all__ = ['RoIAlign', 'FPNRoIAlign']
  19. class RoIAlign(object):
  20. def __init__(self, resolution=7, spatial_scale=1. / 16, sampling_ratio=0):
  21. super(RoIAlign, self).__init__()
  22. self.resolution = resolution
  23. self.spatial_scale = spatial_scale
  24. self.sampling_ratio = sampling_ratio
  25. def __call__(self, head_inputs, rois):
  26. roi_feat = fluid.layers.roi_align(
  27. head_inputs,
  28. rois,
  29. pooled_height=self.resolution,
  30. pooled_width=self.resolution,
  31. spatial_scale=self.spatial_scale,
  32. sampling_ratio=self.sampling_ratio)
  33. return roi_feat
  34. class FPNRoIAlign(object):
  35. """
  36. RoI align pooling for FPN feature maps
  37. Args:
  38. sampling_ratio (int): number of sampling points
  39. min_level (int): lowest level of FPN layer
  40. max_level (int): highest level of FPN layer
  41. canconical_level (int): the canconical FPN feature map level
  42. canonical_size (int): the canconical FPN feature map size
  43. box_resolution (int): box resolution
  44. mask_resolution (int): mask roi resolution
  45. """
  46. def __init__(self,
  47. sampling_ratio=0,
  48. min_level=2,
  49. max_level=5,
  50. canconical_level=4,
  51. canonical_size=224,
  52. box_resolution=7,
  53. mask_resolution=14):
  54. super(FPNRoIAlign, self).__init__()
  55. self.sampling_ratio = sampling_ratio
  56. self.min_level = min_level
  57. self.max_level = max_level
  58. self.canconical_level = canconical_level
  59. self.canonical_size = canonical_size
  60. self.box_resolution = box_resolution
  61. self.mask_resolution = mask_resolution
  62. def __call__(self, head_inputs, rois, spatial_scale, is_mask=False):
  63. """
  64. Adopt RoI align onto several level of feature maps to get RoI features.
  65. Distribute RoIs to different levels by area and get a list of RoI
  66. features by distributed RoIs and their corresponding feature maps.
  67. Returns:
  68. roi_feat(Variable): RoI features with shape of [M, C, R, R],
  69. where M is the number of RoIs and R is RoI resolution
  70. """
  71. k_min = self.min_level
  72. k_max = self.max_level
  73. num_roi_lvls = k_max - k_min + 1
  74. name_list = list(head_inputs.keys())
  75. input_name_list = name_list[-num_roi_lvls:]
  76. spatial_scale = spatial_scale[-num_roi_lvls:]
  77. rois_dist, restore_index = fluid.layers.distribute_fpn_proposals(
  78. rois, k_min, k_max, self.canconical_level, self.canonical_size)
  79. # rois_dist is in ascend order
  80. roi_out_list = []
  81. resolution = is_mask and self.mask_resolution or self.box_resolution
  82. for lvl in range(num_roi_lvls):
  83. name_index = num_roi_lvls - lvl - 1
  84. rois_input = rois_dist[lvl]
  85. head_input = head_inputs[input_name_list[name_index]]
  86. sc = spatial_scale[name_index]
  87. roi_out = fluid.layers.roi_align(
  88. input=head_input,
  89. rois=rois_input,
  90. pooled_height=resolution,
  91. pooled_width=resolution,
  92. spatial_scale=sc,
  93. sampling_ratio=self.sampling_ratio)
  94. roi_out_list.append(roi_out)
  95. roi_feat_shuffle = fluid.layers.concat(roi_out_list)
  96. roi_feat_ = fluid.layers.gather(roi_feat_shuffle, restore_index)
  97. roi_feat = fluid.layers.lod_reset(roi_feat_, rois)
  98. return roi_feat