Compute Library
 21.02
CLHOGDescriptorKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
25 
34 #include "support/StringSupport.h"
35 
36 #include <set>
37 #include <sstream>
38 #include <string>
39 
40 using namespace arm_compute;
41 
43  : _input_magnitude(nullptr), _input_phase(nullptr), _output(nullptr), _cell_size()
44 {
45 }
46 
47 void CLHOGOrientationBinningKernel::configure(const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
48 {
49  configure(CLKernelLibrary::get().get_compile_context(), input_magnitude, input_phase, output, hog_info);
50 }
51 
52 void CLHOGOrientationBinningKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
53 {
56  ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
58  ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimX) != input_phase->info()->dimension(Window::DimX));
59  ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimY) != input_phase->info()->dimension(Window::DimY));
60 
61  _input_magnitude = input_magnitude;
62  _input_phase = input_phase;
63  _output = output;
64  _cell_size = hog_info->cell_size();
65 
66  float phase_scale = (PhaseType::SIGNED == hog_info->phase_type() ? hog_info->num_bins() / 360.0f : hog_info->num_bins() / 180.0f);
67  phase_scale *= (PhaseType::SIGNED == hog_info->phase_type() ? 360.0f / 255.0f : 1.0f);
68 
69  std::stringstream args_str;
70  args_str << "-DCELL_WIDTH=" << hog_info->cell_size().width << " ";
71  args_str << "-DCELL_HEIGHT=" << hog_info->cell_size().height << " ";
72  args_str << "-DNUM_BINS=" << hog_info->num_bins() << " ";
73  args_str << "-DPHASE_SCALE=" << phase_scale << " ";
74 
75  // Construct kernel name
76  std::set<std::string> build_opts = {};
77  build_opts.insert(args_str.str());
78 
79  // Create kernel
80  const std::string kernel_name = std::string("hog_orientation_binning");
81  _kernel = create_kernel(compile_context, kernel_name, build_opts);
82 
83  constexpr unsigned int num_elems_processed_per_iteration = 1;
84  constexpr unsigned int num_elems_read_per_iteration = 1;
85  const unsigned int num_rows_read_per_iteration = hog_info->cell_size().height;
86  constexpr unsigned int num_elems_written_per_iteration = 1;
87 
88  // Configure kernel window
89  Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
90  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
91 
93  AccessWindowRectangle(input_magnitude->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
94  AccessWindowRectangle(input_phase->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
95  output_access);
96 
97  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
98 
99  ICLKernel::configure_internal(win);
100 
101  // Set config_id for enabling LWS tuning
102  _config_id = kernel_name;
103  _config_id += "_";
104  _config_id += lower_string(string_from_data_type(input_magnitude->info()->data_type()));
105  _config_id += "_";
106  _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(0));
107  _config_id += "_";
108  _config_id += support::cpp11::to_string(input_magnitude->info()->dimension(1));
109  _config_id += "_";
110  _config_id += support::cpp11::to_string(output->info()->dimension(0));
111  _config_id += "_";
112  _config_id += support::cpp11::to_string(output->info()->dimension(1));
113 }
114 
115 void CLHOGOrientationBinningKernel::run(const Window &window, cl::CommandQueue &queue)
116 {
119 
121  do
122  {
123  // Compute slice for the magnitude and phase tensors
124  Window slice_mag_phase = window.first_slice_window_2D();
125  slice_mag_phase.set(Window::DimX, Window::Dimension(window.x().start() * _cell_size.width, window.x().start() * _cell_size.width, _cell_size.width));
126  slice_mag_phase.set(Window::DimY, Window::Dimension(window.y().start() * _cell_size.height, window.y().start() * _cell_size.height, _cell_size.height));
127 
128  unsigned int idx = 0;
129  add_2D_tensor_argument(idx, _input_magnitude, slice_mag_phase);
130  add_2D_tensor_argument(idx, _input_phase, slice_mag_phase);
131  add_2D_tensor_argument(idx, _output, slice);
132 
133  enqueue(queue, *this, slice, lws_hint());
134  }
135  while(window.slide_window_slice_2D(slice));
136 }
137 
139  : _input(nullptr), _output(nullptr), _num_cells_per_block_stride()
140 {
141 }
142 
144 {
145  configure(CLKernelLibrary::get().get_compile_context(), input, output, hog_info);
146 }
147 
148 void CLHOGBlockNormalizationKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info)
149 {
150  ARM_COMPUTE_ERROR_ON(hog_info == nullptr);
153 
154  // Number of cells per block
155  const Size2D num_cells_per_block(hog_info->block_size().width / hog_info->cell_size().width,
156  hog_info->block_size().height / hog_info->cell_size().height);
157 
158  ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins() * num_cells_per_block.area(), DataType::F32);
159 
160  // Number of cells per block stride
161  const Size2D num_cells_per_block_stride(hog_info->block_stride().width / hog_info->cell_size().width,
162  hog_info->block_stride().height / hog_info->cell_size().height);
163 
164  _input = input;
165  _output = output;
166  _num_cells_per_block_stride = num_cells_per_block_stride;
167 
168  std::stringstream args_str;
169  args_str << "-DL2_HYST_THRESHOLD=" << hog_info->l2_hyst_threshold() << " ";
170  args_str << "-DNUM_CELLS_PER_BLOCK_HEIGHT=" << num_cells_per_block.height << " ";
171  args_str << "-DNUM_BINS_PER_BLOCK_X=" << num_cells_per_block.width *hog_info->num_bins() << " ";
172  args_str << "-DNUM_BINS_PER_BLOCK=" << _output->info()->num_channels() << " ";
173  args_str << "-DL2_NORM=" << static_cast<int>(HOGNormType::L2_NORM) << " ";
174  args_str << "-DL1_NORM=" << static_cast<int>(HOGNormType::L1_NORM) << " ";
175  args_str << "-DL2HYS_NORM=" << static_cast<int>(HOGNormType::L2HYS_NORM) << " ";
176  args_str << "-DHOG_NORM_TYPE=" << static_cast<int>(hog_info->normalization_type()) << " ";
177 
178  // Construct kernel name
179  std::set<std::string> build_opts = {};
180  build_opts.insert(args_str.str());
181 
182  const std::string kernel_name = std::string("hog_block_normalization");
183  _kernel = create_kernel(compile_context, kernel_name, build_opts);
184 
185  constexpr unsigned int num_elems_processed_per_iteration = 1;
186  constexpr unsigned int num_elems_read_per_iteration = 1;
187  const unsigned int num_rows_read_per_iteration = num_cells_per_block.height;
188  constexpr unsigned int num_elems_written_per_iteration = 1;
189  const unsigned int num_rows_written_per_iteration = num_cells_per_block.height;
190 
191  // Configure kernel window
192  Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
193  AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration);
194 
196  AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration),
197  output_access);
198 
199  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
200 
201  ICLKernel::configure_internal(win);
202 
203  // Set config_id for enabling LWS tuning
204  _config_id = kernel_name;
205  _config_id += "_";
206  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
207  _config_id += "_";
208  _config_id += support::cpp11::to_string(input->info()->dimension(0));
209  _config_id += "_";
210  _config_id += support::cpp11::to_string(input->info()->dimension(1));
211  _config_id += "_";
212  _config_id += support::cpp11::to_string(output->info()->dimension(0));
213  _config_id += "_";
214  _config_id += support::cpp11::to_string(output->info()->dimension(1));
215 }
216 
217 void CLHOGBlockNormalizationKernel::run(const Window &window, cl::CommandQueue &queue)
218 {
221 
223  do
224  {
225  // Compute slice for the magnitude and phase tensors
226  Window slice_in = window.first_slice_window_2D();
227  slice_in.set_dimension_step(Window::DimX, _num_cells_per_block_stride.width);
228  slice_in.set_dimension_step(Window::DimY, _num_cells_per_block_stride.height);
229 
230  unsigned int idx = 0;
231  add_2D_tensor_argument(idx, _input, slice_in);
232  add_2D_tensor_argument(idx, _output, slice);
233 
234  enqueue(queue, *this, slice, lws_hint());
235  }
236  while(window.slide_window_slice_2D(slice));
237 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
L2-norm followed by clipping.
size_t num_bins() const
The number of histogram bins for each cell.
Definition: HOGInfo.cpp:111
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const Size2D & cell_size() const
The cell size in pixels.
Definition: HOGInfo.cpp:91
Angle range: [0, 360].
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
1 channel, 1 U8 per channel
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
Store the HOG&#39;s metadata.
Definition: HOGInfo.h:35
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:350
PhaseType phase_type() const
The type of PhaseType.
Definition: HOGInfo.cpp:126
const Size2D & block_stride() const
The block stride in pixels.
Definition: HOGInfo.cpp:106
HOGNormType normalization_type() const
The normalization type.
Definition: HOGInfo.cpp:116
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
Implementation of a rectangular access pattern.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:692
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
const Size2D & block_size() const
The block size in pixels.
Definition: HOGInfo.cpp:96
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
std::string kernel_name
void configure(const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info)
Initialise the kernel&#39;s inputs, output and HOG&#39;s metadata.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
float l2_hyst_threshold() const
Threshold used for L2HYS_NORM normalization type.
Definition: HOGInfo.cpp:121
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
Wrapper to configure the Khronos OpenCL C++ header.
unsigned int num_elems_processed_per_iteration
void configure(const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info)
Initialise the kernel&#39;s input, output and HOG&#39;s metadata.
Container for valid region of a window.
Definition: Types.h:188
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
virtual size_t num_channels() const =0
The number of channels for each tensor element.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145