Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-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/CLFastCornersKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/CL/OpenCL.h" |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/IAccessWindow.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include <set> |
| 36 | #include <string> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | CLFastCornersKernel::CLFastCornersKernel() |
| 41 | : ICLKernel(), _input(nullptr), _output(nullptr) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | BorderSize CLFastCornersKernel::border_size() const |
| 46 | { |
| 47 | return BorderSize(3); |
| 48 | } |
| 49 | |
| 50 | void CLFastCornersKernel::configure(const ICLImage *input, ICLImage *output, float threshold, bool non_max_suppression, BorderMode border_mode) |
| 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 53 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 54 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 55 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 56 | ARM_COMPUTE_ERROR_ON_MSG(border_mode != BorderMode::UNDEFINED, "Not implemented"); |
| 57 | |
| 58 | _input = input; |
| 59 | _output = output; |
| 60 | |
| 61 | // Create build options |
| 62 | std::set<std::string> build_opts; |
| 63 | |
| 64 | if(non_max_suppression) |
| 65 | { |
| 66 | build_opts.emplace("-DUSE_MAXSUPPRESSION"); |
| 67 | } |
| 68 | |
| 69 | // Create kernel |
| 70 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("fast_corners", build_opts)); |
| 71 | |
| 72 | // Set static kernel arguments |
| 73 | unsigned int idx = 2 * num_arguments_per_2D_tensor(); // Skip the input and output parameters |
| 74 | _kernel.setArg<cl_float>(idx, static_cast<float>(threshold)); |
| 75 | |
| 76 | // Configure kernel window |
| 77 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 78 | constexpr unsigned int num_elems_read_per_iteration = 7; |
| 79 | constexpr unsigned int num_rows_read_per_iteration = 3; |
| 80 | |
| 81 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_mode == BorderMode::UNDEFINED, BorderSize(3)); |
| 82 | |
| 83 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 84 | AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration); |
| 85 | |
| 86 | update_window_and_padding(win, input_access, output_access); |
| 87 | |
| 88 | output_access.set_valid_region(win, input->info()->valid_region(), border_mode == BorderMode::UNDEFINED, border_size()); |
| 89 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 90 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void CLFastCornersKernel::run(const Window &window, cl::CommandQueue &queue) |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 96 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 97 | |
| 98 | Window slice = window.first_slice_window_2D(); |
| 99 | |
| 100 | do |
| 101 | { |
| 102 | unsigned int idx = 0; |
| 103 | add_2D_tensor_argument(idx, _input, slice); |
| 104 | add_2D_tensor_argument(idx, _output, slice); |
| 105 | enqueue(queue, *this, slice); |
| 106 | } |
| 107 | while(window.slide_window_slice_2D(slice)); |
| 108 | } |
| 109 | |
| 110 | CLCopyToArrayKernel::CLCopyToArrayKernel() |
| 111 | : ICLKernel(), _input(nullptr), _corners(nullptr), _num_buffer(nullptr) |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | void CLCopyToArrayKernel::configure(const ICLImage *input, bool update_number, ICLKeyPointArray *corners, cl::Buffer *num_buffers) |
| 116 | { |
| 117 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 118 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 119 | ARM_COMPUTE_ERROR_ON(corners == nullptr); |
| 120 | ARM_COMPUTE_ERROR_ON(num_buffers == nullptr); |
| 121 | |
| 122 | _input = input; |
| 123 | _corners = corners; |
| 124 | _num_buffer = num_buffers; |
| 125 | |
| 126 | std::set<std::string> build_opts; |
| 127 | |
| 128 | if(update_number) |
| 129 | { |
| 130 | build_opts.emplace("-DUPDATE_NUMBER"); |
| 131 | } |
| 132 | |
| 133 | // Create kernel |
| 134 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("copy_to_keypoint", build_opts)); |
| 135 | |
| 136 | //Get how many pixels skipped in the x dimension in the previous stages |
| 137 | unsigned int offset = _input->info()->valid_region().anchor.x(); |
| 138 | |
| 139 | // Set static kernel arguments |
| 140 | unsigned int idx = num_arguments_per_2D_tensor(); // Skip the input and output parameters |
Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 141 | _kernel.setArg<unsigned int>(idx++, _corners->max_num_values()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | _kernel.setArg<cl_uint>(idx++, offset); |
| 143 | _kernel.setArg(idx++, *_num_buffer); |
| 144 | _kernel.setArg(idx++, _corners->cl_buffer()); |
| 145 | |
| 146 | // Configure kernel window |
| 147 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 148 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 149 | update_window_and_padding(win, |
| 150 | AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 151 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void CLCopyToArrayKernel::run(const Window &window, cl::CommandQueue &queue) |
| 155 | { |
| 156 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 157 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 158 | |
| 159 | //Initialise the _num_buffer as it used as both input and output |
| 160 | static const unsigned int zero_init = 0; |
| 161 | queue.enqueueWriteBuffer(*_num_buffer, CL_FALSE, 0, sizeof(unsigned int), &zero_init); |
| 162 | |
| 163 | Window slice = window.first_slice_window_2D(); |
| 164 | |
| 165 | do |
| 166 | { |
| 167 | unsigned int idx = 0; |
| 168 | add_2D_tensor_argument(idx, _input, slice); |
| 169 | enqueue(queue, *this, slice); |
| 170 | } |
| 171 | while(window.slide_window_slice_2D(slice)); |
| 172 | } |