Compute Library
 21.02
CLFastCorners.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 Arm Limited.
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  */
25 
27 #include "arm_compute/core/Error.h"
34 
35 #include <algorithm>
36 #include <cstring>
37 
38 using namespace arm_compute;
39 
40 CLFastCorners::CLFastCorners(std::shared_ptr<IMemoryManager> memory_manager)
41  : _memory_group(std::move(memory_manager)),
42  _fast_corners_kernel(std::make_unique<CLFastCornersKernel>()),
43  _suppr_func(),
44  _copy_array_kernel(std::make_unique<CLCopyToArrayKernel>()),
45  _output(),
46  _suppr(),
47  _win(),
48  _non_max(false),
49  _num_corners(nullptr),
50  _num_buffer(),
51  _corners(nullptr),
52  _constant_border_value(0)
53 {
54 }
55 
57 
58 void CLFastCorners::configure(const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners,
59  unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value)
60 {
61  configure(CLKernelLibrary::get().get_compile_context(), input, threshold, nonmax_suppression, corners, num_corners, border_mode, constant_border_value);
62 }
63 
64 void CLFastCorners::configure(const CLCompileContext &compile_context, const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners,
65  unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value)
66 {
69  ARM_COMPUTE_ERROR_ON(nullptr == corners);
70  ARM_COMPUTE_ERROR_ON(threshold < 1 && threshold > 255);
71 
72  TensorInfo tensor_info(input->info()->tensor_shape(), 1, DataType::U8);
73  _output.allocator()->init(tensor_info);
74 
75  _non_max = nonmax_suppression;
76  _num_corners = num_corners;
77  _corners = corners;
78  _num_buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, sizeof(unsigned int));
79  _constant_border_value = constant_border_value;
80 
81  const bool update_number = (nullptr != _num_corners);
82 
83  _memory_group.manage(&_output);
84  _fast_corners_kernel->configure(compile_context, input, &_output, threshold, nonmax_suppression, border_mode);
85 
86  if(!_non_max)
87  {
88  _copy_array_kernel->configure(compile_context, &_output, update_number, _corners, &_num_buffer);
89  }
90  else
91  {
92  _suppr.allocator()->init(tensor_info);
93  _memory_group.manage(&_suppr);
94 
95  _suppr_func.configure(compile_context, &_output, &_suppr, border_mode);
96  _copy_array_kernel->configure(compile_context, &_suppr, update_number, _corners, &_num_buffer);
97 
98  _suppr.allocator()->allocate();
99  }
100 
101  // Allocate intermediate tensors
102  _output.allocator()->allocate();
103 }
104 
106 {
107  cl::CommandQueue q = CLScheduler::get().queue();
108 
109  MemoryGroupResourceScope scope_mg(_memory_group);
110 
111  if(_non_max)
112  {
113  ARM_COMPUTE_ERROR_ON_MSG(_output.cl_buffer().get() == nullptr, "Unconfigured function");
114  const auto out_buffer = static_cast<unsigned char *>(q.enqueueMapBuffer(_output.cl_buffer(), CL_TRUE, CL_MAP_WRITE, 0, _output.info()->total_size()));
115  memset(out_buffer, 0, _output.info()->total_size());
116  q.enqueueUnmapMemObject(_output.cl_buffer(), out_buffer);
117  }
118 
119  CLScheduler::get().enqueue(*_fast_corners_kernel, false);
120 
121  if(_non_max)
122  {
123  _suppr_func.run();
124  }
125 
126  CLScheduler::get().enqueue(*_copy_array_kernel, false);
127 
128  unsigned int get_num_corners = 0;
129  q.enqueueReadBuffer(_num_buffer, CL_TRUE, 0, sizeof(unsigned int), &get_num_corners);
130 
131  size_t corner_size = std::min(static_cast<size_t>(get_num_corners), _corners->max_num_values());
132 
133  _corners->resize(corner_size);
134 
135  if(_num_corners != nullptr)
136  {
137  *_num_corners = get_num_corners;
138  }
139 
140  q.flush();
141 }
BorderMode
Methods available to handle borders.
Definition: Types.h:265
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Definition: CLTensor.cpp:41
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:856
static CLScheduler & get()
Access the scheduler singleton.
void run() override
Run the kernels contained in the function.
const cl::Buffer & cl_buffer() const override
Interface to be implemented by the child class to return a reference to the OpenCL buffer containing ...
Definition: CLTensor.cpp:51
1 channel, 1 U8 per channel
void resize(size_t num)
Resizes the array to contain "num" elements.
Definition: IArray.h:128
CLFastCorners(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
CL kernel to perform fast corners.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
CLTensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: CLTensor.cpp:61
void init(const TensorInfo &input, size_t alignment=0)
Initialize a tensor based on the passed TensorInfo.
Copyright (c) 2017-2021 Arm Limited.
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
Interface for OpenCL Array.
Definition: ICLArray.h:35
void run() override final
Run the kernels contained in the function.
void configure(ICLTensor *input, ICLTensor *output, BorderMode border_mode)
Initialise the function&#39;s source, destinations and border mode.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:278
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:41
CL kernel to copy keypoints information to ICLKeyPointArray and counts the number of key points...
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
CLCompileContext class.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
void configure(const ICLImage *input, float threshold, bool nonmax_suppression, ICLKeyPointArray *corners, unsigned int *num_corners, BorderMode border_mode, uint8_t constant_border_value=0)
Initialize the function&#39;s source, destination, conv and border_mode.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
~CLFastCorners()
Default destructor.
Borders are left undefined.
Wrapper to configure the Khronos OpenCL C++ header.
size_t max_num_values() const
Maximum number of values which can be stored in this array.
Definition: IArray.h:58
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35