blob: abdac504f5bb4031f58604e1d9a242540956f09b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gary Antcliffefffbdbc2019-05-28 11:40:21 +01002 * Copyright (c) 2017-2019 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"
36
37#include <set>
38#include <sstream>
39#include <string>
40
41using namespace arm_compute;
42
43CLHOGOrientationBinningKernel::CLHOGOrientationBinningKernel()
44 : _input_magnitude(nullptr), _input_phase(nullptr), _output(nullptr), _cell_size()
45{
46}
47
48void CLHOGOrientationBinningKernel::configure(const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
49{
50 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_magnitude, 1, DataType::S16);
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_phase, 1, DataType::U8);
52 ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins(), DataType::F32);
54 ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimX) != input_phase->info()->dimension(Window::DimX));
55 ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimY) != input_phase->info()->dimension(Window::DimY));
56
57 _input_magnitude = input_magnitude;
58 _input_phase = input_phase;
59 _output = output;
60 _cell_size = hog_info->cell_size();
61
62 float phase_scale = (PhaseType::SIGNED == hog_info->phase_type() ? hog_info->num_bins() / 360.0f : hog_info->num_bins() / 180.0f);
63 phase_scale *= (PhaseType::SIGNED == hog_info->phase_type() ? 360.0f / 255.0f : 1.0f);
64
65 std::stringstream args_str;
66 args_str << "-DCELL_WIDTH=" << hog_info->cell_size().width << " ";
67 args_str << "-DCELL_HEIGHT=" << hog_info->cell_size().height << " ";
68 args_str << "-DNUM_BINS=" << hog_info->num_bins() << " ";
69 args_str << "-DPHASE_SCALE=" << phase_scale << " ";
70
71 // Construct kernel name
72 std::set<std::string> build_opts = {};
73 build_opts.insert(args_str.str());
74
75 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010076 const std::string kernel_name = std::string("hog_orientation_binning");
77 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 constexpr unsigned int num_elems_processed_per_iteration = 1;
80 constexpr unsigned int num_elems_read_per_iteration = 1;
81 const unsigned int num_rows_read_per_iteration = hog_info->cell_size().height;
82 constexpr unsigned int num_elems_written_per_iteration = 1;
83
84 // Configure kernel window
85 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
86 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
87
88 update_window_and_padding(win,
89 AccessWindowRectangle(input_magnitude->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
90 AccessWindowRectangle(input_phase->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
91 output_access);
92
93 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
94
Anthony Barbierb6eb3532018-08-08 13:20:04 +010095 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010096
97 // Set config_id for enabling LWS tuning
98 _config_id = kernel_name;
99 _config_id += "_";
100 _config_id += lower_string(string_from_data_type(input_magnitude->info()->data_type()));
101 _config_id += "_";
102 _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(0));
103 _config_id += "_";
104 _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(1));
105 _config_id += "_";
106 _config_id += support::cpp11::to_string(output->info()->dimension(0));
107 _config_id += "_";
108 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109}
110
111void CLHOGOrientationBinningKernel::run(const Window &window, cl::CommandQueue &queue)
112{
113 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
114 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
115
116 Window slice = window.first_slice_window_2D();
117 do
118 {
119 // Compute slice for the magnitude and phase tensors
120 Window slice_mag_phase = window.first_slice_window_2D();
121 slice_mag_phase.set(Window::DimX, Window::Dimension(window.x().start() * _cell_size.width, window.x().start() * _cell_size.width, _cell_size.width));
122 slice_mag_phase.set(Window::DimY, Window::Dimension(window.y().start() * _cell_size.height, window.y().start() * _cell_size.height, _cell_size.height));
123
124 unsigned int idx = 0;
125 add_2D_tensor_argument(idx, _input_magnitude, slice_mag_phase);
126 add_2D_tensor_argument(idx, _input_phase, slice_mag_phase);
127 add_2D_tensor_argument(idx, _output, slice);
128
129 enqueue(queue, *this, slice);
130 }
131 while(window.slide_window_slice_2D(slice));
132}
133
134CLHOGBlockNormalizationKernel::CLHOGBlockNormalizationKernel()
135 : _input(nullptr), _output(nullptr), _num_cells_per_block_stride()
136{
137}
138
139void CLHOGBlockNormalizationKernel::configure(const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info)
140{
141 ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
142 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, hog_info->num_bins(), DataType::F32);
143 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
144
145 // Number of cells per block
146 const Size2D num_cells_per_block(hog_info->block_size().width / hog_info->cell_size().width,
147 hog_info->block_size().height / hog_info->cell_size().height);
148
149 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins() * num_cells_per_block.area(), DataType::F32);
150
151 // Number of cells per block stride
152 const Size2D num_cells_per_block_stride(hog_info->block_stride().width / hog_info->cell_size().width,
153 hog_info->block_stride().height / hog_info->cell_size().height);
154
155 _input = input;
156 _output = output;
157 _num_cells_per_block_stride = num_cells_per_block_stride;
158
159 std::stringstream args_str;
160 args_str << "-DL2_HYST_THRESHOLD=" << hog_info->l2_hyst_threshold() << " ";
161 args_str << "-DNUM_CELLS_PER_BLOCK_HEIGHT=" << num_cells_per_block.height << " ";
162 args_str << "-DNUM_BINS_PER_BLOCK_X=" << num_cells_per_block.width *hog_info->num_bins() << " ";
163 args_str << "-DNUM_BINS_PER_BLOCK=" << _output->info()->num_channels() << " ";
164 args_str << "-DL2_NORM=" << static_cast<int>(HOGNormType::L2_NORM) << " ";
165 args_str << "-DL1_NORM=" << static_cast<int>(HOGNormType::L1_NORM) << " ";
166 args_str << "-DL2HYS_NORM=" << static_cast<int>(HOGNormType::L2HYS_NORM) << " ";
167 args_str << "-DHOG_NORM_TYPE=" << static_cast<int>(hog_info->normalization_type()) << " ";
168
169 // Construct kernel name
170 std::set<std::string> build_opts = {};
171 build_opts.insert(args_str.str());
172
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100173 const std::string kernel_name = std::string("hog_block_normalization");
174 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
176 constexpr unsigned int num_elems_processed_per_iteration = 1;
177 constexpr unsigned int num_elems_read_per_iteration = 1;
178 const unsigned int num_rows_read_per_iteration = num_cells_per_block.height;
179 constexpr unsigned int num_elems_written_per_iteration = 1;
180 const unsigned int num_rows_written_per_iteration = num_cells_per_block.height;
181
182 // Configure kernel window
183 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
184 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration);
185
186 update_window_and_padding(win,
187 AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
188 output_access);
189
John Richardson684cb0f2018-01-09 11:17:00 +0000190 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100192 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100193
194 // Set config_id for enabling LWS tuning
195 _config_id = kernel_name;
196 _config_id += "_";
197 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
198 _config_id += "_";
199 _config_id += support::cpp11::to_string(input->info()->dimension(0));
200 _config_id += "_";
201 _config_id += support::cpp11::to_string(input->info()->dimension(1));
202 _config_id += "_";
203 _config_id += support::cpp11::to_string(output->info()->dimension(0));
204 _config_id += "_";
205 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206}
207
208void CLHOGBlockNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
209{
210 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
211 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
212
213 Window slice = window.first_slice_window_2D();
214 do
215 {
216 // Compute slice for the magnitude and phase tensors
217 Window slice_in = window.first_slice_window_2D();
218 slice_in.set_dimension_step(Window::DimX, _num_cells_per_block_stride.width);
219 slice_in.set_dimension_step(Window::DimY, _num_cells_per_block_stride.height);
220
221 unsigned int idx = 0;
222 add_2D_tensor_argument(idx, _input, slice_in);
223 add_2D_tensor_argument(idx, _output, slice);
224
225 enqueue(queue, *this, slice);
226 }
227 while(window.slide_window_slice_2D(slice));
228}