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