Compute Library
 21.02
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 configure (const CLCompileContext &compile_context, 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...
 
virtual void run_op (ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue. 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...
 
void set_wbsm_hint (const cl_int &wbsm_hint)
 Set the workgroup batch size modifier hint. More...
 
cl_int wbsm_hint () const
 Return the workgroup batch size modifier 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<unsigned int dimension_size>
void add_tensor_argument (unsigned &idx, const ICLTensor *tensor, const Window &window)
 
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...
 
- 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 38 of file CLHOGDetectorKernel.cpp.

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

◆ 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() [1/2]

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 43 of file CLHOGDetectorKernel.cpp.

References CLKernelLibrary::get().

45 {
46  configure(CLKernelLibrary::get().get_compile_context(), input, hog, detection_windows, num_detection_windows, detection_window_stride, threshold, idx_class);
47 }
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
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...
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35

◆ configure() [2/2]

void configure ( const CLCompileContext compile_context,
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]compile_contextThe compile context to be used.
[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 49 of file CLHOGDetectorKernel.cpp.

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(), Size2D::height, IHOG::info(), ITensor::info(), arm_compute::test::validation::input, kernel_name, 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::support::cpp11::to_string(), arm_compute::update_window_and_padding(), arm_compute::test::validation::valid_region, ITensorInfo::valid_region(), and Size2D::width.

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 }
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:261
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:466
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
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
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
#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
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
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: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 ...
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.

◆ 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.

Reimplemented from ICLKernel.

Definition at line 132 of file CLHOGDetectorKernel.cpp.

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().

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
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
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)

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