Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2016-2019 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 | { |
| 58 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 59 | ARM_COMPUTE_ERROR_ON(BorderMode::UNDEFINED != border_mode); |
| 60 | ARM_COMPUTE_ERROR_ON(nullptr == corners); |
| 61 | ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255); |
| 62 | |
| 63 | TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8); |
| 64 | _output.allocator()->init(tensor_info); |
| 65 | |
| 66 | _non_max = nonmax_suppression; |
| 67 | _num_corners = num_corners; |
| 68 | _corners = corners; |
| 69 | _num_buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int)); |
| 70 | _constant_border_value = constant_border_value; |
| 71 | |
| 72 | const bool update_number = (nullptr != _num_corners); |
| 73 | |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 74 | _memory_group.manage(&_output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | _fast_corners_kernel.configure(input, &_output, threshold, nonmax_suppression, border_mode); |
| 76 | |
| 77 | if(!_non_max) |
| 78 | { |
Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 79 | _copy_array_kernel.configure(&_output, update_number, _corners, &_num_buffer); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | } |
| 81 | else |
| 82 | { |
| 83 | _suppr.allocator()->init(tensor_info); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 84 | _memory_group.manage(&_suppr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | _suppr_func.configure(&_output, &_suppr, border_mode); |
Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 87 | _copy_array_kernel.configure(&_suppr, update_number, _corners, &_num_buffer); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | |
| 89 | _suppr.allocator()->allocate(); |
| 90 | } |
| 91 | |
| 92 | // Allocate intermediate tensors |
| 93 | _output.allocator()->allocate(); |
| 94 | } |
| 95 | |
| 96 | void CLFastCorners::run() |
| 97 | { |
| 98 | cl::CommandQueue q = CLScheduler::get().queue(); |
| 99 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame^] | 100 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 5701e2a | 2017-09-18 17:43:33 +0100 | [diff] [blame] | 101 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | if(_non_max) |
| 103 | { |
| 104 | ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function"); |
| 105 | const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size())); |
| 106 | memset(out_buffer, 0, _output.info()->total_size()); |
| 107 | q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer); |
| 108 | } |
| 109 | |
| 110 | CLScheduler::get().enqueue(_fast_corners_kernel, false); |
| 111 | |
| 112 | if(_non_max) |
| 113 | { |
| 114 | _suppr_func.run(); |
| 115 | } |
| 116 | |
| 117 | CLScheduler::get().enqueue(_copy_array_kernel, false); |
| 118 | |
| 119 | unsigned int get_num_corners = 0; |
| 120 | q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners); |
| 121 | |
| 122 | size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values()); |
| 123 | |
| 124 | _corners->resize(corner_size); |
| 125 | |
| 126 | if(_num_corners != nullptr) |
| 127 | { |
| 128 | *_num_corners = get_num_corners; |
| 129 | } |
| 130 | |
| 131 | q.flush(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | } |