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