Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-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/runtime/CL/functions/CLFastCorners.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/OpenCL.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLFastCornersKernel.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cstring> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 39 | CLFastCorners::CLFastCorners(std::shared_ptr<IMemoryManager> memory_manager) |
| 40 | : _memory_group(std::move(memory_manager)), |
| 41 | _fast_corners_kernel(), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | _suppr_func(), |
| 43 | _copy_array_kernel(), |
| 44 | _output(), |
| 45 | _suppr(), |
| 46 | _win(), |
| 47 | _non_max(false), |
| 48 | _num_corners(nullptr), |
| 49 | _num_buffer(), |
| 50 | _corners(nullptr), |
| 51 | _constant_border_value(0) |
| 52 | { |
| 53 | } |
| 54 | |
Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 55 | void CLFastCorners::configure(const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value) |
| 57 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 58 | configure(CLKernelLibrary::get().get_compile_context(), input, threshold, nonmax_suppression, corners, num_corners, border_mode, constant_border_value); |
| 59 | } |
| 60 | |
| 61 | void CLFastCorners::configure(const CLCompileContext &compile_context, const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners, |
| 62 | unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value) |
| 63 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 65 | ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode); |
| 66 | ARM_COMPUTE_ERROR_ON(nullptr == corners); |
| 67 | ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255); |
| 68 | |
| 69 | TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8); |
| 70 | _output.allocator()->init(tensor_info); |
| 71 | |
| 72 | _non_max = nonmax_suppression; |
| 73 | _num_corners = num_corners; |
| 74 | _corners = corners; |
| 75 | _num_buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int)); |
| 76 | _constant_border_value = constant_border_value; |
| 77 | |
| 78 | const bool update_number = (nullptr != _num_corners); |
| 79 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 80 | _memory_group.manage(&_output); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | _fast_corners_kernel.configure(compile_context, input, &_output, threshold, nonmax_suppression, border_mode); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | |
| 83 | if(!_non_max) |
| 84 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 85 | _copy_array_kernel.configure(compile_context, &_output, update_number, _corners, &_num_buffer); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | } |
| 87 | else |
| 88 | { |
| 89 | _suppr.allocator()->init(tensor_info); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 90 | _memory_group.manage(&_suppr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 92 | _suppr_func.configure(compile_context, &_output, &_suppr, border_mode); |
| 93 | _copy_array_kernel.configure(compile_context, &_suppr, update_number, _corners, &_num_buffer); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | |
| 95 | _suppr.allocator()->allocate(); |
| 96 | } |
| 97 | |
| 98 | // Allocate intermediate tensors |
| 99 | _output.allocator()->allocate(); |
| 100 | } |
| 101 | |
| 102 | void CLFastCorners::run() |
| 103 | { |
| 104 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 105 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 106 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 107 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | if(_non_max) |
| 109 | { |
| 110 | ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function"); |
| 111 | const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size())); |
| 112 | memset(out_buffer, 0, _output.info()->total_size()); |
| 113 | q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer); |
| 114 | } |
| 115 | |
| 116 | CLScheduler::get().enqueue(_fast_corners_kernel, false); |
| 117 | |
| 118 | if(_non_max) |
| 119 | { |
| 120 | _suppr_func.run(); |
| 121 | } |
| 122 | |
| 123 | CLScheduler::get().enqueue(_copy_array_kernel, false); |
| 124 | |
| 125 | unsigned int get_num_corners = 0; |
| 126 | q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners); |
| 127 | |
| 128 | size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values()); |
| 129 | |
| 130 | _corners->resize(corner_size); |
| 131 | |
| 132 | if(_num_corners != nullptr) |
| 133 | { |
| 134 | *_num_corners = get_num_corners; |
| 135 | } |
| 136 | |
| 137 | q.flush(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | } |