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/runtime/CL/functions/CLHOGMultiDetection.h"
25
26 #include "arm_compute/core/CL/OpenCL.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/runtime/CL/CLArray.h"
30 #include "arm_compute/runtime/CL/CLScheduler.h"
31 #include "arm_compute/runtime/CL/CLTensor.h"
32 #include "arm_compute/runtime/Scheduler.h"
33 #include "src/core/CL/kernels/CLFillBorderKernel.h"
34 #include "src/core/CL/kernels/CLHOGDescriptorKernel.h"
35 #include "src/core/CL/kernels/CLHOGDetectorKernel.h"
36 #include "src/core/CL/kernels/CLMagnitudePhaseKernel.h"
37 #include "support/MemorySupport.h"
38
39 using namespace arm_compute;
40
CLHOGMultiDetection(std::shared_ptr<IMemoryManager> memory_manager)41 CLHOGMultiDetection::CLHOGMultiDetection(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
42 : _memory_group(std::move(memory_manager)),
43 _gradient_kernel(),
44 _orient_bin_kernel(),
45 _block_norm_kernel(),
46 _hog_detect_kernel(),
47 _non_maxima_kernel(),
48 _hog_space(),
49 _hog_norm_space(),
50 _detection_windows(),
51 _mag(),
52 _phase(),
53 _non_maxima_suppression(false),
54 _num_orient_bin_kernel(0),
55 _num_block_norm_kernel(0),
56 _num_hog_detect_kernel(0)
57 {
58 }
59
60 CLHOGMultiDetection::~CLHOGMultiDetection() = default;
61
configure(ICLTensor * input,const ICLMultiHOG * multi_hog,ICLDetectionWindowArray * detection_windows,ICLSize2DArray * detection_window_strides,BorderMode border_mode,uint8_t constant_border_value,float threshold,bool non_maxima_suppression,float min_distance)62 void CLHOGMultiDetection::configure(ICLTensor *input, const ICLMultiHOG *multi_hog, ICLDetectionWindowArray *detection_windows, ICLSize2DArray *detection_window_strides, BorderMode border_mode,
63 uint8_t constant_border_value, float threshold, bool non_maxima_suppression, float min_distance)
64 {
65 configure(CLKernelLibrary::get().get_compile_context(), input, multi_hog, detection_windows, detection_window_strides, border_mode, constant_border_value, threshold, non_maxima_suppression,
66 min_distance);
67 }
68
configure(const CLCompileContext & compile_context,ICLTensor * input,const ICLMultiHOG * multi_hog,ICLDetectionWindowArray * detection_windows,ICLSize2DArray * detection_window_strides,BorderMode border_mode,uint8_t constant_border_value,float threshold,bool non_maxima_suppression,float min_distance)69 void CLHOGMultiDetection::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLMultiHOG *multi_hog, ICLDetectionWindowArray *detection_windows,
70 ICLSize2DArray *detection_window_strides, BorderMode border_mode,
71 uint8_t constant_border_value, float threshold, bool non_maxima_suppression, float min_distance)
72 {
73 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
74 ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(multi_hog);
75 ARM_COMPUTE_ERROR_ON(nullptr == detection_windows);
76 ARM_COMPUTE_ERROR_ON(detection_window_strides->num_values() != multi_hog->num_models());
77
78 const size_t width = input->info()->dimension(Window::DimX);
79 const size_t height = input->info()->dimension(Window::DimY);
80 const TensorShape &shape_img = input->info()->tensor_shape();
81 const size_t num_models = multi_hog->num_models();
82 PhaseType phase_type = multi_hog->model(0)->info()->phase_type();
83
84 size_t prev_num_bins = multi_hog->model(0)->info()->num_bins();
85 Size2D prev_cell_size = multi_hog->model(0)->info()->cell_size();
86 Size2D prev_block_size = multi_hog->model(0)->info()->block_size();
87 Size2D prev_block_stride = multi_hog->model(0)->info()->block_stride();
88
89 /* Check if CLHOGOrientationBinningKernel and CLHOGBlockNormalizationKernel kernels can be skipped for a specific HOG data-object
90 *
91 * 1) CLHOGOrientationBinningKernel and CLHOGBlockNormalizationKernel are skipped if the cell size and the number of bins don't change.
92 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
93 * 2) CLHOGBlockNormalizationKernel is skipped if the cell size, the number of bins and block size do not change.
94 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
95 *
96 * @note Since the orientation binning and block normalization kernels can be skipped, we need to keep track of the input to process for each kernel
97 * with "input_orient_bin", "input_hog_detect" and "input_block_norm"
98 */
99 std::vector<size_t> input_orient_bin;
100 std::vector<size_t> input_hog_detect;
101 std::vector<std::pair<size_t, size_t>> input_block_norm;
102
103 input_orient_bin.push_back(0);
104 input_hog_detect.push_back(0);
105 input_block_norm.emplace_back(0, 0);
106
107 for(size_t i = 1; i < num_models; ++i)
108 {
109 size_t cur_num_bins = multi_hog->model(i)->info()->num_bins();
110 Size2D cur_cell_size = multi_hog->model(i)->info()->cell_size();
111 Size2D cur_block_size = multi_hog->model(i)->info()->block_size();
112 Size2D cur_block_stride = multi_hog->model(i)->info()->block_stride();
113
114 if((cur_num_bins != prev_num_bins) || (cur_cell_size.width != prev_cell_size.width) || (cur_cell_size.height != prev_cell_size.height))
115 {
116 prev_num_bins = cur_num_bins;
117 prev_cell_size = cur_cell_size;
118 prev_block_size = cur_block_size;
119 prev_block_stride = cur_block_stride;
120
121 // Compute orientation binning and block normalization kernels. Update input to process
122 input_orient_bin.push_back(i);
123 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
124 }
125 else if((cur_block_size.width != prev_block_size.width) || (cur_block_size.height != prev_block_size.height) || (cur_block_stride.width != prev_block_stride.width)
126 || (cur_block_stride.height != prev_block_stride.height))
127 {
128 prev_block_size = cur_block_size;
129 prev_block_stride = cur_block_stride;
130
131 // Compute block normalization kernel. Update input to process
132 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
133 }
134
135 // Update input to process for hog detector kernel
136 input_hog_detect.push_back(input_block_norm.size() - 1);
137 }
138
139 _detection_windows = detection_windows;
140 _non_maxima_suppression = non_maxima_suppression;
141 _num_orient_bin_kernel = input_orient_bin.size(); // Number of CLHOGOrientationBinningKernel kernels to compute
142 _num_block_norm_kernel = input_block_norm.size(); // Number of CLHOGBlockNormalizationKernel kernels to compute
143 _num_hog_detect_kernel = input_hog_detect.size(); // Number of CLHOGDetector functions to compute
144
145 _orient_bin_kernel.reserve(_num_orient_bin_kernel);
146 _block_norm_kernel.reserve(_num_block_norm_kernel);
147 _hog_detect_kernel.resize(_num_hog_detect_kernel);
148 _hog_space.resize(_num_orient_bin_kernel);
149 _hog_norm_space.resize(_num_block_norm_kernel);
150
151 // Allocate tensors for magnitude and phase
152 TensorInfo info_mag(shape_img, Format::S16);
153 _mag.allocator()->init(info_mag);
154
155 TensorInfo info_phase(shape_img, Format::U8);
156 _phase.allocator()->init(info_phase);
157
158 // Manage intermediate buffers
159 _memory_group.manage(&_mag);
160 _memory_group.manage(&_phase);
161
162 // Initialise gradient kernel
163 _gradient_kernel.configure(compile_context, input, &_mag, &_phase, phase_type, border_mode, constant_border_value);
164
165 // Configure NETensor for the HOG space and orientation binning kernel
166 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
167 {
168 const size_t idx_multi_hog = input_orient_bin[i];
169
170 // Get the corresponding cell size and number of bins
171 const Size2D &cell = multi_hog->model(idx_multi_hog)->info()->cell_size();
172 const size_t num_bins = multi_hog->model(idx_multi_hog)->info()->num_bins();
173
174 // Calculate number of cells along the x and y directions for the hog_space
175 const size_t num_cells_x = width / cell.width;
176 const size_t num_cells_y = height / cell.height;
177
178 // TensorShape of hog space
179 TensorShape shape_hog_space = input->info()->tensor_shape();
180 shape_hog_space.set(Window::DimX, num_cells_x);
181 shape_hog_space.set(Window::DimY, num_cells_y);
182
183 // Allocate HOG space
184 TensorInfo info_space(shape_hog_space, num_bins, DataType::F32);
185 _hog_space[i].allocator()->init(info_space);
186
187 // Manage intermediate buffers
188 _memory_group.manage(&_hog_space[i]);
189
190 // Initialise orientation binning kernel
191 _orient_bin_kernel.emplace_back(support::cpp14::make_unique<CLHOGOrientationBinningKernel>());
192 _orient_bin_kernel.back()->configure(compile_context, &_mag, &_phase, &_hog_space[i], multi_hog->model(idx_multi_hog)->info());
193 }
194
195 // Allocate intermediate tensors
196 _mag.allocator()->allocate();
197 _phase.allocator()->allocate();
198
199 // Configure CLTensor for the normalized HOG space and block normalization kernel
200 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
201 {
202 const size_t idx_multi_hog = input_block_norm[i].first;
203 const size_t idx_orient_bin = input_block_norm[i].second;
204
205 // Allocate normalized HOG space
206 TensorInfo tensor_info(*(multi_hog->model(idx_multi_hog)->info()), width, height);
207 _hog_norm_space[i].allocator()->init(tensor_info);
208
209 // Manage intermediate buffers
210 _memory_group.manage(&_hog_norm_space[i]);
211
212 // Initialize block normalization kernel
213 _block_norm_kernel.emplace_back(support::cpp14::make_unique<CLHOGBlockNormalizationKernel>());
214 _block_norm_kernel.back()->configure(compile_context, &_hog_space[idx_orient_bin], &_hog_norm_space[i], multi_hog->model(idx_multi_hog)->info());
215 }
216
217 // Allocate intermediate tensors
218 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
219 {
220 _hog_space[i].allocator()->allocate();
221 }
222
223 detection_window_strides->map(CLScheduler::get().queue(), true);
224
225 // Configure HOG detector kernel
226 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
227 {
228 const size_t idx_block_norm = input_hog_detect[i];
229
230 _hog_detect_kernel[i].configure(compile_context, &_hog_norm_space[idx_block_norm], multi_hog->cl_model(i), detection_windows, detection_window_strides->at(i), threshold, i);
231 }
232
233 detection_window_strides->unmap(CLScheduler::get().queue());
234
235 // Configure non maxima suppression kernel
236 _non_maxima_kernel.configure(_detection_windows, min_distance);
237
238 // Allocate intermediate tensors
239 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
240 {
241 _hog_norm_space[i].allocator()->allocate();
242 }
243 }
244
run()245 void CLHOGMultiDetection::run()
246 {
247 ARM_COMPUTE_ERROR_ON_MSG(_detection_windows == nullptr, "Unconfigured function");
248
249 MemoryGroupResourceScope scope_mg(_memory_group);
250
251 // Reset detection window
252 _detection_windows->clear();
253
254 // Run gradient
255 _gradient_kernel.run();
256
257 // Run orientation binning kernel
258 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
259 {
260 CLScheduler::get().enqueue(*_orient_bin_kernel[i], false);
261 }
262
263 // Run block normalization kernel
264 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
265 {
266 CLScheduler::get().enqueue(*_block_norm_kernel[i], false);
267 }
268
269 // Run HOG detector kernel
270 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
271 {
272 _hog_detect_kernel[i].run();
273 }
274
275 // Run non-maxima suppression kernel if enabled
276 if(_non_maxima_suppression)
277 {
278 // Map detection windows array before computing non maxima suppression
279 _detection_windows->map(CLScheduler::get().queue(), true);
280 Scheduler::get().schedule(&_non_maxima_kernel, Window::DimY);
281 _detection_windows->unmap(CLScheduler::get().queue());
282 }
283 }
284