blob: caca49846f56884a43d1e42f9553b2ee4e47bbd0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
John Richardson684cb0f2018-01-09 11:17:00 +00002 * Copyright (c) 2017-2018 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
83 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("hog_detector", build_opts));
84
85 // Set static kernel arguments
86 unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input parameters
87 _kernel.setArg(idx++, hog->cl_buffer());
88 _kernel.setArg(idx++, detection_windows->cl_buffer());
89 _kernel.setArg(idx++, *_num_detection_windows);
90
91 // Get the number of blocks along the x and y directions of the input tensor
92 const ValidRegion &valid_region = input->info()->valid_region();
93 const size_t num_blocks_x = valid_region.shape[0];
94 const size_t num_blocks_y = valid_region.shape[1];
95
96 // Get the number of blocks along the x and y directions of the detection window
97 const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width;
98 const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height;
99
100 const size_t window_step_x = detection_window_stride.width / block_stride.width;
101 const size_t window_step_y = detection_window_stride.height / block_stride.height;
102
103 // Configure kernel window
104 Window win;
John Richardson684cb0f2018-01-09 11:17:00 +0000105 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));
106 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 +0100107
108 constexpr unsigned int num_elems_read_per_iteration = 1;
109 const unsigned int num_rows_read_per_iteration = num_blocks_per_descriptor_y;
110
111 update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration));
112
113 ICLKernel::configure(win);
114}
115
116void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue)
117{
118 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
119 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
120
121 Window slice = window.first_slice_window_2D();
122 do
123 {
124 unsigned int idx = 0;
125 add_2D_tensor_argument(idx, _input, slice);
126
127 enqueue(queue, *this, slice);
128 }
129 while(window.slide_window_slice_2D(slice));
130}