blob: 1224ec14a713704b239a2ed56fc7376fb5220b31 [file] [log] [blame]
Isabella Gottardi883bad72019-07-15 17:33:07 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Isabella Gottardi883bad72019-07-15 17:33:07 +01003 *
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 "arm_compute/core/CPP/kernels/CPPNonMaximumSuppressionKernel.h"
25
Isabella Gottardi883bad72019-07-15 17:33:07 +010026#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/Validate.h"
Isabella Gottardi883bad72019-07-15 17:33:07 +010028
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/AutoConfiguration.h"
30#include "src/core/helpers/WindowHelpers.h"
31
Georgios Pinitasb94c3682019-09-27 19:07:15 +010032#include <algorithm>
Isabella Gottardi883bad72019-07-15 17:33:07 +010033
34namespace arm_compute
35{
36namespace
37{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038Status validate_arguments(const ITensorInfo *bboxes,
39 const ITensorInfo *scores,
40 const ITensorInfo *output_indices,
41 unsigned int max_output_size,
42 const float score_threshold,
43 const float iou_threshold)
Isabella Gottardi883bad72019-07-15 17:33:07 +010044{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(bboxes, scores, output_indices);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bboxes, 1, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_indices, 1, DataType::S32);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bboxes->num_dimensions() > 2,
49 "The bboxes tensor must be a 2-D float tensor of shape [4, num_boxes].");
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scores->num_dimensions() > 1,
51 "The scores tensor must be a 1-D float tensor of shape [num_boxes].");
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_indices->num_dimensions() > 1,
53 "The indices must be 1-D integer tensor of shape [M], where max_output_size <= M");
Isabella Gottardi883bad72019-07-15 17:33:07 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bboxes, scores);
55 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_indices->dimension(0) == 0, "Indices tensor must be bigger than 0");
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(max_output_size == 0, "Max size cannot be 0");
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(iou_threshold < 0.f || iou_threshold > 1.f, "IOU threshold must be in [0,1]");
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG(score_threshold < 0.f || score_threshold > 1.f, "Score threshold must be in [0,1]");
59
60 return Status{};
61}
62} // namespace
63
64CPPNonMaximumSuppressionKernel::CPPNonMaximumSuppressionKernel()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 : _input_bboxes(nullptr),
66 _input_scores(nullptr),
67 _output_indices(nullptr),
68 _max_output_size(0),
69 _score_threshold(0.f),
70 _iou_threshold(0.f),
71 _num_boxes(0)
Isabella Gottardi883bad72019-07-15 17:33:07 +010072{
73}
74
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075void CPPNonMaximumSuppressionKernel::configure(const ITensor *input_bboxes,
76 const ITensor *input_scores,
77 ITensor *output_indices,
78 unsigned int max_output_size,
79 const float score_threshold,
80 const float iou_threshold)
Isabella Gottardi883bad72019-07-15 17:33:07 +010081{
82 ARM_COMPUTE_ERROR_ON_NULLPTR(input_bboxes, input_scores, output_indices);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_bboxes->info(), input_scores->info(), output_indices->info(),
84 max_output_size, score_threshold, iou_threshold));
Isabella Gottardi883bad72019-07-15 17:33:07 +010085
86 auto_init_if_empty(*output_indices->info(), TensorShape(max_output_size), 1, DataType::U8, QuantizationInfo());
87
88 _input_bboxes = input_bboxes;
89 _input_scores = input_scores;
90 _output_indices = output_indices;
91 _score_threshold = score_threshold;
92 _iou_threshold = iou_threshold;
93 _max_output_size = max_output_size;
94 _num_boxes = input_scores->info()->dimension(0);
95
Isabella Gottardi883bad72019-07-15 17:33:07 +010096 // Configure kernel window
97 Window win = calculate_max_window(*output_indices->info(), Steps());
98
99 // The CPPNonMaximumSuppressionKernel doesn't need padding so update_window_and_padding() can be skipped
100 ICPPKernel::configure(win);
101}
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103Status CPPNonMaximumSuppressionKernel::validate(const ITensorInfo *bboxes,
104 const ITensorInfo *scores,
105 const ITensorInfo *output_indices,
106 unsigned int max_output_size,
107 const float score_threshold,
108 const float iou_threshold)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100109{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 ARM_COMPUTE_RETURN_ON_ERROR(
111 validate_arguments(bboxes, scores, output_indices, max_output_size, score_threshold, iou_threshold));
Isabella Gottardi883bad72019-07-15 17:33:07 +0100112 return Status{};
113}
114
115void CPPNonMaximumSuppressionKernel::run(const Window &window, const ThreadInfo &info)
116{
117 ARM_COMPUTE_UNUSED(info);
118 ARM_COMPUTE_UNUSED(window);
119 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
121
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100122 // Auxiliary tensors
123 std::vector<int> indices_above_thd;
124 std::vector<float> scores_above_thd;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 for (unsigned int i = 0; i < _num_boxes; ++i)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100126 {
127 const float score_i = *(reinterpret_cast<float *>(_input_scores->ptr_to_element(Coordinates(i))));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 if (score_i >= _score_threshold)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100129 {
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100130 scores_above_thd.emplace_back(score_i);
131 indices_above_thd.emplace_back(i);
Isabella Gottardi883bad72019-07-15 17:33:07 +0100132 }
133 }
134
135 // Sort selected indices based on scores
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100136 const unsigned int num_above_thd = indices_above_thd.size();
137 std::vector<unsigned int> sorted_indices;
138 sorted_indices.resize(num_above_thd);
139 std::iota(sorted_indices.data(), sorted_indices.data() + num_above_thd, 0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 std::sort(std::begin(sorted_indices), std::end(sorted_indices),
Isabella Gottardi883bad72019-07-15 17:33:07 +0100141 [&](unsigned int first, unsigned int second)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 { return scores_above_thd[first] > scores_above_thd[second]; });
Isabella Gottardi883bad72019-07-15 17:33:07 +0100143
144 // Number of output is the minimum between max_detection and the scores above the threshold
145 const unsigned int num_output = std::min(_max_output_size, num_above_thd);
146 unsigned int output_idx = 0;
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100147 std::vector<bool> visited(num_above_thd, false);
Isabella Gottardi883bad72019-07-15 17:33:07 +0100148
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100149 // Keep only boxes with small IoU
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150 for (unsigned int i = 0; i < num_above_thd; ++i)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100151 {
152 // Check if the output is full
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 if (output_idx >= num_output)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100154 {
155 break;
156 }
157
158 // Check if it was already visited, if not add it to the output and update the indices counter
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 if (!visited[sorted_indices[i]])
Isabella Gottardi883bad72019-07-15 17:33:07 +0100160 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 *(reinterpret_cast<int *>(_output_indices->ptr_to_element(Coordinates(output_idx)))) =
162 indices_above_thd[sorted_indices[i]];
163 visited[sorted_indices[i]] = true;
Isabella Gottardi883bad72019-07-15 17:33:07 +0100164 ++output_idx;
165 }
166 else
167 {
168 continue;
169 }
170
171 // Once added one element at the output check if the next ones overlap and can be skipped
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 for (unsigned int j = i + 1; j < num_above_thd; ++j)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100173 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174 if (!visited[sorted_indices[j]])
Isabella Gottardi883bad72019-07-15 17:33:07 +0100175 {
176 // Calculate IoU
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100177 const unsigned int i_index = indices_above_thd[sorted_indices[i]];
178 const unsigned int j_index = indices_above_thd[sorted_indices[j]];
Isabella Gottardi883bad72019-07-15 17:33:07 +0100179 // Box-corner format: xmin, ymin, xmax, ymax
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100180 const auto box_i_xmin =
181 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(0, i_index))));
182 const auto box_i_ymin =
183 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(1, i_index))));
184 const auto box_i_xmax =
185 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(2, i_index))));
186 const auto box_i_ymax =
187 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(3, i_index))));
Isabella Gottardi883bad72019-07-15 17:33:07 +0100188
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 const auto box_j_xmin =
190 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(0, j_index))));
191 const auto box_j_ymin =
192 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(1, j_index))));
193 const auto box_j_xmax =
194 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(2, j_index))));
195 const auto box_j_ymax =
196 *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(3, j_index))));
Isabella Gottardi883bad72019-07-15 17:33:07 +0100197
198 const float area_i = (box_i_xmax - box_i_xmin) * (box_i_ymax - box_i_ymin);
199 const float area_j = (box_j_xmax - box_j_xmin) * (box_j_ymax - box_j_ymin);
200 float overlap;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100201 if (area_i <= 0 || area_j <= 0)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100202 {
203 overlap = 0.0f;
204 }
205 else
206 {
207 const auto y_min_intersection = std::max<float>(box_i_ymin, box_j_ymin);
208 const auto x_min_intersection = std::max<float>(box_i_xmin, box_j_xmin);
209 const auto y_max_intersection = std::min<float>(box_i_ymax, box_j_ymax);
210 const auto x_max_intersection = std::min<float>(box_i_xmax, box_j_xmax);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 const auto area_intersection = std::max<float>(y_max_intersection - y_min_intersection, 0.0f) *
212 std::max<float>(x_max_intersection - x_min_intersection, 0.0f);
213 overlap = area_intersection / (area_i + area_j - area_intersection);
Isabella Gottardi883bad72019-07-15 17:33:07 +0100214 }
215
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 if (overlap > _iou_threshold)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100217 {
Georgios Pinitasb94c3682019-09-27 19:07:15 +0100218 visited[sorted_indices[j]] = true;
Isabella Gottardi883bad72019-07-15 17:33:07 +0100219 }
220 }
221 }
222 }
223 // The output could be full but not the output indices tensor
224 // Instead return values not valid we put -1
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 for (; output_idx < _max_output_size; ++output_idx)
Isabella Gottardi883bad72019-07-15 17:33:07 +0100226 {
227 *(reinterpret_cast<int *>(_output_indices->ptr_to_element(Coordinates(output_idx)))) = -1;
228 }
229}
230} // namespace arm_compute