Compute Library
 21.02
CLHOGDetectorKernel.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 using namespace arm_compute;
37 
39  : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr)
40 {
41 }
42 
43 void CLHOGDetectorKernel::configure(const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows, const Size2D &detection_window_stride,
44  float threshold, uint16_t idx_class)
45 {
46  configure(CLKernelLibrary::get().get_compile_context(), input, hog, detection_windows, num_detection_windows, detection_window_stride, threshold, idx_class);
47 }
48 
49 void CLHOGDetectorKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows,
50  const Size2D &detection_window_stride,
51  float threshold, uint16_t idx_class)
52 {
54  ARM_COMPUTE_ERROR_ON(hog == nullptr);
55  ARM_COMPUTE_ERROR_ON(detection_windows == nullptr);
56  ARM_COMPUTE_ERROR_ON(num_detection_windows == nullptr);
57  ARM_COMPUTE_ERROR_ON((detection_window_stride.width % hog->info()->block_stride().width) != 0);
58  ARM_COMPUTE_ERROR_ON((detection_window_stride.height % hog->info()->block_stride().height) != 0);
59 
60  const Size2D &detection_window_size = hog->info()->detection_window_size();
61  const Size2D &block_size = hog->info()->block_size();
62  const Size2D &block_stride = hog->info()->block_stride();
63 
64  _input = input;
65  _detection_windows = detection_windows;
66  _num_detection_windows = num_detection_windows;
67 
68  const unsigned int num_bins_per_descriptor_x = ((detection_window_size.width - block_size.width) / block_stride.width + 1) * input->info()->num_channels();
69  const unsigned int num_blocks_per_descriptor_y = (detection_window_size.height - block_size.height) / block_stride.height + 1;
70 
71  ARM_COMPUTE_ERROR_ON((num_bins_per_descriptor_x * num_blocks_per_descriptor_y + 1) != hog->info()->descriptor_size());
72 
73  std::stringstream args_str;
74  args_str << "-DNUM_BLOCKS_PER_DESCRIPTOR_Y=" << num_blocks_per_descriptor_y << " ";
75  args_str << "-DNUM_BINS_PER_DESCRIPTOR_X=" << num_bins_per_descriptor_x << " ";
76  args_str << "-DTHRESHOLD=" << threshold << " ";
77  args_str << "-DMAX_NUM_DETECTION_WINDOWS=" << detection_windows->max_num_values() << " ";
78  args_str << "-DIDX_CLASS=" << idx_class << " ";
79  args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " ";
80  args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " ";
81  args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " ";
82  args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " ";
83 
84  // Construct kernel name
85  std::set<std::string> build_opts = {};
86  build_opts.insert(args_str.str());
87 
88  // Create kernel
89  const std::string kernel_name = std::string("hog_detector");
90  _kernel = create_kernel(compile_context, kernel_name, build_opts);
91 
92  // Set static kernel arguments
93  unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input parameters
94  _kernel.setArg(idx++, hog->cl_buffer());
95  _kernel.setArg(idx++, detection_windows->cl_buffer());
96  _kernel.setArg(idx++, *_num_detection_windows);
97 
98  // Get the number of blocks along the x and y directions of the input tensor
99  const ValidRegion &valid_region = input->info()->valid_region();
100  const size_t num_blocks_x = valid_region.shape[0];
101  const size_t num_blocks_y = valid_region.shape[1];
102 
103  // Get the number of blocks along the x and y directions of the detection window
104  const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width;
105  const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height;
106 
107  const size_t window_step_x = detection_window_stride.width / block_stride.width;
108  const size_t window_step_y = detection_window_stride.height / block_stride.height;
109 
110  // Configure kernel window
111  Window win;
112  win.set(Window::DimX, Window::Dimension(0, floor_to_multiple(num_blocks_x - num_blocks_per_detection_window_x, window_step_x) + window_step_x, window_step_x));
113  win.set(Window::DimY, Window::Dimension(0, floor_to_multiple(num_blocks_y - num_blocks_per_detection_window_y, window_step_y) + window_step_y, window_step_y));
114 
115  constexpr unsigned int num_elems_read_per_iteration = 1;
116  const unsigned int num_rows_read_per_iteration = num_blocks_per_descriptor_y;
117 
118  update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration));
119 
120  ICLKernel::configure_internal(win);
121 
122  // Set config_id for enabling LWS tuning
123  _config_id = kernel_name;
124  _config_id += "_";
125  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
126  _config_id += "_";
127  _config_id += support::cpp11::to_string(input->info()->dimension(0));
128  _config_id += "_";
129  _config_id += support::cpp11::to_string(input->info()->dimension(1));
130 }
131 
132 void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue)
133 {
136 
138  do
139  {
140  unsigned int idx = 0;
141  add_2D_tensor_argument(idx, _input, slice);
142 
143  enqueue(queue, *this, slice, lws_hint());
144  }
145  while(window.slide_window_slice_2D(slice));
146 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
const Size2D & detection_window_size() const
The detection window size in pixels.
Definition: HOGInfo.cpp:101
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
TensorShape shape
Shape of the valid region.
Definition: Types.h:261
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
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.
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
void configure(const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows, const Size2D &detection_window_stride, float threshold=0.0f, uint16_t idx_class=0)
Initialise the kernel&#39;s input, HOG data-object, detection window, the stride of the detection window...
const ValidRegion valid_region
Definition: Scale.cpp:221
auto floor_to_multiple(S value, T divisor) -> decltype((value/divisor) *divisor)
Computes the largest number smaller or equal to value that is a multiple of divisor.
Definition: Utils.h:85
const Size2D & block_stride() const
The block stride in pixels.
Definition: HOGInfo.cpp:106
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
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
Interface for OpenCL Array.
Definition: ICLArray.h:35
#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
const Size2D & block_size() const
The block size in pixels.
Definition: HOGInfo.cpp:96
CLHOGDetectorKernel()
Default constructor.
std::string kernel_name
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
CLCompileContext class.
void run(const Window &window, cl::CommandQueue &queue)
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
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
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
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
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:58
virtual const HOGInfo * info() const =0
Interface to be implemented by the child class to return the HOG&#39;s metadata.
Container for valid region of a window.
Definition: Types.h:188
virtual const cl::Buffer & cl_buffer() const =0
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
size_t descriptor_size() const
The size of HOG descriptor.
Definition: HOGInfo.cpp:131
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35
Describe a multidimensional execution window.
Definition: Window.h:39
virtual size_t num_channels() const =0
The number of channels for each tensor element.
Interface for OpenCL HOG data-object.
Definition: ICLHOG.h:40
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)