blob: c003df4fd26d295f79e1e83f01ce7568e8f1225c [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/CLHOGDetectorKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLHOG.h"
29#include "arm_compute/core/CL/ICLTensor.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
38using namespace arm_compute;
39
40CLHOGDetectorKernel::CLHOGDetectorKernel()
41 : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr)
42{
43}
44
45void CLHOGDetectorKernel::configure(const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows, const Size2D &detection_window_stride,
46 float threshold, uint16_t idx_class)
47{
48 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32);
49 ARM_COMPUTE_ERROR_ON(hog == nullptr);
50 ARM_COMPUTE_ERROR_ON(detection_windows == nullptr);
51 ARM_COMPUTE_ERROR_ON(num_detection_windows == nullptr);
52 ARM_COMPUTE_ERROR_ON((detection_window_stride.width % hog->info()->block_stride().width) != 0);
53 ARM_COMPUTE_ERROR_ON((detection_window_stride.height % hog->info()->block_stride().height) != 0);
54
55 const Size2D &detection_window_size = hog->info()->detection_window_size();
56 const Size2D &block_size = hog->info()->block_size();
57 const Size2D &block_stride = hog->info()->block_stride();
58
59 _input = input;
60 _detection_windows = detection_windows;
61 _num_detection_windows = num_detection_windows;
62
63 const unsigned int num_bins_per_descriptor_x = ((detection_window_size.width - block_size.width) / block_stride.width + 1) * input->info()->num_channels();
64 const unsigned int num_blocks_per_descriptor_y = (detection_window_size.height - block_size.height) / block_stride.height + 1;
65
66 ARM_COMPUTE_ERROR_ON((num_bins_per_descriptor_x * num_blocks_per_descriptor_y + 1) != hog->info()->descriptor_size());
67
68 std::stringstream args_str;
69 args_str << "-DNUM_BLOCKS_PER_DESCRIPTOR_Y=" << num_blocks_per_descriptor_y << " ";
70 args_str << "-DNUM_BINS_PER_DESCRIPTOR_X=" << num_bins_per_descriptor_x << " ";
71 args_str << "-DTHRESHOLD=" << threshold << " ";
72 args_str << "-DMAX_NUM_DETECTION_WINDOWS=" << detection_windows->max_num_values() << " ";
73 args_str << "-DIDX_CLASS=" << idx_class << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " ";
75 args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " ";
John Richardson684cb0f2018-01-09 11:17:00 +000076 args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " ";
77 args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 // Construct kernel name
80 std::set<std::string> build_opts = {};
81 build_opts.insert(args_str.str());
82
83 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010084 const std::string kernel_name = std::string("hog_detector");
85 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 // Set static kernel arguments
88 unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input parameters
89 _kernel.setArg(idx++, hog->cl_buffer());
90 _kernel.setArg(idx++, detection_windows->cl_buffer());
91 _kernel.setArg(idx++, *_num_detection_windows);
92
93 // Get the number of blocks along the x and y directions of the input tensor
94 const ValidRegion &valid_region = input->info()->valid_region();
95 const size_t num_blocks_x = valid_region.shape[0];
96 const size_t num_blocks_y = valid_region.shape[1];
97
98 // Get the number of blocks along the x and y directions of the detection window
99 const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width;
100 const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height;
101
102 const size_t window_step_x = detection_window_stride.width / block_stride.width;
103 const size_t window_step_y = detection_window_stride.height / block_stride.height;
104
105 // Configure kernel window
106 Window win;
John Richardson684cb0f2018-01-09 11:17:00 +0000107 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));
108 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110 constexpr unsigned int num_elems_read_per_iteration = 1;
111 const unsigned int num_rows_read_per_iteration = num_blocks_per_descriptor_y;
112
113 update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration));
114
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100115 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100116
117 // Set config_id for enabling LWS tuning
118 _config_id = kernel_name;
119 _config_id += "_";
120 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
121 _config_id += "_";
122 _config_id += support::cpp11::to_string(input->info()->dimension(0));
123 _config_id += "_";
124 _config_id += support::cpp11::to_string(input->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125}
126
127void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue)
128{
129 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
130 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
131
132 Window slice = window.first_slice_window_2D();
133 do
134 {
135 unsigned int idx = 0;
136 add_2D_tensor_argument(idx, _input, slice);
137
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100138 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 }
140 while(window.slide_window_slice_2D(slice));
141}