blob: 8012c2f60ab8beeb98348dbce8a9c3c70318ace6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#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"
Moritz Pflanzerc186b572017-09-07 09:48:04 +010032#include "arm_compute/runtime/Scheduler.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010033#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35using namespace arm_compute;
36
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010037CLHOGMultiDetection::CLHOGMultiDetection(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
38 : _memory_group(std::move(memory_manager)),
39 _gradient_kernel(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010040 _orient_bin_kernel(),
41 _block_norm_kernel(),
42 _hog_detect_kernel(),
43 _non_maxima_kernel(),
44 _hog_space(),
45 _hog_norm_space(),
46 _detection_windows(),
47 _mag(),
48 _phase(),
49 _non_maxima_suppression(false),
50 _num_orient_bin_kernel(0),
51 _num_block_norm_kernel(0),
52 _num_hog_detect_kernel(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053{
54}
55
56void CLHOGMultiDetection::configure(ICLTensor *input, const ICLMultiHOG *multi_hog, ICLDetectionWindowArray *detection_windows, ICLSize2DArray *detection_window_strides, BorderMode border_mode,
57 uint8_t constant_border_value, float threshold, bool non_maxima_suppression, float min_distance)
58{
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
60 ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(multi_hog);
61 ARM_COMPUTE_ERROR_ON(nullptr == detection_windows);
62 ARM_COMPUTE_ERROR_ON(detection_window_strides->num_values() != multi_hog->num_models());
63
64 const size_t width = input->info()->dimension(Window::DimX);
65 const size_t height = input->info()->dimension(Window::DimY);
66 const TensorShape &shape_img = input->info()->tensor_shape();
67 const size_t num_models = multi_hog->num_models();
68 PhaseType phase_type = multi_hog->model(0)->info()->phase_type();
69
70 size_t prev_num_bins = multi_hog->model(0)->info()->num_bins();
71 Size2D prev_cell_size = multi_hog->model(0)->info()->cell_size();
72 Size2D prev_block_size = multi_hog->model(0)->info()->block_size();
73 Size2D prev_block_stride = multi_hog->model(0)->info()->block_stride();
74
75 /* Check if CLHOGOrientationBinningKernel and CLHOGBlockNormalizationKernel kernels can be skipped for a specific HOG data-object
76 *
77 * 1) CLHOGOrientationBinningKernel and CLHOGBlockNormalizationKernel are skipped if the cell size and the number of bins don't change.
78 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
79 * 2) CLHOGBlockNormalizationKernel is skipped if the cell size, the number of bins and block size do not change.
80 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
81 *
82 * @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
83 * with "input_orient_bin", "input_hog_detect" and "input_block_norm"
84 */
85 std::vector<size_t> input_orient_bin;
86 std::vector<size_t> input_hog_detect;
87 std::vector<std::pair<size_t, size_t>> input_block_norm;
88
89 input_orient_bin.push_back(0);
90 input_hog_detect.push_back(0);
91 input_block_norm.emplace_back(0, 0);
92
93 for(size_t i = 1; i < num_models; ++i)
94 {
95 size_t cur_num_bins = multi_hog->model(i)->info()->num_bins();
96 Size2D cur_cell_size = multi_hog->model(i)->info()->cell_size();
97 Size2D cur_block_size = multi_hog->model(i)->info()->block_size();
98 Size2D cur_block_stride = multi_hog->model(i)->info()->block_stride();
99
100 if((cur_num_bins != prev_num_bins) || (cur_cell_size.width != prev_cell_size.width) || (cur_cell_size.height != prev_cell_size.height))
101 {
102 prev_num_bins = cur_num_bins;
103 prev_cell_size = cur_cell_size;
104 prev_block_size = cur_block_size;
105 prev_block_stride = cur_block_stride;
106
107 // Compute orientation binning and block normalization kernels. Update input to process
108 input_orient_bin.push_back(i);
109 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
110 }
111 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)
112 || (cur_block_stride.height != prev_block_stride.height))
113 {
114 prev_block_size = cur_block_size;
115 prev_block_stride = cur_block_stride;
116
117 // Compute block normalization kernel. Update input to process
118 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
119 }
120
121 // Update input to process for hog detector kernel
122 input_hog_detect.push_back(input_block_norm.size() - 1);
123 }
124
125 _detection_windows = detection_windows;
126 _non_maxima_suppression = non_maxima_suppression;
127 _num_orient_bin_kernel = input_orient_bin.size(); // Number of CLHOGOrientationBinningKernel kernels to compute
128 _num_block_norm_kernel = input_block_norm.size(); // Number of CLHOGBlockNormalizationKernel kernels to compute
129 _num_hog_detect_kernel = input_hog_detect.size(); // Number of CLHOGDetector functions to compute
130
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100131 _orient_bin_kernel = arm_compute::support::cpp14::make_unique<CLHOGOrientationBinningKernel[]>(_num_orient_bin_kernel);
132 _block_norm_kernel = arm_compute::support::cpp14::make_unique<CLHOGBlockNormalizationKernel[]>(_num_block_norm_kernel);
133 _hog_detect_kernel = arm_compute::support::cpp14::make_unique<CLHOGDetector[]>(_num_hog_detect_kernel);
134 _non_maxima_kernel = arm_compute::support::cpp14::make_unique<CPPDetectionWindowNonMaximaSuppressionKernel>();
135 _hog_space = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_orient_bin_kernel);
136 _hog_norm_space = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_block_norm_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
138 // Allocate tensors for magnitude and phase
139 TensorInfo info_mag(shape_img, Format::S16);
140 _mag.allocator()->init(info_mag);
141
142 TensorInfo info_phase(shape_img, Format::U8);
143 _phase.allocator()->init(info_phase);
144
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100145 // Manage intermediate buffers
146 _memory_group.manage(&_mag);
147 _memory_group.manage(&_phase);
148
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 // Initialise gradient kernel
150 _gradient_kernel.configure(input, &_mag, &_phase, phase_type, border_mode, constant_border_value);
151
152 // Configure NETensor for the HOG space and orientation binning kernel
153 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
154 {
155 const size_t idx_multi_hog = input_orient_bin[i];
156
157 // Get the corresponding cell size and number of bins
158 const Size2D &cell = multi_hog->model(idx_multi_hog)->info()->cell_size();
159 const size_t num_bins = multi_hog->model(idx_multi_hog)->info()->num_bins();
160
161 // Calculate number of cells along the x and y directions for the hog_space
162 const size_t num_cells_x = width / cell.width;
163 const size_t num_cells_y = height / cell.height;
164
165 // TensorShape of hog space
166 TensorShape shape_hog_space = input->info()->tensor_shape();
167 shape_hog_space.set(Window::DimX, num_cells_x);
168 shape_hog_space.set(Window::DimY, num_cells_y);
169
170 // Allocate HOG space
171 TensorInfo info_space(shape_hog_space, num_bins, DataType::F32);
172 _hog_space[i].allocator()->init(info_space);
173
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100174 // Manage intermediate buffers
175 _memory_group.manage(_hog_space.get() + i);
176
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 // Initialise orientation binning kernel
178 _orient_bin_kernel[i].configure(&_mag, &_phase, _hog_space.get() + i, multi_hog->model(idx_multi_hog)->info());
179 }
180
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100181 // Allocate intermediate tensors
182 _mag.allocator()->allocate();
183 _phase.allocator()->allocate();
184
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 // Configure CLTensor for the normalized HOG space and block normalization kernel
186 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
187 {
188 const size_t idx_multi_hog = input_block_norm[i].first;
189 const size_t idx_orient_bin = input_block_norm[i].second;
190
191 // Allocate normalized HOG space
192 TensorInfo tensor_info(*(multi_hog->model(idx_multi_hog)->info()), width, height);
193 _hog_norm_space[i].allocator()->init(tensor_info);
194
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100195 // Manage intermediate buffers
196 _memory_group.manage(_hog_norm_space.get() + i);
197
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 // Initialize block normalization kernel
199 _block_norm_kernel[i].configure(_hog_space.get() + idx_orient_bin, _hog_norm_space.get() + i, multi_hog->model(idx_multi_hog)->info());
200 }
201
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100202 // Allocate intermediate tensors
203 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
204 {
205 _hog_space[i].allocator()->allocate();
206 }
207
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 detection_window_strides->map(CLScheduler::get().queue(), true);
209
210 // Configure HOG detector kernel
211 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
212 {
213 const size_t idx_block_norm = input_hog_detect[i];
214
215 _hog_detect_kernel[i].configure(_hog_norm_space.get() + idx_block_norm, multi_hog->cl_model(i), detection_windows, detection_window_strides->at(i), threshold, i);
216 }
217
218 detection_window_strides->unmap(CLScheduler::get().queue());
219
220 // Configure non maxima suppression kernel
221 _non_maxima_kernel->configure(_detection_windows, min_distance);
222
223 // Allocate intermediate tensors
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
225 {
226 _hog_norm_space[i].allocator()->allocate();
227 }
228}
229
230void CLHOGMultiDetection::run()
231{
232 ARM_COMPUTE_ERROR_ON_MSG(_detection_windows == nullptr, "Unconfigured function");
233
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100234 _memory_group.acquire();
235
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236 // Reset detection window
237 _detection_windows->clear();
238
239 // Run gradient
240 _gradient_kernel.run();
241
242 // Run orientation binning kernel
243 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
244 {
245 CLScheduler::get().enqueue(*(_orient_bin_kernel.get() + i), false);
246 }
247
248 // Run block normalization kernel
249 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
250 {
251 CLScheduler::get().enqueue(*(_block_norm_kernel.get() + i), false);
252 }
253
254 // Run HOG detector kernel
255 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
256 {
257 _hog_detect_kernel[i].run();
258 }
259
260 // Run non-maxima suppression kernel if enabled
261 if(_non_maxima_suppression)
262 {
263 // Map detection windows array before computing non maxima suppression
264 _detection_windows->map(CLScheduler::get().queue(), true);
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100265 Scheduler::get().schedule(_non_maxima_kernel.get(), Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266 _detection_windows->unmap(CLScheduler::get().queue());
267 }
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100268
269 _memory_group.release();
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100270}