Compute Library
 19.08
CLHOGDetectorKernel Class Reference

OpenCL kernel to perform HOG detector kernel using linear SVM. More...

#include <CLHOGDetectorKernel.h>

Collaboration diagram for CLHOGDetectorKernel:
[legend]

Public Member Functions

 CLHOGDetectorKernel ()
 Default constructor. More...
 
 CLHOGDetectorKernel (const CLHOGDetectorKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLHOGDetectorKerneloperator= (const CLHOGDetectorKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLHOGDetectorKernel (CLHOGDetectorKernel &&)=default
 Allow instances of this class to be moved. More...
 
CLHOGDetectorKerneloperator= (CLHOGDetectorKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~CLHOGDetectorKernel ()=default
 Default destructor. More...
 
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's input, HOG data-object, detection window, the stride of the detection window, the threshold and index of the object to detect. More...
 
void run (const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. More...
 
- Public Member Functions inherited from ICLKernel
 ICLKernel ()
 Constructor. More...
 
cl::Kernel & kernel ()
 Returns a reference to the OpenCL kernel of this object. More...
 
template<typename T >
void add_1D_array_argument (unsigned int &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed 1D array's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_1D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_2D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_2D_tensor_argument_if (bool cond, unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx if the condition is true. More...
 
void add_3D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_4D_tensor_argument (unsigned int &idx, const ICLTensor *tensor, const Window &window)
 Add the passed 4D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
template<typename T >
void add_argument (unsigned int &idx, T value)
 Add the passed parameters to the object's kernel's arguments starting from the index idx. More...
 
void set_lws_hint (const cl::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
cl::NDRange lws_hint () const
 Return the Local-Workgroup-Size hint. More...
 
const std::string & config_id () const
 Get the configuration ID. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
void set_target (cl::Device &device)
 Set the targeted GPU architecture according to the CL device. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
size_t get_max_workgroup_size ()
 Get the maximum workgroup size for the device the CLKernelLibrary uses. More...
 
template<typename T , unsigned int dimension_size>
void add_array_argument (unsigned &idx, const ICLArray< T > *array, const Strides &strides, unsigned int num_dimensions, const Window &window)
 Add the passed array's parameters to the object's kernel's arguments starting from the index idx. More...
 
template<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from ICLKernel
static constexpr unsigned int num_arguments_per_1D_array ()
 Returns the number of arguments enqueued per 1D array object. More...
 
static constexpr unsigned int num_arguments_per_1D_tensor ()
 Returns the number of arguments enqueued per 1D tensor object. More...
 
static constexpr unsigned int num_arguments_per_2D_tensor ()
 Returns the number of arguments enqueued per 2D tensor object. More...
 
static constexpr unsigned int num_arguments_per_3D_tensor ()
 Returns the number of arguments enqueued per 3D tensor object. More...
 
static constexpr unsigned int num_arguments_per_4D_tensor ()
 Returns the number of arguments enqueued per 4D tensor object. More...
 
static cl::NDRange gws_from_window (const Window &window)
 Get the global work size given an execution window. More...
 

Detailed Description

OpenCL kernel to perform HOG detector kernel using linear SVM.

Definition at line 42 of file CLHOGDetectorKernel.h.

Constructor & Destructor Documentation

◆ CLHOGDetectorKernel() [1/3]

Default constructor.

Definition at line 39 of file CLHOGDetectorKernel.cpp.

40  : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr)
41 {
42 }

◆ CLHOGDetectorKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ CLHOGDetectorKernel() [3/3]

Allow instances of this class to be moved.

◆ ~CLHOGDetectorKernel()

~CLHOGDetectorKernel ( )
default

Default destructor.

Member Function Documentation

◆ configure()

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's input, HOG data-object, detection window, the stride of the detection window, the threshold and index of the object to detect.

Parameters
[in]inputInput tensor which stores the HOG descriptor obtained with CLHOGOrientationBinningKernel. Data type supported: F32. Number of channels supported: equal to the number of histogram bins per block
[in]hogHOG data object used by CLHOGOrientationBinningKernel and CLHOGBlockNormalizationKernel
[out]detection_windowsArray of DetectionWindow. This array stores all the detected objects
[in]num_detection_windowsNumber of detected objects
[in]detection_window_strideDistance in pixels between 2 consecutive detection windows in x and y directions. It must be multiple of the hog->info()->block_stride()
[in]threshold(Optional) Threshold for the distance between features and SVM classifying plane
[in]idx_class(Optional) Index of the class used for evaluating which class the detection window belongs to

Definition at line 44 of file CLHOGDetectorKernel.cpp.

46 {
48  ARM_COMPUTE_ERROR_ON(hog == nullptr);
49  ARM_COMPUTE_ERROR_ON(detection_windows == nullptr);
50  ARM_COMPUTE_ERROR_ON(num_detection_windows == nullptr);
51  ARM_COMPUTE_ERROR_ON((detection_window_stride.width % hog->info()->block_stride().width) != 0);
52  ARM_COMPUTE_ERROR_ON((detection_window_stride.height % hog->info()->block_stride().height) != 0);
53 
54  const Size2D &detection_window_size = hog->info()->detection_window_size();
55  const Size2D &block_size = hog->info()->block_size();
56  const Size2D &block_stride = hog->info()->block_stride();
57 
58  _input = input;
59  _detection_windows = detection_windows;
60  _num_detection_windows = num_detection_windows;
61 
62  const unsigned int num_bins_per_descriptor_x = ((detection_window_size.width - block_size.width) / block_stride.width + 1) * input->info()->num_channels();
63  const unsigned int num_blocks_per_descriptor_y = (detection_window_size.height - block_size.height) / block_stride.height + 1;
64 
65  ARM_COMPUTE_ERROR_ON((num_bins_per_descriptor_x * num_blocks_per_descriptor_y + 1) != hog->info()->descriptor_size());
66 
67  std::stringstream args_str;
68  args_str << "-DNUM_BLOCKS_PER_DESCRIPTOR_Y=" << num_blocks_per_descriptor_y << " ";
69  args_str << "-DNUM_BINS_PER_DESCRIPTOR_X=" << num_bins_per_descriptor_x << " ";
70  args_str << "-DTHRESHOLD=" << threshold << " ";
71  args_str << "-DMAX_NUM_DETECTION_WINDOWS=" << detection_windows->max_num_values() << " ";
72  args_str << "-DIDX_CLASS=" << idx_class << " ";
73  args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " ";
74  args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " ";
75  args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " ";
76  args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " ";
77 
78  // Construct kernel name
79  std::set<std::string> build_opts = {};
80  build_opts.insert(args_str.str());
81 
82  // Create kernel
83  const std::string kernel_name = std::string("hog_detector");
84  _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
85 
86  // Set static kernel arguments
87  unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input parameters
88  _kernel.setArg(idx++, hog->cl_buffer());
89  _kernel.setArg(idx++, detection_windows->cl_buffer());
90  _kernel.setArg(idx++, *_num_detection_windows);
91 
92  // Get the number of blocks along the x and y directions of the input tensor
93  const ValidRegion &valid_region = input->info()->valid_region();
94  const size_t num_blocks_x = valid_region.shape[0];
95  const size_t num_blocks_y = valid_region.shape[1];
96 
97  // Get the number of blocks along the x and y directions of the detection window
98  const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width;
99  const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height;
100 
101  const size_t window_step_x = detection_window_stride.width / block_stride.width;
102  const size_t window_step_y = detection_window_stride.height / block_stride.height;
103 
104  // Configure kernel window
105  Window win;
106  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));
107  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));
108 
109  constexpr unsigned int num_elems_read_per_iteration = 1;
110  const unsigned int num_rows_read_per_iteration = num_blocks_per_descriptor_y;
111 
112  update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration));
113 
114  ICLKernel::configure_internal(win);
115 
116  // Set config_id for enabling LWS tuning
117  _config_id = kernel_name;
118  _config_id += "_";
119  _config_id += lower_string(string_from_data_type(input->info()->data_type()));
120  _config_id += "_";
121  _config_id += support::cpp11::to_string(input->info()->dimension(0));
122  _config_id += "_";
123  _config_id += support::cpp11::to_string(input->info()->dimension(1));
124 }
const Size2D & detection_window_size() const
The detection window size in pixels.
Definition: HOGInfo.cpp:101
TensorShape shape
Shape of the valid region.
Definition: Types.h:247
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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:337
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:327
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:80
const Size2D & block_stride() const
The block stride in pixels.
Definition: HOGInfo.cpp:106
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:93
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:144
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: Helpers.h:402
const Size2D & block_size() const
The block size in pixels.
Definition: HOGInfo.cpp:96
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:192
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:92
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 ...
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's metadata.
Container for valid region of a window.
Definition: Types.h:174
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.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:691

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN, HOGInfo::block_size(), HOGInfo::block_stride(), ICLHOG::cl_buffer(), ICLArray< T >::cl_buffer(), arm_compute::create_kernel(), ITensorInfo::data_type(), HOGInfo::descriptor_size(), HOGInfo::detection_window_size(), ITensorInfo::dimension(), Window::DimX, Window::DimY, arm_compute::F32, arm_compute::floor_to_multiple(), CLKernelLibrary::get(), Size2D::height, IHOG::info(), ITensor::info(), arm_compute::lower_string(), IArray< T >::max_num_values(), ICLKernel::num_arguments_per_2D_tensor(), ITensorInfo::num_channels(), Window::set(), ValidRegion::shape, arm_compute::string_from_data_type(), arm_compute::test::validation::reference::threshold(), arm_compute::support::cpp11::to_string(), arm_compute::update_window_and_padding(), arm_compute::test::validation::valid_region, ITensorInfo::valid_region(), and Size2D::width.

Referenced by CLHOGDetector::configure().

◆ operator=() [1/2]

CLHOGDetectorKernel& operator= ( const CLHOGDetectorKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

CLHOGDetectorKernel& operator= ( CLHOGDetectorKernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
cl::CommandQueue &  queue 
)
virtual

Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.

Note
The queue is not flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.

Implements ICLKernel.

Definition at line 126 of file CLHOGDetectorKernel.cpp.

127 {
130 
132  do
133  {
134  unsigned int idx = 0;
135  add_2D_tensor_argument(idx, _input, slice);
136 
137  enqueue(queue, *this, slice, lws_hint());
138  }
140 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:267
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:39
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:247
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:307
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx.
Definition: ICLKernel.h:134
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

References ICLKernel::add_2D_tensor_argument(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, arm_compute::enqueue(), Window::first_slice_window_2D(), ICLKernel::lws_hint(), arm_compute::test::validation::reference::slice(), Window::slide_window_slice_2D(), and IKernel::window().


The documentation for this class was generated from the following files: