blob: 635b793d0c72401e1fe20342ff22e3130d513359 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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/NEON/functions/NEHOGMultiDetection.h"
25
26#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010028#include "arm_compute/core/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
30#include "arm_compute/runtime/Tensor.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010031#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
33using namespace arm_compute;
34
35NEHOGMultiDetection::NEHOGMultiDetection()
36 : _gradient_kernel(), _orient_bin_kernel(), _block_norm_kernel(), _hog_detect_kernel(), _non_maxima_kernel(), _hog_space(), _hog_norm_space(), _detection_windows(), _mag(), _phase(),
37 _non_maxima_suppression(false), _num_orient_bin_kernel(0), _num_block_norm_kernel(0), _num_hog_detect_kernel(0)
38{
39}
40
41void NEHOGMultiDetection::configure(ITensor *input, const IMultiHOG *multi_hog, IDetectionWindowArray *detection_windows, const ISize2DArray *detection_window_strides, BorderMode border_mode,
42 uint8_t constant_border_value, float threshold, bool non_maxima_suppression, float min_distance)
43{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
45 ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(multi_hog);
46 ARM_COMPUTE_ERROR_ON(nullptr == detection_windows);
47 ARM_COMPUTE_ERROR_ON(detection_window_strides->num_values() != multi_hog->num_models());
48
49 const size_t width = input->info()->dimension(Window::DimX);
50 const size_t height = input->info()->dimension(Window::DimY);
51 const TensorShape &shape_img = input->info()->tensor_shape();
52 const size_t num_models = multi_hog->num_models();
53 PhaseType phase_type = multi_hog->model(0)->info()->phase_type();
54
55 size_t prev_num_bins = multi_hog->model(0)->info()->num_bins();
56 Size2D prev_cell_size = multi_hog->model(0)->info()->cell_size();
57 Size2D prev_block_size = multi_hog->model(0)->info()->block_size();
58 Size2D prev_block_stride = multi_hog->model(0)->info()->block_stride();
59
60 /* Check if NEHOGOrientationBinningKernel and NEHOGBlockNormalizationKernel kernels can be skipped for a specific HOG data-object
61 *
62 * 1) NEHOGOrientationBinningKernel and NEHOGBlockNormalizationKernel are skipped if the cell size and the number of bins don't change.
63 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
64 * 2) NEHOGBlockNormalizationKernel is skipped if the cell size, the number of bins and block size do not change.
65 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
66 *
67 * @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
68 * with "input_orient_bin", "input_hog_detect" and "input_block_norm"
69 */
70 std::vector<size_t> input_orient_bin;
71 std::vector<size_t> input_hog_detect;
72 std::vector<std::pair<size_t, size_t>> input_block_norm;
73
74 input_orient_bin.push_back(0);
75 input_hog_detect.push_back(0);
76 input_block_norm.emplace_back(0, 0);
77
78 for(size_t i = 1; i < num_models; ++i)
79 {
80 size_t cur_num_bins = multi_hog->model(i)->info()->num_bins();
81 Size2D cur_cell_size = multi_hog->model(i)->info()->cell_size();
82 Size2D cur_block_size = multi_hog->model(i)->info()->block_size();
83 Size2D cur_block_stride = multi_hog->model(i)->info()->block_stride();
84
85 if((cur_num_bins != prev_num_bins) || (cur_cell_size.width != prev_cell_size.width) || (cur_cell_size.height != prev_cell_size.height))
86 {
87 prev_num_bins = cur_num_bins;
88 prev_cell_size = cur_cell_size;
89 prev_block_size = cur_block_size;
90 prev_block_stride = cur_block_stride;
91
92 // Compute orientation binning and block normalization kernels. Update input to process
93 input_orient_bin.push_back(i);
94 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
95 }
96 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)
97 || (cur_block_stride.height != prev_block_stride.height))
98 {
99 prev_block_size = cur_block_size;
100 prev_block_stride = cur_block_stride;
101
102 // Compute block normalization kernel. Update input to process
103 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
104 }
105
106 // Update input to process for hog detector kernel
107 input_hog_detect.push_back(input_block_norm.size() - 1);
108 }
109
110 _detection_windows = detection_windows;
111 _non_maxima_suppression = non_maxima_suppression;
112 _num_orient_bin_kernel = input_orient_bin.size(); // Number of NEHOGOrientationBinningKernel kernels to compute
113 _num_block_norm_kernel = input_block_norm.size(); // Number of NEHOGBlockNormalizationKernel kernels to compute
114 _num_hog_detect_kernel = input_hog_detect.size(); // Number of NEHOGDetector functions to compute
115
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100116 _orient_bin_kernel = arm_compute::support::cpp14::make_unique<NEHOGOrientationBinningKernel[]>(_num_orient_bin_kernel);
117 _block_norm_kernel = arm_compute::support::cpp14::make_unique<NEHOGBlockNormalizationKernel[]>(_num_block_norm_kernel);
118 _hog_detect_kernel = arm_compute::support::cpp14::make_unique<NEHOGDetector[]>(_num_hog_detect_kernel);
119 _non_maxima_kernel = arm_compute::support::cpp14::make_unique<CPPDetectionWindowNonMaximaSuppressionKernel>();
120 _hog_space = arm_compute::support::cpp14::make_unique<Tensor[]>(_num_orient_bin_kernel);
121 _hog_norm_space = arm_compute::support::cpp14::make_unique<Tensor[]>(_num_block_norm_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 // Allocate tensors for magnitude and phase
124 TensorInfo info_mag(shape_img, Format::S16);
125 _mag.allocator()->init(info_mag);
126
127 TensorInfo info_phase(shape_img, Format::U8);
128 _phase.allocator()->init(info_phase);
129
130 // Initialise gradient kernel
131 _gradient_kernel.configure(input, &_mag, &_phase, phase_type, border_mode, constant_border_value);
132
133 // Configure NETensor for the HOG space and orientation binning kernel
134 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
135 {
136 const size_t idx_multi_hog = input_orient_bin[i];
137
138 // Get the corresponding cell size and number of bins
139 const Size2D &cell = multi_hog->model(idx_multi_hog)->info()->cell_size();
140 const size_t num_bins = multi_hog->model(idx_multi_hog)->info()->num_bins();
141
142 // Calculate number of cells along the x and y directions for the hog_space
143 const size_t num_cells_x = width / cell.width;
144 const size_t num_cells_y = height / cell.height;
145
146 // TensorShape of hog space
147 TensorShape shape_hog_space = input->info()->tensor_shape();
148 shape_hog_space.set(Window::DimX, num_cells_x);
149 shape_hog_space.set(Window::DimY, num_cells_y);
150
151 // Allocate HOG space
152 TensorInfo info_space(shape_hog_space, num_bins, DataType::F32);
153 _hog_space[i].allocator()->init(info_space);
154
155 // Initialise orientation binning kernel
156 _orient_bin_kernel[i].configure(&_mag, &_phase, _hog_space.get() + i, multi_hog->model(idx_multi_hog)->info());
157 }
158
159 // Configure NETensor for the normalized HOG space and block normalization kernel
160 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
161 {
162 const size_t idx_multi_hog = input_block_norm[i].first;
163 const size_t idx_orient_bin = input_block_norm[i].second;
164
165 // Allocate normalized HOG space
166 TensorInfo tensor_info(*(multi_hog->model(idx_multi_hog)->info()), width, height);
167 _hog_norm_space[i].allocator()->init(tensor_info);
168
169 // Initialize block normalization kernel
170 _block_norm_kernel[i].configure(_hog_space.get() + idx_orient_bin, _hog_norm_space.get() + i, multi_hog->model(idx_multi_hog)->info());
171 }
172
173 // Configure HOG detector kernel
174 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
175 {
176 const size_t idx_block_norm = input_hog_detect[i];
177
178 _hog_detect_kernel[i].configure(_hog_norm_space.get() + idx_block_norm, multi_hog->model(i), detection_windows, detection_window_strides->at(i), threshold, i);
179 }
180
181 // Configure non maxima suppression kernel
182 _non_maxima_kernel->configure(_detection_windows, min_distance);
183
184 // Allocate intermediate tensors
185 _mag.allocator()->allocate();
186 _phase.allocator()->allocate();
187
188 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
189 {
190 _hog_space[i].allocator()->allocate();
191 }
192
193 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
194 {
195 _hog_norm_space[i].allocator()->allocate();
196 }
197}
198
199void NEHOGMultiDetection::run()
200{
201 ARM_COMPUTE_ERROR_ON_MSG(_detection_windows == nullptr, "Unconfigured function");
202
203 // Reset detection window
204 _detection_windows->clear();
205
206 // Run gradient
207 _gradient_kernel.run();
208
209 // Run orientation binning kernel
210 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
211 {
212 NEScheduler::get().schedule(_orient_bin_kernel.get() + i, Window::DimY);
213 }
214
215 // Run block normalization kernel
216 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
217 {
218 NEScheduler::get().schedule(_block_norm_kernel.get() + i, Window::DimY);
219 }
220
221 // Run HOG detector kernel
222 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
223 {
224 _hog_detect_kernel[i].run();
225 }
226
227 // Run non-maxima suppression kernel if enabled
228 if(_non_maxima_suppression)
229 {
230 _non_maxima_kernel->run(_non_maxima_kernel->window());
231 }
232}