Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | CLHOGDetectorKernel::CLHOGDetectorKernel() |
| 41 | : _input(nullptr), _detection_windows(), _num_detection_windows(nullptr) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void 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 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 48 | configure(CLKernelLibrary::get().get_compile_context(), input, hog, detection_windows, num_detection_windows, detection_window_stride, threshold, idx_class); |
| 49 | } |
| 50 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 51 | void CLHOGDetectorKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLHOG *hog, ICLDetectionWindowArray *detection_windows, cl::Buffer *num_detection_windows, |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 52 | const Size2D &detection_window_stride, |
| 53 | float threshold, uint16_t idx_class) |
| 54 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32); |
| 56 | ARM_COMPUTE_ERROR_ON(hog == nullptr); |
| 57 | ARM_COMPUTE_ERROR_ON(detection_windows == nullptr); |
| 58 | ARM_COMPUTE_ERROR_ON(num_detection_windows == nullptr); |
| 59 | ARM_COMPUTE_ERROR_ON((detection_window_stride.width % hog->info()->block_stride().width) != 0); |
| 60 | ARM_COMPUTE_ERROR_ON((detection_window_stride.height % hog->info()->block_stride().height) != 0); |
| 61 | |
| 62 | const Size2D &detection_window_size = hog->info()->detection_window_size(); |
| 63 | const Size2D &block_size = hog->info()->block_size(); |
| 64 | const Size2D &block_stride = hog->info()->block_stride(); |
| 65 | |
| 66 | _input = input; |
| 67 | _detection_windows = detection_windows; |
| 68 | _num_detection_windows = num_detection_windows; |
| 69 | |
| 70 | const unsigned int num_bins_per_descriptor_x = ((detection_window_size.width - block_size.width) / block_stride.width + 1) * input->info()->num_channels(); |
| 71 | const unsigned int num_blocks_per_descriptor_y = (detection_window_size.height - block_size.height) / block_stride.height + 1; |
| 72 | |
| 73 | ARM_COMPUTE_ERROR_ON((num_bins_per_descriptor_x * num_blocks_per_descriptor_y + 1) != hog->info()->descriptor_size()); |
| 74 | |
| 75 | std::stringstream args_str; |
| 76 | args_str << "-DNUM_BLOCKS_PER_DESCRIPTOR_Y=" << num_blocks_per_descriptor_y << " "; |
| 77 | args_str << "-DNUM_BINS_PER_DESCRIPTOR_X=" << num_bins_per_descriptor_x << " "; |
| 78 | args_str << "-DTHRESHOLD=" << threshold << " "; |
| 79 | args_str << "-DMAX_NUM_DETECTION_WINDOWS=" << detection_windows->max_num_values() << " "; |
| 80 | args_str << "-DIDX_CLASS=" << idx_class << " "; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | args_str << "-DDETECTION_WINDOW_WIDTH=" << detection_window_size.width << " "; |
| 82 | args_str << "-DDETECTION_WINDOW_HEIGHT=" << detection_window_size.height << " "; |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 83 | args_str << "-DDETECTION_WINDOW_STRIDE_WIDTH=" << detection_window_stride.width << " "; |
| 84 | args_str << "-DDETECTION_WINDOW_STRIDE_HEIGHT=" << detection_window_stride.height << " "; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | // Construct kernel name |
| 87 | std::set<std::string> build_opts = {}; |
| 88 | build_opts.insert(args_str.str()); |
| 89 | |
| 90 | // Create kernel |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 91 | const std::string kernel_name = std::string("hog_detector"); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 92 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | // Set static kernel arguments |
| 95 | unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input parameters |
| 96 | _kernel.setArg(idx++, hog->cl_buffer()); |
| 97 | _kernel.setArg(idx++, detection_windows->cl_buffer()); |
| 98 | _kernel.setArg(idx++, *_num_detection_windows); |
| 99 | |
| 100 | // Get the number of blocks along the x and y directions of the input tensor |
| 101 | const ValidRegion &valid_region = input->info()->valid_region(); |
| 102 | const size_t num_blocks_x = valid_region.shape[0]; |
| 103 | const size_t num_blocks_y = valid_region.shape[1]; |
| 104 | |
| 105 | // Get the number of blocks along the x and y directions of the detection window |
| 106 | const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width; |
| 107 | const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height; |
| 108 | |
| 109 | const size_t window_step_x = detection_window_stride.width / block_stride.width; |
| 110 | const size_t window_step_y = detection_window_stride.height / block_stride.height; |
| 111 | |
| 112 | // Configure kernel window |
| 113 | Window win; |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 114 | 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)); |
| 115 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | constexpr unsigned int num_elems_read_per_iteration = 1; |
| 118 | const unsigned int num_rows_read_per_iteration = num_blocks_per_descriptor_y; |
| 119 | |
| 120 | update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration)); |
| 121 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 122 | ICLKernel::configure_internal(win); |
Gary Antcliffe | fffbdbc | 2019-05-28 11:40:21 +0100 | [diff] [blame] | 123 | |
| 124 | // Set config_id for enabling LWS tuning |
| 125 | _config_id = kernel_name; |
| 126 | _config_id += "_"; |
| 127 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 128 | _config_id += "_"; |
| 129 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 130 | _config_id += "_"; |
| 131 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void CLHOGDetectorKernel::run(const Window &window, cl::CommandQueue &queue) |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 137 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 138 | |
| 139 | Window slice = window.first_slice_window_2D(); |
| 140 | do |
| 141 | { |
| 142 | unsigned int idx = 0; |
| 143 | add_2D_tensor_argument(idx, _input, slice); |
| 144 | |
Michele Di Giorgio | 6b9f388 | 2019-07-01 16:37:04 +0100 | [diff] [blame] | 145 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | } |
| 147 | while(window.slide_window_slice_2D(slice)); |
| 148 | } |