blob: 160be988f955c936783d554cdf2e3dc5b09c0c8d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gary Antcliffefffbdbc2019-05-28 11:40:21 +01002 * Copyright (c) 2017-2019 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"
36
37using namespace arm_compute;
38
39CLHOGDetectorKernel::CLHOGDetectorKernel()
40 : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr)
41{
42}
43
44void CLHOGDetectorKernel::configure(const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows, const Size2D &detection_window_stride,
45 float threshold, uint16_t idx_class)
46{
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32);
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 << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " ";
74 args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " ";
John Richardson684cb0f2018-01-09 11:17:00 +000075 args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " ";
76 args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " ";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
78 // Construct kernel name
79 std::set<std::string> build_opts = {};
80 build_opts.insert(args_str.str());
81
82 // Create kernel
Gary Antcliffefffbdbc2019-05-28 11:40:21 +010083 const std::string kernel_name = std::string("hog_detector");
84 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085
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;
John Richardson684cb0f2018-01-09 11:17:00 +0000106 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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
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
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100114 ICLKernel::configure_internal(win);
Gary Antcliffefffbdbc2019-05-28 11:40:21 +0100115
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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124}
125
126void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue)
127{
128 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
129 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
130
131 Window slice = window.first_slice_window_2D();
132 do
133 {
134 unsigned int idx = 0;
135 add_2D_tensor_argument(idx, _input, slice);
136
137 enqueue(queue, *this, slice);
138 }
139 while(window.slide_window_slice_2D(slice));
140}