Caffe2 - C++ API
A deep learning, cross platform ML framework
collect_and_distribute_fpn_rpn_proposals_op.h
1 #ifndef CAFFE2_OPERATORS_COLLECT_AND_DISTRIBUTE_FPN_RPN_PROPOSALS_OP_H_
2 #define CAFFE2_OPERATORS_COLLECT_AND_DISTRIBUTE_FPN_RPN_PROPOSALS_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/operator.h"
6 #include "caffe2/utils/eigen_utils.h"
7 #include "caffe2/utils/math.h"
8 
9 namespace caffe2 {
10 
11 namespace utils {
12 
13 // Compute the area of an array of boxes.
14 ERArrXXf BoxesArea(const ERArrXXf& boxes);
15 
16 // Determine which FPN level each RoI in a set of RoIs should map to based
17 // on the heuristic in the FPN paper.
18 ERArrXXf MapRoIsToFpnLevels(Eigen::Ref<const ERArrXXf> rois,
19  const float k_min, const float k_max,
20  const float s0, const float lvl0);
21 
22 // Sort RoIs from highest to lowest individual RoI score based on
23 // values from scores array and limit to n results
24 void SortAndLimitRoIsByScores(Eigen::Ref<const EArrXf> scores, int n,
25  ERArrXXf& rois);
26 
27 // Updates arr to be indices that would sort the array. Implementation of
28 // https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html
29 void ArgSort(EArrXi& arr);
30 
31 // Update out_filtered and out_indices with rows from rois where lvl matches
32 // value in lvls passed in.
33 void RowsWhereRoILevelEquals(Eigen::Ref<const ERArrXXf> rois,
34  const ERArrXXf& lvls, const int lvl,
35  ERArrXXf* out_filtered, EArrXi* out_indices);
36 
37 } // namespace utils
38 
39 // C++ implementation of CollectAndDistributeFpnRpnProposalsOp
40 // Merge RPN proposals generated at multiple FPN levels and then
41 // distribute those proposals to their appropriate FPN levels for Faster RCNN.
42 // An anchor at one FPN level may predict an RoI that will map to another
43 // level, hence the need to redistribute the proposals.
44 // Reference: facebookresearch/Detectron/detectron/ops/collect_and_distribute_fpn_rpn_proposals.py
45 template <class Context>
46 class CollectAndDistributeFpnRpnProposalsOp final : public Operator<Context> {
47  public:
48  USE_OPERATOR_CONTEXT_FUNCTIONS;
49  template <class... Args>
50  explicit CollectAndDistributeFpnRpnProposalsOp(Args&&... args)
51  : Operator<Context>(std::forward<Args>(args)...),
52  roi_canonical_scale_(
53  this->template GetSingleArgument<int>("roi_canonical_scale", 224)),
54  roi_canonical_level_(
55  this->template GetSingleArgument<int>("roi_canonical_level", 4)),
56  roi_max_level_(
57  this->template GetSingleArgument<int>("roi_max_level", 5)),
58  roi_min_level_(
59  this->template GetSingleArgument<int>("roi_min_level", 2)),
60  rpn_max_level_(
61  this->template GetSingleArgument<int>("rpn_max_level", 6)),
62  rpn_min_level_(
63  this->template GetSingleArgument<int>("rpn_min_level", 2)),
64  rpn_post_nms_topN_(
65  this->template GetSingleArgument<int>("rpn_post_nms_topN", 2000)) {
66  CAFFE_ENFORCE_GE(
67  roi_max_level_,
68  roi_min_level_,
69  "roi_max_level " + c10::to_string(roi_max_level_) +
70  " must be greater than or equal to roi_min_level " +
71  c10::to_string(roi_min_level_) + ".");
72  CAFFE_ENFORCE_GE(
73  rpn_max_level_,
74  rpn_min_level_,
75  "rpn_max_level " + c10::to_string(rpn_max_level_) +
76  " must be greater than or equal to rpn_min_level " +
77  c10::to_string(rpn_min_level_) + ".");
78  }
79 
80  ~CollectAndDistributeFpnRpnProposalsOp() {}
81 
82  bool RunOnDevice() override;
83 
84  protected:
85  // ROI_CANONICAL_SCALE
86  int roi_canonical_scale_{224};
87  // ROI_CANONICAL_LEVEL
88  int roi_canonical_level_{4};
89  // ROI_MAX_LEVEL
90  int roi_max_level_{5};
91  // ROI_MIN_LEVEL
92  int roi_min_level_{2};
93  // RPN_MAX_LEVEL
94  int rpn_max_level_{6};
95  // RPN_MIN_LEVEL
96  int rpn_min_level_{2};
97  // RPN_POST_NMS_TOP_N
98  int rpn_post_nms_topN_{2000};
99 };
100 
101 } // namespace caffe2
102 
103 #endif // CAFFE2_OPERATORS_COLLECT_AND_DISTRIBUTE_FPN_RPN_PROPOSALS_OP_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13