blob: 75929085b3e093a04e6440ef4469209a85ca7ff7 [file] [log] [blame]
Pablo Telloe96e4f02018-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 "NonMaxSuppression.h"
25
26#include "arm_compute/core/Types.h"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37namespace
38{
39using CandidateBox = std::pair<int /* index */, float /* score */>;
40using Box = std::tuple<float, float, float, float>;
41
42inline float get_elem_by_coordinate(const SimpleTensor<float> &tensor, Coordinates coord)
43{
44 return *static_cast<const float *>(tensor(coord));
45}
46
47inline Box get_box(const SimpleTensor<float> &boxes, size_t id)
48{
49 return std::make_tuple(
50 get_elem_by_coordinate(boxes, Coordinates(0, id)),
51 get_elem_by_coordinate(boxes, Coordinates(1, id)),
52 get_elem_by_coordinate(boxes, Coordinates(2, id)),
53 get_elem_by_coordinate(boxes, Coordinates(3, id)));
54}
55
56inline std::pair<float, float> get_min_yx(Box b)
57{
58 return std::make_pair(
59 std::min<float>(std::get<0>(b), std::get<2>(b)),
60 std::min<float>(std::get<1>(b), std::get<3>(b)));
61}
62
63inline std::pair<float, float> get_max_yx(Box b)
64{
65 return std::make_pair(
66 std::max<float>(std::get<0>(b), std::get<2>(b)),
67 std::max<float>(std::get<1>(b), std::get<3>(b)));
68}
69
70inline float compute_size(const std::pair<float, float> &min, const std::pair<float, float> &max)
71{
72 return (max.first - min.first) * (max.second - min.second);
73}
74
75inline float compute_intersection(const std::pair<float, float> &b0_min, const std::pair<float, float> &b0_max,
76 const std::pair<float, float> &b1_min, const std::pair<float, float> &b1_max, float b0_size, float b1_size)
77{
78 const float inter = std::max<float>(std::min<float>(b0_max.first, b1_max.first) - std::max<float>(b0_min.first, b1_min.first), 0.0) * std::max<float>(std::min<float>(b0_max.second,
79 b1_max.second)
80 - std::max<float>(b0_min.second, b1_min.second),
81 0.0);
82 return inter / (b0_size + b1_size - inter);
83}
84
85inline bool reject_box(Box b0, Box b1, float threshold)
86{
87 const auto b0_min = get_min_yx(b0);
88 const auto b0_max = get_max_yx(b0);
89 const auto b1_min = get_min_yx(b1);
90 const auto b1_max = get_max_yx(b1);
91 const float b0_size = compute_size(b0_min, b0_max);
92 const float b1_size = compute_size(b1_min, b1_max);
93 if(b0_size <= 0.f || b1_size <= 0.f)
94 {
95 return false;
96 }
97 else
98 {
99 return compute_intersection(b0_min, b0_max, b1_min, b1_max, b0_size, b1_size) > threshold;
100 }
101}
102
103inline std::vector<CandidateBox> get_candidates(const SimpleTensor<float> &scores, float threshold)
104{
105 std::vector<CandidateBox> candidates_vector;
106 for(int i = 0; i < scores.num_elements(); ++i)
107 {
108 if(scores[i] > threshold)
109 {
110 const auto cb = CandidateBox({ i, scores[i] });
111 candidates_vector.push_back(cb);
112 }
113 }
114 std::sort(candidates_vector.begin(), candidates_vector.end(), [](const CandidateBox bb0, const CandidateBox bb1)
115 {
116 return bb0.second >= bb1.second;
117 });
118 return candidates_vector;
119}
120
121inline bool is_box_selected(const CandidateBox &cb, const SimpleTensor<float> &bboxes, std::vector<int> &selected_boxes, float threshold)
122{
123 for(int j = selected_boxes.size() - 1; j >= 0; --j)
124 {
125 if(reject_box(get_box(bboxes, cb.first), get_box(bboxes, selected_boxes[j]), threshold))
126 {
127 return false;
128 }
129 }
130 return true;
131}
132} // namespace
133
134SimpleTensor<int> non_max_suppression(const SimpleTensor<float> &bboxes, const SimpleTensor<float> &scores, SimpleTensor<int> &indices,
135 unsigned int max_output_size, float score_threshold, float nms_threshold)
136{
137 const size_t num_boxes = bboxes.shape().y();
138 const size_t output_size = std::min(static_cast<size_t>(max_output_size), num_boxes);
139 const std::vector<CandidateBox> candidates_vector = get_candidates(scores, score_threshold);
140 std::vector<int> selected;
141 size_t p(0);
142 while(selected.size() < output_size && p < candidates_vector.size() && selected.size() < candidates_vector.size())
143 {
144 const auto nc = candidates_vector[p++];
145 if(is_box_selected(nc, bboxes, selected, nms_threshold))
146 {
147 selected.push_back(nc.first);
148 }
149 }
150 std::copy_n(selected.begin(), selected.size(), indices.data());
151 return indices;
152}
153
154} // namespace reference
155} // namespace validation
156} // namespace test
157} // namespace arm_compute