|
| 1 | +#include <algorithm> |
| 2 | +#include <functional> |
| 3 | +#include <utility> |
| 4 | +#include <vector> |
| 5 | +#include <queue> |
| 6 | +#define HelperMin(a, b) std::min(a, b) |
| 7 | +#define HelperMax(a, b) std::max(a, b) |
| 8 | + |
| 9 | +#include "caffe/layers/non_max_suppression_layer.hpp" |
| 10 | + |
| 11 | +namespace caffe { |
| 12 | + |
| 13 | +template <typename Dtype> |
| 14 | +void NonMaxSuppressionLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 15 | + const vector<Blob<Dtype>*>& top) { |
| 16 | + const NonMaxSuppressionParameter& non_max_suppression_param = this->layer_param_.non_max_suppression_param(); |
| 17 | + max_output_boxes_per_class_ = non_max_suppression_param.max_output_boxes_per_class(); |
| 18 | + iou_threshold_ = non_max_suppression_param.iou_threshold(); |
| 19 | + score_threshold_ = non_max_suppression_param.score_threshold(); |
| 20 | + center_point_box_ = non_max_suppression_param.center_point_box(); |
| 21 | + |
| 22 | + CHECK_EQ(bottom[0]->num_axes(), 3) << "bottom[0] must have 3 axes."; |
| 23 | + CHECK_EQ(bottom[1]->num_axes(), 3) << "bottom[1] must have 3 axes."; |
| 24 | + |
| 25 | + CHECK_EQ(bottom[0]->shape(0), bottom[1]->shape(0)) << "The boxes and scores should have same num_batches."; |
| 26 | + CHECK_EQ(bottom[0]->shape(1), bottom[1]->shape(2)) << "The boxes and scores should have same spatial_dimension."; |
| 27 | + CHECK_EQ(bottom[0]->shape(2), 4) << "This coordinates axis of boxes must have shape 4."; |
| 28 | + num_batches_ = bottom[0]->shape(0); |
| 29 | + num_classes_ = bottom[1]->shape(1); |
| 30 | + num_boxes_ = bottom[0]->shape(1); |
| 31 | + |
| 32 | + CHECK_GE(iou_threshold_, 0) << "The iou_threshold must not be less than 0."; |
| 33 | + CHECK_LE(iou_threshold_, 1) << "The iou_threshold must not be greater than 1."; |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +template <typename Dtype> |
| 38 | +void NonMaxSuppressionLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 39 | + const vector<Blob<Dtype>*>& top) { |
| 40 | + std::vector<int> shape(2); |
| 41 | + shape[0] = max_output_boxes_per_class_ * num_classes_ * num_batches_; |
| 42 | + shape[1] = 3; |
| 43 | + top[0]->Reshape(shape); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +template <typename Dtype> |
| 48 | +void NonMaxSuppressionLayer<Dtype>::MaxMin(float lhs, float rhs, float& min, float& max) { |
| 49 | + if (lhs >= rhs) { |
| 50 | + min = rhs; |
| 51 | + max = lhs; |
| 52 | + } else { |
| 53 | + min = lhs; |
| 54 | + max = rhs; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +template <typename Dtype> |
| 60 | +bool NonMaxSuppressionLayer<Dtype>::SuppressByIOU(const Dtype* boxes_data, int64_t box_index1, int64_t box_index2, |
| 61 | + int64_t center_point_box, float iou_threshold) { |
| 62 | + float x1_min{}; |
| 63 | + float y1_min{}; |
| 64 | + float x1_max{}; |
| 65 | + float y1_max{}; |
| 66 | + float x2_min{}; |
| 67 | + float y2_min{}; |
| 68 | + float x2_max{}; |
| 69 | + float y2_max{}; |
| 70 | + |
| 71 | + const Dtype* box1 = boxes_data + 4 * box_index1; |
| 72 | + const Dtype* box2 = boxes_data + 4 * box_index2; |
| 73 | + // center_point_box_ only support 0 or 1 |
| 74 | + if (0 == center_point_box) { |
| 75 | + // boxes data format [y1, x1, y2, x2], |
| 76 | + MaxMin(box1[1], box1[3], x1_min, x1_max); |
| 77 | + MaxMin(box1[0], box1[2], y1_min, y1_max); |
| 78 | + MaxMin(box2[1], box2[3], x2_min, x2_max); |
| 79 | + MaxMin(box2[0], box2[2], y2_min, y2_max); |
| 80 | + } else { |
| 81 | + // 1 == center_point_box_ => boxes data format [x_center, y_center, width, height] |
| 82 | + float box1_width_half = box1[2] / 2; |
| 83 | + float box1_height_half = box1[3] / 2; |
| 84 | + float box2_width_half = box2[2] / 2; |
| 85 | + float box2_height_half = box2[3] / 2; |
| 86 | + |
| 87 | + x1_min = box1[0] - box1_width_half; |
| 88 | + x1_max = box1[0] + box1_width_half; |
| 89 | + y1_min = box1[1] - box1_height_half; |
| 90 | + y1_max = box1[1] + box1_height_half; |
| 91 | + |
| 92 | + x2_min = box2[0] - box2_width_half; |
| 93 | + x2_max = box2[0] + box2_width_half; |
| 94 | + y2_min = box2[1] - box2_height_half; |
| 95 | + y2_max = box2[1] + box2_height_half; |
| 96 | + } |
| 97 | + |
| 98 | + const float intersection_x_min = HelperMax(x1_min, x2_min); |
| 99 | + const float intersection_y_min = HelperMax(y1_min, y2_min); |
| 100 | + const float intersection_x_max = HelperMin(x1_max, x2_max); |
| 101 | + const float intersection_y_max = HelperMin(y1_max, y2_max); |
| 102 | + |
| 103 | + const float intersection_area = HelperMax(intersection_x_max - intersection_x_min, .0f) * |
| 104 | + HelperMax(intersection_y_max - intersection_y_min, .0f); |
| 105 | + |
| 106 | + if (intersection_area <= .0f) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + const float area1 = (x1_max - x1_min) * (y1_max - y1_min); |
| 111 | + const float area2 = (x2_max - x2_min) * (y2_max - y2_min); |
| 112 | + const float union_area = area1 + area2 - intersection_area; |
| 113 | + |
| 114 | + if (area1 <= .0f || area2 <= .0f || union_area <= .0f) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + const float intersection_over_union = intersection_area / union_area; |
| 119 | + |
| 120 | + return intersection_over_union > iou_threshold; |
| 121 | +} |
| 122 | + |
| 123 | +template <typename Dtype> |
| 124 | +void NonMaxSuppressionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
| 125 | + const vector<Blob<Dtype>*>& top) { |
| 126 | + const Dtype* boxes_data = bottom[0]->cpu_data(); |
| 127 | + const Dtype* scores_data = bottom[1]->cpu_data(); |
| 128 | + |
| 129 | + std::vector<int64_t> selected_indices; |
| 130 | + for (int64_t batch_index = 0; batch_index < num_batches_; ++batch_index) { |
| 131 | + for (int64_t class_index = 0; class_index < num_classes_; ++class_index) { |
| 132 | + int64_t box_score_offset = (batch_index * num_classes_ + class_index) * num_boxes_; |
| 133 | + int64_t box_offset = batch_index * num_boxes_ * 4; |
| 134 | + |
| 135 | + // Filter by score_threshold_ |
| 136 | + std::priority_queue<ScoreIndexPair, std::deque<ScoreIndexPair>> sorted_scores_with_index; |
| 137 | + const Dtype* class_scores = scores_data + box_score_offset; |
| 138 | + for (int64_t box_index = 0; box_index < num_boxes_; ++box_index, ++class_scores) { |
| 139 | + if (*class_scores > score_threshold_) { |
| 140 | + sorted_scores_with_index.push(ScoreIndexPair(*class_scores, box_index)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + ScoreIndexPair next_top_score; |
| 145 | + std::vector<int64_t> selected_indices_inside_class; |
| 146 | + // Get the next box with top score, filter by iou_threshold |
| 147 | + while (!sorted_scores_with_index.empty()) { |
| 148 | + next_top_score = sorted_scores_with_index.top(); |
| 149 | + sorted_scores_with_index.pop(); |
| 150 | + |
| 151 | + bool selected = true; |
| 152 | + // Check with existing selected boxes for this class, suppress if exceed the IOU (Intersection Over Union) threshold |
| 153 | + for (int64_t selected_index : selected_indices_inside_class) { |
| 154 | + if (SuppressByIOU(boxes_data + box_offset, selected_index, next_top_score.index_, |
| 155 | + center_point_box_, iou_threshold_)) { |
| 156 | + selected = false; |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (selected) { |
| 162 | + if (max_output_boxes_per_class_ > 0 && |
| 163 | + static_cast<int64_t>(selected_indices_inside_class.size()) >= max_output_boxes_per_class_) { |
| 164 | + break; |
| 165 | + } |
| 166 | + selected_indices_inside_class.push_back(next_top_score.index_); |
| 167 | + //selected_indices.emplace_back(batch_index, class_index, next_top_score.index_); |
| 168 | + selected_indices.emplace_back(batch_index); |
| 169 | + selected_indices.emplace_back(class_index); |
| 170 | + selected_indices.emplace_back(next_top_score.index_); |
| 171 | + } |
| 172 | + } //while |
| 173 | + |
| 174 | + } //for class_index |
| 175 | + } //for batch_index |
| 176 | + |
| 177 | + Dtype* top_data = top[0]->mutable_cpu_data(); |
| 178 | + const int64_t num_selected = selected_indices.size(); |
| 179 | + for(int i=0; i<num_selected; i++) |
| 180 | + top_data[i] = selected_indices[i]; |
| 181 | + // Note: fixed output shape might be larger than the count of selected indices, |
| 182 | + // the following parts will be filled by 0. |
| 183 | +} |
| 184 | + |
| 185 | +INSTANTIATE_CLASS(NonMaxSuppressionLayer); |
| 186 | +REGISTER_LAYER_CLASS(NonMaxSuppression); |
| 187 | + |
| 188 | +} // namespace caffe |
0 commit comments