blob: f7edf8edd0dff1d23d829dee7ac985c0b8815eb2 [file] [log] [blame]
Isabella Gottardi883bad72019-07-15 17:33:07 +01001/*
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 "arm_compute/core/CPP/kernels/CPPNonMaximumSuppressionKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Validate.h"
29#include "support/ToolchainSupport.h"
30
31#include <list>
32
33namespace arm_compute
34{
35namespace
36{
37Status validate_arguments(const ITensorInfo *bboxes, const ITensorInfo *scores, const ITensorInfo *output_indices, unsigned int max_output_size,
38 const float score_threshold, const float iou_threshold)
39{
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(bboxes, scores, output_indices);
41 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bboxes, 1, DataType::F32);
42 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_indices, 1, DataType::S32);
43 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bboxes->num_dimensions() > 2, "The bboxes tensor must be a 2-D float tensor of shape [4, num_boxes].");
44 ARM_COMPUTE_RETURN_ERROR_ON_MSG(scores->num_dimensions() > 1, "The scores tensor must be a 1-D float tensor of shape [num_boxes].");
45 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_indices->num_dimensions() > 1, "The indices must be 1-D integer tensor of shape [M], where max_output_size <= M");
46 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bboxes, scores);
47 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_indices->dimension(0) == 0, "Indices tensor must be bigger than 0");
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(max_output_size == 0, "Max size cannot be 0");
49 ARM_COMPUTE_RETURN_ERROR_ON_MSG(iou_threshold < 0.f || iou_threshold > 1.f, "IOU threshold must be in [0,1]");
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(score_threshold < 0.f || score_threshold > 1.f, "Score threshold must be in [0,1]");
51
52 return Status{};
53}
54} // namespace
55
56CPPNonMaximumSuppressionKernel::CPPNonMaximumSuppressionKernel()
57 : _input_bboxes(nullptr), _input_scores(nullptr), _output_indices(nullptr), _max_output_size(0), _score_threshold(0.f), _iou_threshold(0.f), _num_boxes(0), _scores_above_thd_vector(),
58 _indices_above_thd_vector(), _visited(), _sorted_indices()
59{
60}
61
62void CPPNonMaximumSuppressionKernel::configure(
63 const ITensor *input_bboxes, const ITensor *input_scores, ITensor *output_indices, unsigned int max_output_size,
64 const float score_threshold, const float iou_threshold)
65{
66 ARM_COMPUTE_ERROR_ON_NULLPTR(input_bboxes, input_scores, output_indices);
67 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input_bboxes->info(), input_scores->info(), output_indices->info(), max_output_size, score_threshold, iou_threshold));
68
69 auto_init_if_empty(*output_indices->info(), TensorShape(max_output_size), 1, DataType::U8, QuantizationInfo());
70
71 _input_bboxes = input_bboxes;
72 _input_scores = input_scores;
73 _output_indices = output_indices;
74 _score_threshold = score_threshold;
75 _iou_threshold = iou_threshold;
76 _max_output_size = max_output_size;
77 _num_boxes = input_scores->info()->dimension(0);
78
79 _scores_above_thd_vector.reserve(_num_boxes);
80 _indices_above_thd_vector.reserve(_num_boxes);
81
82 // Visited and sorted_indices are preallocated as num_boxes size, which is the maximum size possible
83 // Will be used only N elements where N is the number of score above the threshold
84 _visited.reserve(_num_boxes);
85 _sorted_indices.reserve(_num_boxes);
86
87 // Configure kernel window
88 Window win = calculate_max_window(*output_indices->info(), Steps());
89
90 // The CPPNonMaximumSuppressionKernel doesn't need padding so update_window_and_padding() can be skipped
91 ICPPKernel::configure(win);
92}
93
94Status CPPNonMaximumSuppressionKernel::validate(
95 const ITensorInfo *bboxes, const ITensorInfo *scores, const ITensorInfo *output_indices, unsigned int max_output_size,
96 const float score_threshold, const float iou_threshold)
97{
98 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(bboxes, scores, output_indices, max_output_size, score_threshold, iou_threshold));
99 return Status{};
100}
101
102void CPPNonMaximumSuppressionKernel::run(const Window &window, const ThreadInfo &info)
103{
104 ARM_COMPUTE_UNUSED(info);
105 ARM_COMPUTE_UNUSED(window);
106 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
107 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
108
109 unsigned int num_above_thd = 0;
110 for(unsigned int i = 0; i < _num_boxes; ++i)
111 {
112 const float score_i = *(reinterpret_cast<float *>(_input_scores->ptr_to_element(Coordinates(i))));
113 if(score_i >= _score_threshold)
114 {
115 _indices_above_thd_vector.emplace_back(i);
116 _scores_above_thd_vector.emplace_back(score_i);
117 // Initialize respective index and visited
118 _sorted_indices.emplace_back(num_above_thd);
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100119 _visited.push_back(false);
Isabella Gottardi883bad72019-07-15 17:33:07 +0100120 ++num_above_thd;
121 }
122 }
123
124 // Sort selected indices based on scores
125 std::sort(_sorted_indices.begin(),
126 _sorted_indices.end(),
127 [&](unsigned int first, unsigned int second)
128 {
129 return _scores_above_thd_vector[first] > _scores_above_thd_vector[second];
130 });
131
132 // Number of output is the minimum between max_detection and the scores above the threshold
133 const unsigned int num_output = std::min(_max_output_size, num_above_thd);
134 unsigned int output_idx = 0;
135
136 for(unsigned int i = 0; i < num_above_thd; ++i)
137 {
138 // Check if the output is full
139 if(output_idx >= num_output)
140 {
141 break;
142 }
143
144 // Check if it was already visited, if not add it to the output and update the indices counter
145 if(!_visited[_sorted_indices[i]])
146 {
147 *(reinterpret_cast<int *>(_output_indices->ptr_to_element(Coordinates(output_idx)))) = _indices_above_thd_vector[_sorted_indices[i]];
148 ++output_idx;
149 }
150 else
151 {
152 continue;
153 }
154
155 // Once added one element at the output check if the next ones overlap and can be skipped
156 for(unsigned int j = i + 1; j < num_above_thd; ++j)
157 {
158 if(!_visited[_sorted_indices[j]])
159 {
160 // Calculate IoU
161 const unsigned int i_index = _indices_above_thd_vector[_sorted_indices[i]];
162 const unsigned int j_index = _indices_above_thd_vector[_sorted_indices[j]];
163 // Box-corner format: xmin, ymin, xmax, ymax
164 const auto box_i_xmin = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(0, i_index))));
165 const auto box_i_ymin = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(1, i_index))));
166 const auto box_i_xmax = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(2, i_index))));
167 const auto box_i_ymax = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(3, i_index))));
168
169 const auto box_j_xmin = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(0, j_index))));
170 const auto box_j_ymin = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(1, j_index))));
171 const auto box_j_xmax = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(2, j_index))));
172 const auto box_j_ymax = *(reinterpret_cast<float *>(_input_bboxes->ptr_to_element(Coordinates(3, j_index))));
173
174 const float area_i = (box_i_xmax - box_i_xmin) * (box_i_ymax - box_i_ymin);
175 const float area_j = (box_j_xmax - box_j_xmin) * (box_j_ymax - box_j_ymin);
176 float overlap;
177 if(area_i <= 0 || area_j <= 0)
178 {
179 overlap = 0.0f;
180 }
181 else
182 {
183 const auto y_min_intersection = std::max<float>(box_i_ymin, box_j_ymin);
184 const auto x_min_intersection = std::max<float>(box_i_xmin, box_j_xmin);
185 const auto y_max_intersection = std::min<float>(box_i_ymax, box_j_ymax);
186 const auto x_max_intersection = std::min<float>(box_i_xmax, box_j_xmax);
187 const auto area_intersection = std::max<float>(y_max_intersection - y_min_intersection, 0.0f) * std::max<float>(x_max_intersection - x_min_intersection, 0.0f);
188 overlap = area_intersection / (area_i + area_j - area_intersection);
189 }
190
191 if(overlap > _iou_threshold)
192 {
193 _visited[_sorted_indices[j]] = true;
194 }
195 }
196 }
197 }
198 // The output could be full but not the output indices tensor
199 // Instead return values not valid we put -1
200 for(; output_idx < _max_output_size; ++output_idx)
201 {
202 *(reinterpret_cast<int *>(_output_indices->ptr_to_element(Coordinates(output_idx)))) = -1;
203 }
204}
205} // namespace arm_compute