blob: 7f618b294b9f0f638e5aa97345320f73df80cbd4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-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/core/CL/kernels/CLHOGDescriptorKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <set>
39#include <sstream>
40#include <string>
41
42using namespace arm_compute;
43
44CLHOGOrientationBinningKernel::CLHOGOrientationBinningKernel()
45 : _input_magnitude(nullptr), _input_phase(nullptr), _output(nullptr), _cell_size()
46{
47}
48
49void CLHOGOrientationBinningKernel::configure(const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
50{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010051 configure(CLKernelLibrary::get().get_compile_context(), input_magnitude, input_phase, output, hog_info);
52}
53
Manuel Bottini679fc962020-04-21 16:08:53 +010054void CLHOGOrientationBinningKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010055{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_magnitude, 1, DataType::S16);
57 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_phase, 1, DataType::U8);
58 ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins(), DataType::F32);
60 ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimX) != input_phase->info()->dimension(Window::DimX));
61 ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimY) != input_phase->info()->dimension(Window::DimY));
62
63 _input_magnitude = input_magnitude;
64 _input_phase = input_phase;
65 _output = output;
66 _cell_size = hog_info->cell_size();
67
68 float phase_scale = (PhaseType::SIGNED == hog_info->phase_type() ? hog_info->num_bins() / 360.0f : hog_info->num_bins() / 180.0f);
69 phase_scale *= (PhaseType::SIGNED == hog_info->phase_type() ? 360.0f / 255.0f : 1.0f);
70
71 std::stringstream args_str;
72 args_str << "-DCELL_WIDTH=" << hog_info->cell_size().width << " ";
73 args_str << "-DCELL_HEIGHT=" << hog_info->cell_size().height << " ";
74 args_str << "-DNUM_BINS=" << hog_info->num_bins() << " ";
75 args_str << "-DPHASE_SCALE=" << phase_scale << " ";
76
77 // Construct kernel name
78 std::set<std::string> build_opts = {};
79 build_opts.insert(args_str.str());
80
81 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010082 const std::string kernel_name = std::string("hog_orientation_binning");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010083 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084
85 constexpr unsigned int num_elems_processed_per_iteration = 1;
86 constexpr unsigned int num_elems_read_per_iteration = 1;
87 const unsigned int num_rows_read_per_iteration = hog_info->cell_size().height;
88 constexpr unsigned int num_elems_written_per_iteration = 1;
89
90 // Configure kernel window
91 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
92 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
93
94 update_window_and_padding(win,
95 AccessWindowRectangle(input_magnitude->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
96 AccessWindowRectangle(input_phase->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
97 output_access);
98
99 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
100
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100101 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100102
103 // Set config_id for enabling LWS tuning
104 _config_id = kernel_name;
105 _config_id += "_";
106 _config_id += lower_string(string_from_data_type(input_magnitude->info()->data_type()));
107 _config_id += "_";
108 _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(0));
109 _config_id += "_";
110 _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(1));
111 _config_id += "_";
112 _config_id += support::cpp11::to_string(output->info()->dimension(0));
113 _config_id += "_";
114 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115}
116
117void CLHOGOrientationBinningKernel::run(const Window &window, cl::CommandQueue &queue)
118{
119 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
121
122 Window slice = window.first_slice_window_2D();
123 do
124 {
125 // Compute slice for the magnitude and phase tensors
126 Window slice_mag_phase = window.first_slice_window_2D();
127 slice_mag_phase.set(Window::DimX, Window::Dimension(window.x().start() * _cell_size.width, window.x().start() * _cell_size.width, _cell_size.width));
128 slice_mag_phase.set(Window::DimY, Window::Dimension(window.y().start() * _cell_size.height, window.y().start() * _cell_size.height, _cell_size.height));
129
130 unsigned int idx = 0;
131 add_2D_tensor_argument(idx, _input_magnitude, slice_mag_phase);
132 add_2D_tensor_argument(idx, _input_phase, slice_mag_phase);
133 add_2D_tensor_argument(idx, _output, slice);
134
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100135 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 }
137 while(window.slide_window_slice_2D(slice));
138}
139
140CLHOGBlockNormalizationKernel::CLHOGBlockNormalizationKernel()
141 : _input(nullptr), _output(nullptr), _num_cells_per_block_stride()
142{
143}
144
145void CLHOGBlockNormalizationKernel::configure(const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info)
146{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100147 configure(CLKernelLibrary::get().get_compile_context(), input, output, hog_info);
148}
149
Manuel Bottini679fc962020-04-21 16:08:53 +0100150void CLHOGBlockNormalizationKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100151{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
153 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, hog_info->num_bins(), DataType::F32);
154 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
155
156 // Number of cells per block
157 const Size2D num_cells_per_block(hog_info->block_size().width / hog_info->cell_size().width,
158 hog_info->block_size().height / hog_info->cell_size().height);
159
160 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins() * num_cells_per_block.area(), DataType::F32);
161
162 // Number of cells per block stride
163 const Size2D num_cells_per_block_stride(hog_info->block_stride().width / hog_info->cell_size().width,
164 hog_info->block_stride().height / hog_info->cell_size().height);
165
166 _input = input;
167 _output = output;
168 _num_cells_per_block_stride = num_cells_per_block_stride;
169
170 std::stringstream args_str;
171 args_str << "-DL2_HYST_THRESHOLD=" << hog_info->l2_hyst_threshold() << " ";
172 args_str << "-DNUM_CELLS_PER_BLOCK_HEIGHT=" << num_cells_per_block.height << " ";
173 args_str << "-DNUM_BINS_PER_BLOCK_X=" << num_cells_per_block.width *hog_info->num_bins() << " ";
174 args_str << "-DNUM_BINS_PER_BLOCK=" << _output->info()->num_channels() << " ";
175 args_str << "-DL2_NORM=" << static_cast<int>(HOGNormType::L2_NORM) << " ";
176 args_str << "-DL1_NORM=" << static_cast<int>(HOGNormType::L1_NORM) << " ";
177 args_str << "-DL2HYS_NORM=" << static_cast<int>(HOGNormType::L2HYS_NORM) << " ";
178 args_str << "-DHOG_NORM_TYPE=" << static_cast<int>(hog_info->normalization_type()) << " ";
179
180 // Construct kernel name
181 std::set<std::string> build_opts = {};
182 build_opts.insert(args_str.str());
183
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100184 const std::string kernel_name = std::string("hog_block_normalization");
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100185 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 constexpr unsigned int num_elems_processed_per_iteration = 1;
188 constexpr unsigned int num_elems_read_per_iteration = 1;
189 const unsigned int num_rows_read_per_iteration = num_cells_per_block.height;
190 constexpr unsigned int num_elems_written_per_iteration = 1;
191 const unsigned int num_rows_written_per_iteration = num_cells_per_block.height;
192
193 // Configure kernel window
194 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
195 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration);
196
197 update_window_and_padding(win,
198 AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
199 output_access);
200
John Richardson684cb0f2018-01-09 11:17:00 +0000201 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100203 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100204
205 // Set config_id for enabling LWS tuning
206 _config_id = kernel_name;
207 _config_id += "_";
208 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
209 _config_id += "_";
210 _config_id += support::cpp11::to_string(input->info()->dimension(0));
211 _config_id += "_";
212 _config_id += support::cpp11::to_string(input->info()->dimension(1));
213 _config_id += "_";
214 _config_id += support::cpp11::to_string(output->info()->dimension(0));
215 _config_id += "_";
216 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217}
218
219void CLHOGBlockNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
220{
221 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
222 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
223
224 Window slice = window.first_slice_window_2D();
225 do
226 {
227 // Compute slice for the magnitude and phase tensors
228 Window slice_in = window.first_slice_window_2D();
229 slice_in.set_dimension_step(Window::DimX, _num_cells_per_block_stride.width);
230 slice_in.set_dimension_step(Window::DimY, _num_cells_per_block_stride.height);
231
232 unsigned int idx = 0;
233 add_2D_tensor_argument(idx, _input, slice_in);
234 add_2D_tensor_argument(idx, _output, slice);
235
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100236 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237 }
238 while(window.slide_window_slice_2D(slice));
239}