• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 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/CPPDetectionWindowNonMaximaSuppressionKernel.h"
25 
26 #include "arm_compute/core/Helpers.h"
27 
28 #include <algorithm>
29 #include <cmath>
30 
31 using namespace arm_compute;
32 
33 namespace
34 {
compare_detection_window(const DetectionWindow & lhs,const DetectionWindow & rhs)35 bool compare_detection_window(const DetectionWindow &lhs, const DetectionWindow &rhs)
36 {
37     if(lhs.idx_class < rhs.idx_class)
38     {
39         return true;
40     }
41     if(rhs.idx_class < lhs.idx_class)
42     {
43         return false;
44     }
45 
46     // idx_classes are equal so compare by score
47     if(lhs.score > rhs.score)
48     {
49         return true;
50     }
51     if(rhs.score > lhs.score)
52     {
53         return false;
54     }
55 
56     return false;
57 }
58 } // namespace
59 
CPPDetectionWindowNonMaximaSuppressionKernel()60 CPPDetectionWindowNonMaximaSuppressionKernel::CPPDetectionWindowNonMaximaSuppressionKernel()
61     : _input_output(nullptr), _min_distance(0.0f)
62 {
63 }
64 
is_parallelisable() const65 bool CPPDetectionWindowNonMaximaSuppressionKernel::is_parallelisable() const
66 {
67     return false;
68 }
69 
configure(IDetectionWindowArray * input_output,float min_distance)70 void CPPDetectionWindowNonMaximaSuppressionKernel::configure(IDetectionWindowArray *input_output, float min_distance)
71 {
72     ARM_COMPUTE_ERROR_ON(nullptr == input_output);
73 
74     _input_output = input_output;
75     _min_distance = min_distance;
76 
77     IKernel::configure(Window()); // Default 1 iteration window
78 }
79 
run(const Window & window,const ThreadInfo & info)80 void CPPDetectionWindowNonMaximaSuppressionKernel::run(const Window &window, const ThreadInfo &info)
81 {
82     ARM_COMPUTE_UNUSED(info);
83     ARM_COMPUTE_UNUSED(window);
84     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
85     ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(IKernel::window(), window);
86     ARM_COMPUTE_ERROR_ON(_input_output->buffer() == nullptr);
87 
88     const size_t num_candidates = _input_output->num_values();
89     size_t       num_detections = 0;
90 
91     // Sort list of candidates by idx_class and then score
92     std::sort(_input_output->buffer(), _input_output->buffer() + num_candidates, compare_detection_window);
93 
94     const float min_distance_pow2 = _min_distance * _min_distance;
95 
96     // Euclidean distance
97     for(size_t i = 0; i < num_candidates; ++i)
98     {
99         if(0.0f != _input_output->at(i).score)
100         {
101             DetectionWindow cur;
102             cur.x         = _input_output->at(i).x;
103             cur.y         = _input_output->at(i).y;
104             cur.width     = _input_output->at(i).width;
105             cur.height    = _input_output->at(i).height;
106             cur.idx_class = _input_output->at(i).idx_class;
107             cur.score     = _input_output->at(i).score;
108 
109             // Store window
110             _input_output->at(num_detections) = cur;
111 
112             ++num_detections;
113 
114             const float xc = cur.x + cur.width * 0.5f;
115             const float yc = cur.y + cur.height * 0.5f;
116 
117             for(size_t k = i + 1; k < (num_candidates) && (cur.idx_class == _input_output->at(k).idx_class); ++k)
118             {
119                 const float xn = _input_output->at(k).x + _input_output->at(k).width * 0.5f;
120                 const float yn = _input_output->at(k).y + _input_output->at(k).height * 0.5f;
121 
122                 const float dx = std::fabs(xn - xc);
123                 const float dy = std::fabs(yn - yc);
124 
125                 if(dx < _min_distance && dy < _min_distance)
126                 {
127                     const float d = dx * dx + dy * dy;
128 
129                     if(d < min_distance_pow2)
130                     {
131                         // Invalidate detection window
132                         _input_output->at(k).score = 0.0f;
133                     }
134                 }
135             }
136         }
137     }
138 
139     _input_output->resize(num_detections);
140 }
141