blob: 6e14996732308a2864f1cd9180aa488c2f78066b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000034#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36using namespace arm_compute;
37
38CLHOGDetectorKernel::CLHOGDetectorKernel()
39 : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr)
40{
41}
42
43void 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{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010046 configure(CLKernelLibrary::get().get_compile_context(), input, hog, detection_windows, num_detection_windows, detection_window_stride, threshold, idx_class);
47}
48
Manuel Bottini679fc962020-04-21 16:08:53 +010049void CLHOGDetectorKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010050 const Size2D &detection_window_stride,
51 float threshold, uint16_t idx_class)
52{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32);
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 << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " ";
80 args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " ";
John Richardson684cb0f2018-01-09 11:17:00 +000081 args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " ";
82 args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
84 // Construct kernel name
85 std::set<std::string> build_opts = {};
86 build_opts.insert(args_str.str());
87
88 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010089 const std::string kernel_name = std::string("hog_detector");
Manuel Bottini4c6bd512020-04-08 10:15:51 +010090 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
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;
John Richardson684cb0f2018-01-09 11:17:00 +0000112 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100120 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100121
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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130}
131
132void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue)
133{
134 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
135 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
136
137 Window slice = window.first_slice_window_2D();
138 do
139 {
140 unsigned int idx = 0;
141 add_2D_tensor_argument(idx, _input, slice);
142
Michele Di Giorgio6b9f3882019-07-01 16:37:04 +0100143 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 }
145 while(window.slide_window_slice_2D(slice));
146}