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: detectron/lib/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  CollectAndDistributeFpnRpnProposalsOp(const OperatorDef& operator_def, Workspace* ws)
50  : Operator<Context>(operator_def, ws),
51  roi_canonical_scale_(
52  OperatorBase::GetSingleArgument<int>("roi_canonical_scale", 224)),
53  roi_canonical_level_(
54  OperatorBase::GetSingleArgument<int>("roi_canonical_level", 4)),
55  roi_max_level_(
56  OperatorBase::GetSingleArgument<int>("roi_max_level", 5)),
57  roi_min_level_(
58  OperatorBase::GetSingleArgument<int>("roi_min_level", 2)),
59  rpn_max_level_(
60  OperatorBase::GetSingleArgument<int>("rpn_max_level", 6)),
61  rpn_min_level_(
62  OperatorBase::GetSingleArgument<int>("rpn_min_level", 2)),
63  rpn_post_nms_topN_(
64  OperatorBase::GetSingleArgument<int>("post_nms_topN", 2000)) {
65  CAFFE_ENFORCE_GE(
66  roi_max_level_,
67  roi_min_level_,
68  "roi_max_level " + caffe2::to_string(roi_max_level_) +
69  " must be greater than or equal to roi_min_level " +
70  caffe2::to_string(roi_min_level_) + ".");
71  CAFFE_ENFORCE_GE(
72  rpn_max_level_,
73  rpn_min_level_,
74  "rpn_max_level " + caffe2::to_string(rpn_max_level_) +
75  " must be greater than or equal to rpn_min_level " +
76  caffe2::to_string(rpn_min_level_) + ".");
77  }
78 
80 
81  bool RunOnDevice() override;
82 
83  protected:
84  // ROI_CANONICAL_SCALE
85  int roi_canonical_scale_{224};
86  // ROI_CANONICAL_LEVEL
87  int roi_canonical_level_{4};
88  // ROI_MAX_LEVEL
89  int roi_max_level_{5};
90  // ROI_MIN_LEVEL
91  int roi_min_level_{2};
92  // RPN_MAX_LEVEL
93  int rpn_max_level_{6};
94  // RPN_MIN_LEVEL
95  int rpn_min_level_{2};
96  // RPN_POST_NMS_TOP_N
97  int rpn_post_nms_topN_{2000};
98 };
99 
100 } // namespace caffe2
101 
102 #endif // CAFFE2_OPERATORS_COLLECT_AND_DISTRIBUTE_FPN_RPN_PROPOSALS_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...