blob: 013a26f645a85a844601334e06943222fb4ceb01 [file] [log] [blame]
Pablo Telloa0a0e292018-12-21 16:47:23 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "Permute.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/Helpers.h"
28#include <queue>
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38namespace
39{
40inline float get_elem_by_coordinate(const SimpleTensor<float> &tensor, Coordinates coord)
41{
42 return *static_cast<const float *>(tensor(coord));
43}
44
45// Return intersection-over-union overlap between boxes i and j
46inline bool iou_greater_than_threshold(const SimpleTensor<float> &boxes, size_t i, size_t j, float iou_threshold)
47{
48 const float ymin_i = std::min<float>(get_elem_by_coordinate(boxes, Coordinates(0, i)), get_elem_by_coordinate(boxes, Coordinates(2, i)));
49 const float xmin_i = std::min<float>(get_elem_by_coordinate(boxes, Coordinates(1, i)), get_elem_by_coordinate(boxes, Coordinates(3, i)));
50 const float ymax_i = std::max<float>(get_elem_by_coordinate(boxes, Coordinates(0, i)), get_elem_by_coordinate(boxes, Coordinates(2, i)));
51 const float xmax_i = std::max<float>(get_elem_by_coordinate(boxes, Coordinates(1, i)), get_elem_by_coordinate(boxes, Coordinates(3, i)));
52 const float ymin_j = std::min<float>(get_elem_by_coordinate(boxes, Coordinates(0, j)), get_elem_by_coordinate(boxes, Coordinates(2, j)));
53 const float xmin_j = std::min<float>(get_elem_by_coordinate(boxes, Coordinates(1, j)), get_elem_by_coordinate(boxes, Coordinates(3, j)));
54 const float ymax_j = std::max<float>(get_elem_by_coordinate(boxes, Coordinates(0, j)), get_elem_by_coordinate(boxes, Coordinates(2, j)));
55 const float xmax_j = std::max<float>(get_elem_by_coordinate(boxes, Coordinates(1, j)), get_elem_by_coordinate(boxes, Coordinates(3, j)));
56 const float area_i = (ymax_i - ymin_i) * (xmax_i - xmin_i);
57 const float area_j = (ymax_j - ymin_j) * (xmax_j - xmin_j);
58 if(area_i <= 0 || area_j <= 0)
59 {
60 return false;
61 }
62 const float intersection_ymin = std::max<float>(ymin_i, ymin_j);
63 const float intersection_xmin = std::max<float>(xmin_i, xmin_j);
64 const float intersection_ymax = std::min<float>(ymax_i, ymax_j);
65 const float intersection_xmax = std::min<float>(xmax_i, xmax_j);
66 const float intersection_area = std::max<float>(intersection_ymax - intersection_ymin, 0.0) * std::max<float>(intersection_xmax - intersection_xmin, 0.0);
67 const float iou = intersection_area / (area_i + area_j - intersection_area);
68 return iou > iou_threshold;
69}
70
71} // namespace
72
73SimpleTensor<int> non_max_suppression(const SimpleTensor<float> &bboxes, const SimpleTensor<float> &scores, SimpleTensor<int> &indices,
74 unsigned int max_output_size, float score_threshold, float nms_threshold)
75{
76 const size_t num_boxes = bboxes.shape().y();
77 const size_t output_size = std::min(static_cast<size_t>(max_output_size), num_boxes);
78 std::vector<float> scores_data(num_boxes);
79 std::copy_n(scores.data(), num_boxes, scores_data.begin());
80
81 using CandidateBox = std::pair<int /* index */, float /* score */>;
82 auto cmp = [](const CandidateBox bb0, const CandidateBox bb1)
83 {
84 return bb0.second < bb1.second;
85 };
86
87 std::priority_queue<CandidateBox, std::deque<CandidateBox>, decltype(cmp)> candidate_priority_queue(cmp);
88 for(size_t i = 0; i < scores_data.size(); ++i)
89 {
90 if(scores_data[i] > score_threshold)
91 {
92 candidate_priority_queue.emplace(CandidateBox({ i, scores_data[i] }));
93 }
94 }
95
96 std::vector<int> selected;
97 std::vector<float> selected_scores;
98 CandidateBox next_candidate;
99
100 while(selected.size() < output_size && !candidate_priority_queue.empty())
101 {
102 next_candidate = candidate_priority_queue.top();
103 candidate_priority_queue.pop();
104 bool should_select = true;
105 for(int j = selected.size() - 1; j >= 0; --j)
106 {
107 if(iou_greater_than_threshold(bboxes, next_candidate.first, selected[j], nms_threshold))
108 {
109 should_select = false;
110 break;
111 }
112 }
113 if(should_select)
114 {
115 selected.push_back(next_candidate.first);
116 selected_scores.push_back(next_candidate.second);
117 }
118 }
119 std::copy_n(selected.begin(), selected.size(), indices.data());
120 return indices;
121}
122
123} // namespace reference
124} // namespace validation
125} // namespace test
126} // namespace arm_compute