blob: e08b699e1cffb5dec61536ce707e47abf0131a02 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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/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"
31
32using namespace arm_compute;
33
Georgios Pinitas658039b2017-09-15 16:30:50 +010034NEHOGMultiDetection::NEHOGMultiDetection(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
35 : _memory_group(std::move(memory_manager)),
36 _gradient_kernel(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010037 _orient_bin_kernel(),
38 _block_norm_kernel(),
39 _hog_detect_kernel(),
40 _non_maxima_kernel(),
41 _hog_space(),
42 _hog_norm_space(),
43 _detection_windows(),
44 _mag(),
45 _phase(),
46 _non_maxima_suppression(false),
47 _num_orient_bin_kernel(0),
48 _num_block_norm_kernel(0),
49 _num_hog_detect_kernel(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050{
51}
52
53void NEHOGMultiDetection::configure(ITensor *input, const IMultiHOG *multi_hog, IDetectionWindowArray *detection_windows, const ISize2DArray *detection_window_strides, BorderMode border_mode,
54 uint8_t constant_border_value, float threshold, bool non_maxima_suppression, float min_distance)
55{
56 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
57 ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(multi_hog);
58 ARM_COMPUTE_ERROR_ON(nullptr == detection_windows);
59 ARM_COMPUTE_ERROR_ON(detection_window_strides->num_values() != multi_hog->num_models());
60
61 const size_t width = input->info()->dimension(Window::DimX);
62 const size_t height = input->info()->dimension(Window::DimY);
63 const TensorShape &shape_img = input->info()->tensor_shape();
64 const size_t num_models = multi_hog->num_models();
65 PhaseType phase_type = multi_hog->model(0)->info()->phase_type();
66
67 size_t prev_num_bins = multi_hog->model(0)->info()->num_bins();
68 Size2D prev_cell_size = multi_hog->model(0)->info()->cell_size();
69 Size2D prev_block_size = multi_hog->model(0)->info()->block_size();
70 Size2D prev_block_stride = multi_hog->model(0)->info()->block_stride();
71
72 /* Check if NEHOGOrientationBinningKernel and NEHOGBlockNormalizationKernel kernels can be skipped for a specific HOG data-object
73 *
74 * 1) NEHOGOrientationBinningKernel and NEHOGBlockNormalizationKernel are skipped if the cell size and the number of bins don't change.
75 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
76 * 2) NEHOGBlockNormalizationKernel is skipped if the cell size, the number of bins and block size do not change.
77 * Since "multi_hog" is sorted,it is enough to check the HOG descriptors at level "ith" and level "(i-1)th
78 *
79 * @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
80 * with "input_orient_bin", "input_hog_detect" and "input_block_norm"
81 */
82 std::vector<size_t> input_orient_bin;
83 std::vector<size_t> input_hog_detect;
84 std::vector<std::pair<size_t, size_t>> input_block_norm;
85
86 input_orient_bin.push_back(0);
87 input_hog_detect.push_back(0);
88 input_block_norm.emplace_back(0, 0);
89
90 for(size_t i = 1; i < num_models; ++i)
91 {
92 size_t cur_num_bins = multi_hog->model(i)->info()->num_bins();
93 Size2D cur_cell_size = multi_hog->model(i)->info()->cell_size();
94 Size2D cur_block_size = multi_hog->model(i)->info()->block_size();
95 Size2D cur_block_stride = multi_hog->model(i)->info()->block_stride();
96
97 if((cur_num_bins != prev_num_bins) || (cur_cell_size.width != prev_cell_size.width) || (cur_cell_size.height != prev_cell_size.height))
98 {
99 prev_num_bins = cur_num_bins;
100 prev_cell_size = cur_cell_size;
101 prev_block_size = cur_block_size;
102 prev_block_stride = cur_block_stride;
103
104 // Compute orientation binning and block normalization kernels. Update input to process
105 input_orient_bin.push_back(i);
106 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
107 }
108 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)
109 || (cur_block_stride.height != prev_block_stride.height))
110 {
111 prev_block_size = cur_block_size;
112 prev_block_stride = cur_block_stride;
113
114 // Compute block normalization kernel. Update input to process
115 input_block_norm.emplace_back(i, input_orient_bin.size() - 1);
116 }
117
118 // Update input to process for hog detector kernel
119 input_hog_detect.push_back(input_block_norm.size() - 1);
120 }
121
122 _detection_windows = detection_windows;
123 _non_maxima_suppression = non_maxima_suppression;
124 _num_orient_bin_kernel = input_orient_bin.size(); // Number of NEHOGOrientationBinningKernel kernels to compute
125 _num_block_norm_kernel = input_block_norm.size(); // Number of NEHOGBlockNormalizationKernel kernels to compute
126 _num_hog_detect_kernel = input_hog_detect.size(); // Number of NEHOGDetector functions to compute
127
Georgios Pinitas725b1732019-05-20 19:40:47 +0100128 _orient_bin_kernel.clear();
129 _block_norm_kernel.clear();
130 _hog_detect_kernel.clear();
131 _hog_space.clear();
132 _hog_norm_space.clear();
133
134 _orient_bin_kernel.resize(_num_orient_bin_kernel);
135 _block_norm_kernel.resize(_num_block_norm_kernel);
136 _hog_detect_kernel.resize(_num_hog_detect_kernel);
137 _hog_space.resize(_num_orient_bin_kernel);
138 _hog_norm_space.resize(_num_block_norm_kernel);
139 _non_maxima_kernel = CPPDetectionWindowNonMaximaSuppressionKernel();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
141 // Allocate tensors for magnitude and phase
142 TensorInfo info_mag(shape_img, Format::S16);
143 _mag.allocator()->init(info_mag);
144
145 TensorInfo info_phase(shape_img, Format::U8);
146 _phase.allocator()->init(info_phase);
147
Georgios Pinitas658039b2017-09-15 16:30:50 +0100148 // Manage intermediate buffers
149 _memory_group.manage(&_mag);
150 _memory_group.manage(&_phase);
151
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 // Initialise gradient kernel
153 _gradient_kernel.configure(input, &_mag, &_phase, phase_type, border_mode, constant_border_value);
154
155 // Configure NETensor for the HOG space and orientation binning kernel
156 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
157 {
158 const size_t idx_multi_hog = input_orient_bin[i];
159
160 // Get the corresponding cell size and number of bins
161 const Size2D &cell = multi_hog->model(idx_multi_hog)->info()->cell_size();
162 const size_t num_bins = multi_hog->model(idx_multi_hog)->info()->num_bins();
163
164 // Calculate number of cells along the x and y directions for the hog_space
165 const size_t num_cells_x = width / cell.width;
166 const size_t num_cells_y = height / cell.height;
167
168 // TensorShape of hog space
169 TensorShape shape_hog_space = input->info()->tensor_shape();
170 shape_hog_space.set(Window::DimX, num_cells_x);
171 shape_hog_space.set(Window::DimY, num_cells_y);
172
173 // Allocate HOG space
174 TensorInfo info_space(shape_hog_space, num_bins, DataType::F32);
Georgios Pinitas725b1732019-05-20 19:40:47 +0100175 _hog_space[i].allocator()->init(info_space);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
Georgios Pinitas658039b2017-09-15 16:30:50 +0100177 // Manage intermediate buffers
Georgios Pinitas725b1732019-05-20 19:40:47 +0100178 _memory_group.manage(&_hog_space[i]);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100179
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 // Initialise orientation binning kernel
Georgios Pinitas725b1732019-05-20 19:40:47 +0100181 _orient_bin_kernel[i].configure(&_mag, &_phase, &_hog_space[i], multi_hog->model(idx_multi_hog)->info());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 }
183
Georgios Pinitas658039b2017-09-15 16:30:50 +0100184 // Allocate intermediate tensors
185 _mag.allocator()->allocate();
186 _phase.allocator()->allocate();
187
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 // Configure NETensor for the normalized HOG space and block normalization kernel
189 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
190 {
191 const size_t idx_multi_hog = input_block_norm[i].first;
192 const size_t idx_orient_bin = input_block_norm[i].second;
193
194 // Allocate normalized HOG space
195 TensorInfo tensor_info(*(multi_hog->model(idx_multi_hog)->info()), width, height);
Georgios Pinitas725b1732019-05-20 19:40:47 +0100196 _hog_norm_space[i].allocator()->init(tensor_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Georgios Pinitas658039b2017-09-15 16:30:50 +0100198 // Manage intermediate buffers
Georgios Pinitas725b1732019-05-20 19:40:47 +0100199 _memory_group.manage(&_hog_norm_space[i]);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100200
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 // Initialize block normalization kernel
Georgios Pinitas725b1732019-05-20 19:40:47 +0100202 _block_norm_kernel[i].configure(&_hog_space[idx_orient_bin], &_hog_norm_space[i], multi_hog->model(idx_multi_hog)->info());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203 }
204
Georgios Pinitas658039b2017-09-15 16:30:50 +0100205 // Allocate intermediate tensors
206 for(size_t i = 0; i < _num_orient_bin_kernel; ++i)
207 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100208 _hog_space[i].allocator()->allocate();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100209 }
210
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 // Configure HOG detector kernel
212 for(size_t i = 0; i < _num_hog_detect_kernel; ++i)
213 {
214 const size_t idx_block_norm = input_hog_detect[i];
215
Georgios Pinitas725b1732019-05-20 19:40:47 +0100216 _hog_detect_kernel[i].configure(&_hog_norm_space[idx_block_norm], multi_hog->model(i), detection_windows, detection_window_strides->at(i), threshold, i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 }
218
219 // Configure non maxima suppression kernel
Georgios Pinitas725b1732019-05-20 19:40:47 +0100220 _non_maxima_kernel.configure(_detection_windows, min_distance);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
222 // Allocate intermediate tensors
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 for(size_t i = 0; i < _num_block_norm_kernel; ++i)
224 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100225 _hog_norm_space[i].allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 }
227}
228
229void NEHOGMultiDetection::run()
230{
231 ARM_COMPUTE_ERROR_ON_MSG(_detection_windows == nullptr, "Unconfigured function");
232
Georgios Pinitasda953f22019-04-02 17:27:03 +0100233 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100234
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235 // Reset detection window
236 _detection_windows->clear();
237
238 // Run gradient
239 _gradient_kernel.run();
240
241 // Run orientation binning kernel
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100242 for(auto &kernel : _orient_bin_kernel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100244 NEScheduler::get().schedule(&kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 }
246
247 // Run block normalization kernel
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100248 for(auto &kernel : _block_norm_kernel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100250 NEScheduler::get().schedule(&kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251 }
252
253 // Run HOG detector kernel
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100254 for(auto &kernel : _hog_detect_kernel)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100256 kernel.run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257 }
258
259 // Run non-maxima suppression kernel if enabled
260 if(_non_maxima_suppression)
261 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100262 NEScheduler::get().schedule(&_non_maxima_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264}