Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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/CLHOGDescriptorKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.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 | |
| 37 | #include <set> |
| 38 | #include <sstream> |
| 39 | #include <string> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | CLHOGOrientationBinningKernel::CLHOGOrientationBinningKernel() |
| 44 | : _input_magnitude(nullptr), _input_phase(nullptr), _output(nullptr), _cell_size() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void CLHOGOrientationBinningKernel::configure(const ICLTensor *input_magnitude, const ICLTensor *input_phase, ICLTensor *output, const HOGInfo *hog_info) |
| 49 | { |
| 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_magnitude, 1, DataType::S16); |
| 51 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input_phase, 1, DataType::U8); |
| 52 | ARM_COMPUTE_ERROR_ON(hog_info == nullptr); |
| 53 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins(), DataType::F32); |
| 54 | ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimX) != input_phase->info()->dimension(Window::DimX)); |
| 55 | ARM_COMPUTE_ERROR_ON(input_magnitude->info()->dimension(Window::DimY) != input_phase->info()->dimension(Window::DimY)); |
| 56 | |
| 57 | _input_magnitude = input_magnitude; |
| 58 | _input_phase = input_phase; |
| 59 | _output = output; |
| 60 | _cell_size = hog_info->cell_size(); |
| 61 | |
| 62 | float phase_scale = (PhaseType::SIGNED == hog_info->phase_type() ? hog_info->num_bins() / 360.0f : hog_info->num_bins() / 180.0f); |
| 63 | phase_scale *= (PhaseType::SIGNED == hog_info->phase_type() ? 360.0f / 255.0f : 1.0f); |
| 64 | |
| 65 | std::stringstream args_str; |
| 66 | args_str << "-DCELL_WIDTH=" << hog_info->cell_size().width << " "; |
| 67 | args_str << "-DCELL_HEIGHT=" << hog_info->cell_size().height << " "; |
| 68 | args_str << "-DNUM_BINS=" << hog_info->num_bins() << " "; |
| 69 | args_str << "-DPHASE_SCALE=" << phase_scale << " "; |
| 70 | |
| 71 | // Construct kernel name |
| 72 | std::set<std::string> build_opts = {}; |
| 73 | build_opts.insert(args_str.str()); |
| 74 | |
| 75 | // Create kernel |
| 76 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("hog_orientation_binning", build_opts)); |
| 77 | |
| 78 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 79 | constexpr unsigned int num_elems_read_per_iteration = 1; |
| 80 | const unsigned int num_rows_read_per_iteration = hog_info->cell_size().height; |
| 81 | constexpr unsigned int num_elems_written_per_iteration = 1; |
| 82 | |
| 83 | // Configure kernel window |
| 84 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 85 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration); |
| 86 | |
| 87 | update_window_and_padding(win, |
| 88 | AccessWindowRectangle(input_magnitude->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 89 | AccessWindowRectangle(input_phase->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 90 | output_access); |
| 91 | |
| 92 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 93 | |
| 94 | ICLKernel::configure(win); |
| 95 | } |
| 96 | |
| 97 | void CLHOGOrientationBinningKernel::run(const Window &window, cl::CommandQueue &queue) |
| 98 | { |
| 99 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 100 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 101 | |
| 102 | Window slice = window.first_slice_window_2D(); |
| 103 | do |
| 104 | { |
| 105 | // Compute slice for the magnitude and phase tensors |
| 106 | Window slice_mag_phase = window.first_slice_window_2D(); |
| 107 | slice_mag_phase.set(Window::DimX, Window::Dimension(window.x().start() * _cell_size.width, window.x().start() * _cell_size.width, _cell_size.width)); |
| 108 | slice_mag_phase.set(Window::DimY, Window::Dimension(window.y().start() * _cell_size.height, window.y().start() * _cell_size.height, _cell_size.height)); |
| 109 | |
| 110 | unsigned int idx = 0; |
| 111 | add_2D_tensor_argument(idx, _input_magnitude, slice_mag_phase); |
| 112 | add_2D_tensor_argument(idx, _input_phase, slice_mag_phase); |
| 113 | add_2D_tensor_argument(idx, _output, slice); |
| 114 | |
| 115 | enqueue(queue, *this, slice); |
| 116 | } |
| 117 | while(window.slide_window_slice_2D(slice)); |
| 118 | } |
| 119 | |
| 120 | CLHOGBlockNormalizationKernel::CLHOGBlockNormalizationKernel() |
| 121 | : _input(nullptr), _output(nullptr), _num_cells_per_block_stride() |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | void CLHOGBlockNormalizationKernel::configure(const ICLTensor *input, ICLTensor *output, const HOGInfo *hog_info) |
| 126 | { |
| 127 | ARM_COMPUTE_ERROR_ON(hog_info == nullptr); |
| 128 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, hog_info->num_bins(), DataType::F32); |
| 129 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32); |
| 130 | |
| 131 | // Number of cells per block |
| 132 | const Size2D num_cells_per_block(hog_info->block_size().width / hog_info->cell_size().width, |
| 133 | hog_info->block_size().height / hog_info->cell_size().height); |
| 134 | |
| 135 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, hog_info->num_bins() * num_cells_per_block.area(), DataType::F32); |
| 136 | |
| 137 | // Number of cells per block stride |
| 138 | const Size2D num_cells_per_block_stride(hog_info->block_stride().width / hog_info->cell_size().width, |
| 139 | hog_info->block_stride().height / hog_info->cell_size().height); |
| 140 | |
| 141 | _input = input; |
| 142 | _output = output; |
| 143 | _num_cells_per_block_stride = num_cells_per_block_stride; |
| 144 | |
| 145 | std::stringstream args_str; |
| 146 | args_str << "-DL2_HYST_THRESHOLD=" << hog_info->l2_hyst_threshold() << " "; |
| 147 | args_str << "-DNUM_CELLS_PER_BLOCK_HEIGHT=" << num_cells_per_block.height << " "; |
| 148 | args_str << "-DNUM_BINS_PER_BLOCK_X=" << num_cells_per_block.width *hog_info->num_bins() << " "; |
| 149 | args_str << "-DNUM_BINS_PER_BLOCK=" << _output->info()->num_channels() << " "; |
| 150 | args_str << "-DL2_NORM=" << static_cast<int>(HOGNormType::L2_NORM) << " "; |
| 151 | args_str << "-DL1_NORM=" << static_cast<int>(HOGNormType::L1_NORM) << " "; |
| 152 | args_str << "-DL2HYS_NORM=" << static_cast<int>(HOGNormType::L2HYS_NORM) << " "; |
| 153 | args_str << "-DHOG_NORM_TYPE=" << static_cast<int>(hog_info->normalization_type()) << " "; |
| 154 | |
| 155 | // Construct kernel name |
| 156 | std::set<std::string> build_opts = {}; |
| 157 | build_opts.insert(args_str.str()); |
| 158 | |
| 159 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("hog_block_normalization", build_opts)); |
| 160 | |
| 161 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 162 | constexpr unsigned int num_elems_read_per_iteration = 1; |
| 163 | const unsigned int num_rows_read_per_iteration = num_cells_per_block.height; |
| 164 | constexpr unsigned int num_elems_written_per_iteration = 1; |
| 165 | const unsigned int num_rows_written_per_iteration = num_cells_per_block.height; |
| 166 | |
| 167 | // Configure kernel window |
| 168 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 169 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration); |
| 170 | |
| 171 | update_window_and_padding(win, |
| 172 | AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 173 | output_access); |
| 174 | |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 175 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
| 177 | ICLKernel::configure(win); |
| 178 | } |
| 179 | |
| 180 | void CLHOGBlockNormalizationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 183 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 184 | |
| 185 | Window slice = window.first_slice_window_2D(); |
| 186 | do |
| 187 | { |
| 188 | // Compute slice for the magnitude and phase tensors |
| 189 | Window slice_in = window.first_slice_window_2D(); |
| 190 | slice_in.set_dimension_step(Window::DimX, _num_cells_per_block_stride.width); |
| 191 | slice_in.set_dimension_step(Window::DimY, _num_cells_per_block_stride.height); |
| 192 | |
| 193 | unsigned int idx = 0; |
| 194 | add_2D_tensor_argument(idx, _input, slice_in); |
| 195 | add_2D_tensor_argument(idx, _output, slice); |
| 196 | |
| 197 | enqueue(queue, *this, slice); |
| 198 | } |
| 199 | while(window.slide_window_slice_2D(slice)); |
| 200 | } |