blob: 5037ac55cb533d003854c6f2a868a14f20117e32 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
John Richardson7f4a8192018-02-05 15:12:22 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CPPDetectionWindowNonMaximaSuppressionKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28
29#include <algorithm>
30#include <cmath>
31
32using namespace arm_compute;
33
34namespace
35{
36bool compare_detection_window(const DetectionWindow &lhs, const DetectionWindow &rhs)
37{
John Richardson7f4a8192018-02-05 15:12:22 +000038 if(lhs.idx_class < rhs.idx_class)
39 {
40 return true;
41 }
42 if(rhs.idx_class < lhs.idx_class)
43 {
44 return false;
45 }
46
47 // idx_classes are equal so compare by score
48 if(lhs.score > rhs.score)
49 {
50 return true;
51 }
52 if(rhs.score > lhs.score)
53 {
54 return false;
55 }
56
57 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058}
59} // namespace
60
61CPPDetectionWindowNonMaximaSuppressionKernel::CPPDetectionWindowNonMaximaSuppressionKernel()
62 : _input_output(nullptr), _min_distance(0.0f)
63{
64}
65
66bool CPPDetectionWindowNonMaximaSuppressionKernel::is_parallelisable() const
67{
68 return false;
69}
70
71void CPPDetectionWindowNonMaximaSuppressionKernel::configure(IDetectionWindowArray *input_output, float min_distance)
72{
73 ARM_COMPUTE_ERROR_ON(nullptr == input_output);
74
75 _input_output = input_output;
76 _min_distance = min_distance;
77
78 IKernel::configure(Window()); // Default 1 iteration window
79}
80
Moritz Pflanzerc186b572017-09-07 09:48:04 +010081void CPPDetectionWindowNonMaximaSuppressionKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010083 ARM_COMPUTE_UNUSED(info);
Anthony Barbierc0ee4612017-12-11 13:15:02 +000084 ARM_COMPUTE_UNUSED(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
86 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IKernel::window(), window);
87 ARM_COMPUTE_ERROR_ON(_input_output->buffer() == nullptr);
88
89 const size_t num_candidates = _input_output->num_values();
90 size_t num_detections = 0;
91
John Richardson7f4a8192018-02-05 15:12:22 +000092 // Sort list of candidates by idx_class and then score
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 std::sort(_input_output->buffer(), _input_output->buffer() + num_candidates, compare_detection_window);
94
95 const float min_distance_pow2 = _min_distance * _min_distance;
96
97 // Euclidean distance
98 for(size_t i = 0; i < num_candidates; ++i)
99 {
100 if(0.0f != _input_output->at(i).score)
101 {
102 DetectionWindow cur;
103 cur.x = _input_output->at(i).x;
104 cur.y = _input_output->at(i).y;
105 cur.width = _input_output->at(i).width;
106 cur.height = _input_output->at(i).height;
107 cur.idx_class = _input_output->at(i).idx_class;
108 cur.score = _input_output->at(i).score;
109
110 // Store window
111 _input_output->at(num_detections) = cur;
112
113 ++num_detections;
114
115 const float xc = cur.x + cur.width * 0.5f;
116 const float yc = cur.y + cur.height * 0.5f;
117
John Richardson7f4a8192018-02-05 15:12:22 +0000118 for(size_t k = i + 1; k < (num_candidates) && (cur.idx_class == _input_output->at(k).idx_class); ++k)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 {
120 const float xn = _input_output->at(k).x + _input_output->at(k).width * 0.5f;
121 const float yn = _input_output->at(k).y + _input_output->at(k).height * 0.5f;
122
123 const float dx = std::fabs(xn - xc);
124 const float dy = std::fabs(yn - yc);
125
126 if(dx < _min_distance && dy < _min_distance)
127 {
128 const float d = dx * dx + dy * dy;
129
130 if(d < min_distance_pow2)
131 {
John Richardson7f4a8192018-02-05 15:12:22 +0000132 // Invalidate detection window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 _input_output->at(k).score = 0.0f;
134 }
135 }
136 }
137 }
138 }
139
140 _input_output->resize(num_detections);
141}