blob: a439c2448e021fd0ca96d91d710026dcd04a57bf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLLKTrackerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLArray.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Coordinates.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/AccessWindowStatic.h"
34#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include <cmath>
37
38using namespace arm_compute;
39
40void CLLKTrackerInitKernel::configure(const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates,
41 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
42 bool use_initial_estimate, size_t level, size_t num_levels, float pyramid_scale)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010043{
44 configure(CLKernelLibrary::get().get_compile_context(), old_points, new_points_estimates, old_points_internal, new_points_internal, use_initial_estimate, level, num_levels, pyramid_scale);
45}
46
Manuel Bottini679fc962020-04-21 16:08:53 +010047void CLLKTrackerInitKernel::configure(const CLCompileContext &compile_context, const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010048 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
49 bool use_initial_estimate, size_t level, size_t num_levels, float pyramid_scale)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
51{
52 ARM_COMPUTE_ERROR_ON(old_points == nullptr);
53 ARM_COMPUTE_ERROR_ON(old_points_internal == nullptr);
54 ARM_COMPUTE_ERROR_ON(new_points_internal == nullptr);
55
56 const float scale = std::pow(pyramid_scale, level);
57
58 // Create kernel
59 std::string kernel_name = "init_level";
60 if(level == (num_levels - 1))
61 {
62 kernel_name += (use_initial_estimate) ? std::string("_max_initial_estimate") : std::string("_max");
63 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +010064 _kernel = create_kernel(compile_context, kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
66 // Set static kernel arguments
67 unsigned int idx = 0;
68 if(level == (num_levels - 1))
69 {
70 _kernel.setArg(idx++, old_points->cl_buffer());
71 if(use_initial_estimate)
72 {
73 _kernel.setArg(idx++, new_points_estimates->cl_buffer());
74 }
75 }
76 _kernel.setArg(idx++, old_points_internal->cl_buffer());
77 _kernel.setArg(idx++, new_points_internal->cl_buffer());
78 _kernel.setArg<cl_float>(idx++, scale);
79
80 // Configure kernel window
81 Window window;
82 window.set(Window::DimX, Window::Dimension(0, old_points->num_values(), 1));
83 window.set(Window::DimY, Window::Dimension(0, 1, 1));
Anthony Barbierb6eb3532018-08-08 13:20:04 +010084 ICLKernel::configure_internal(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085}
86
87void CLLKTrackerInitKernel::run(const Window &window, cl::CommandQueue &queue)
88{
89 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
90 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
91
Georgios Pinitas275f99c2019-08-23 12:44:11 +010092 enqueue(queue, *this, window, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
94
95void CLLKTrackerFinalizeKernel::configure(ICLLKInternalKeypointArray *new_points_internal, ICLKeyPointArray *new_points)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010096{
97 configure(CLKernelLibrary::get().get_compile_context(), new_points_internal, new_points);
98}
99
Manuel Bottini679fc962020-04-21 16:08:53 +0100100void CLLKTrackerFinalizeKernel::configure(const CLCompileContext &compile_context, ICLLKInternalKeypointArray *new_points_internal, ICLKeyPointArray *new_points)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102{
103 ARM_COMPUTE_ERROR_ON(new_points_internal == nullptr);
104 ARM_COMPUTE_ERROR_ON(new_points == nullptr);
105
106 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100107 _kernel = create_kernel(compile_context, "finalize");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
109 // Set static kernel arguments
110 unsigned int idx = 0;
111 _kernel.setArg(idx++, new_points_internal->cl_buffer());
112 _kernel.setArg(idx++, new_points->cl_buffer());
113
114 // Configure kernel window
115 Window window;
116 window.set(Window::DimX, Window::Dimension(0, new_points_internal->num_values(), 1));
117 window.set(Window::DimY, Window::Dimension(0, 1, 1));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100118 ICLKernel::configure_internal(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119}
120
121void CLLKTrackerFinalizeKernel::run(const Window &window, cl::CommandQueue &queue)
122{
123 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
125
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100126 enqueue(queue, *this, window, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127}
128
129CLLKTrackerStage0Kernel::CLLKTrackerStage0Kernel()
130 : _old_input(nullptr), _old_scharr_gx(nullptr), _old_scharr_gy(nullptr)
131{
132}
133
134void CLLKTrackerStage0Kernel::configure(const ICLTensor *old_input, const ICLTensor *old_scharr_gx, const ICLTensor *old_scharr_gy,
135 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
136 ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
137 size_t window_dimension, size_t level)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100138{
139 configure(CLKernelLibrary::get().get_compile_context(), old_input, old_scharr_gx, old_scharr_gy, old_points_internal, new_points_internal, coeff_table, old_ival, window_dimension, level);
140}
141
Manuel Bottini679fc962020-04-21 16:08:53 +0100142void CLLKTrackerStage0Kernel::configure(const CLCompileContext &compile_context, const ICLTensor *old_input, const ICLTensor *old_scharr_gx, const ICLTensor *old_scharr_gy,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100143 ICLLKInternalKeypointArray *old_points_internal, ICLLKInternalKeypointArray *new_points_internal,
144 ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
145 size_t window_dimension, size_t level)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147{
148 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(old_input, 1, DataType::U8);
149 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(old_scharr_gx, 1, DataType::S16);
150 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(old_scharr_gy, 1, DataType::S16);
151 ARM_COMPUTE_ERROR_ON(old_points_internal == nullptr);
152 ARM_COMPUTE_ERROR_ON(new_points_internal == nullptr);
153 ARM_COMPUTE_ERROR_ON(coeff_table == nullptr);
154 ARM_COMPUTE_ERROR_ON(old_ival == nullptr);
155
156 _old_input = old_input;
157 _old_scharr_gx = old_scharr_gx;
158 _old_scharr_gy = old_scharr_gy;
159
160 // Configure kernel window
161 Window window;
162 window.set(Window::DimX, Window::Dimension(0, new_points_internal->num_values(), 1));
163 window.set(Window::DimY, Window::Dimension(0, 1, 1));
164
165 const ValidRegion valid_region = intersect_valid_regions(
166 old_input->info()->valid_region(),
167 old_scharr_gx->info()->valid_region(),
168 old_scharr_gy->info()->valid_region());
169
170 update_window_and_padding(window,
171 AccessWindowStatic(old_input->info(), valid_region.start(0), valid_region.start(1),
172 valid_region.end(0), valid_region.end(1)),
173 AccessWindowStatic(old_scharr_gx->info(), valid_region.start(0), valid_region.start(1),
174 valid_region.end(0), valid_region.end(1)),
175 AccessWindowStatic(old_scharr_gy->info(), valid_region.start(0), valid_region.start(1),
176 valid_region.end(0), valid_region.end(1)));
177
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100178 ICLKernel::configure_internal(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180 // Initialize required variables
181 const int level0 = (level == 0) ? 1 : 0;
182 const int window_size = window_dimension;
183 const int window_size_squared = window_dimension * window_dimension;
184 const int window_size_half = window_dimension / 2;
185 const float eig_const = 1.0f / (2.0f * window_size_squared);
186 const cl_float3 border_limits =
187 {
188 {
189 // -1 because we load 2 values at once for bilinear interpolation
190 static_cast<cl_float>(valid_region.end(0) - window_size - 1),
191 static_cast<cl_float>(valid_region.end(1) - window_size - 1),
192 static_cast<cl_float>(valid_region.start(0))
193 }
194 };
195
196 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100197 _kernel = create_kernel(compile_context, "lktracker_stage0");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198
199 // Set arguments
200 unsigned int idx = 3 * num_arguments_per_2D_tensor();
201 _kernel.setArg(idx++, old_points_internal->cl_buffer());
202 _kernel.setArg(idx++, new_points_internal->cl_buffer());
203 _kernel.setArg(idx++, coeff_table->cl_buffer());
204 _kernel.setArg(idx++, old_ival->cl_buffer());
205 _kernel.setArg<cl_int>(idx++, window_size);
206 _kernel.setArg<cl_int>(idx++, window_size_squared);
207 _kernel.setArg<cl_int>(idx++, window_size_half);
208 _kernel.setArg<cl_float3>(idx++, border_limits);
209 _kernel.setArg<cl_float>(idx++, eig_const);
210 _kernel.setArg<cl_int>(idx++, level0);
211}
212
213void CLLKTrackerStage0Kernel::run(const Window &window, cl::CommandQueue &queue)
214{
215 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
216 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
217
218 // Set static tensor arguments. Setting here as allocation might be deferred.
219 unsigned int idx = 0;
220 add_2D_tensor_argument(idx, _old_input, window);
221 add_2D_tensor_argument(idx, _old_scharr_gx, window);
222 add_2D_tensor_argument(idx, _old_scharr_gy, window);
223
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100224 enqueue(queue, *this, window, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225}
226
227CLLKTrackerStage1Kernel::CLLKTrackerStage1Kernel()
228 : _new_input(nullptr)
229{
230}
231
232void CLLKTrackerStage1Kernel::configure(const ICLTensor *new_input, ICLLKInternalKeypointArray *new_points_internal, ICLCoefficientTableArray *coeff_table, ICLOldValArray *old_ival,
233 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, size_t level)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100234{
235 configure(CLKernelLibrary::get().get_compile_context(), new_input, new_points_internal, coeff_table, old_ival, termination, epsilon, num_iterations, window_dimension, level);
236}
237
Manuel Bottini679fc962020-04-21 16:08:53 +0100238void CLLKTrackerStage1Kernel::configure(const CLCompileContext &compile_context, const ICLTensor *new_input, ICLLKInternalKeypointArray *new_points_internal, ICLCoefficientTableArray *coeff_table,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100239 ICLOldValArray *old_ival,
240 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, size_t level)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
242{
243 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(new_input, 1, DataType::U8);
244 ARM_COMPUTE_ERROR_ON(new_points_internal == nullptr);
245 ARM_COMPUTE_ERROR_ON(coeff_table == nullptr);
246 ARM_COMPUTE_ERROR_ON(old_ival == nullptr);
247
248 _new_input = new_input;
249
250 // Configure kernel window
251 Window window;
252 window.set(Window::DimX, Window::Dimension(0, new_points_internal->num_values(), 1));
253 window.set(Window::DimY, Window::Dimension(0, 1, 1));
254
255 const ValidRegion &valid_region = new_input->info()->valid_region();
256
257 update_window_and_padding(window,
258 AccessWindowStatic(new_input->info(), valid_region.start(0), valid_region.start(1),
259 valid_region.end(0), valid_region.end(1)));
260
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100261 ICLKernel::configure_internal(window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262
263 // Initialize required variables
264 const int level0 = (level == 0) ? 1 : 0;
265 const int window_size = window_dimension;
266 const int window_size_squared = window_dimension * window_dimension;
267 const int window_size_half = window_dimension / 2;
268 const float eig_const = 1.0f / (2.0f * window_size_squared);
269 const cl_float3 border_limits =
270 {
271 {
272 // -1 because we load 2 values at once for bilinear interpolation
273 static_cast<cl_float>(valid_region.end(0) - window_size - 1),
274 static_cast<cl_float>(valid_region.end(1) - window_size - 1),
275 static_cast<cl_float>(valid_region.start(0))
276 }
277 };
John Richardson8de92612018-02-22 14:09:31 +0000278
279 // Set maximum number of iterations used for convergence
280 const size_t max_iterations = 1000;
281 num_iterations = (termination == Termination::TERM_CRITERIA_EPSILON) ? max_iterations : num_iterations;
282
283 const int term_epsilon = (termination == Termination::TERM_CRITERIA_EPSILON || termination == Termination::TERM_CRITERIA_BOTH) ? 1 : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284
285 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100286 _kernel = create_kernel(compile_context, "lktracker_stage1");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287
288 // Set static kernel arguments
289 unsigned int idx = num_arguments_per_2D_tensor();
290 _kernel.setArg(idx++, new_points_internal->cl_buffer());
291 _kernel.setArg(idx++, coeff_table->cl_buffer());
292 _kernel.setArg(idx++, old_ival->cl_buffer());
293 _kernel.setArg<cl_int>(idx++, window_size);
294 _kernel.setArg<cl_int>(idx++, window_size_squared);
295 _kernel.setArg<cl_int>(idx++, window_size_half);
296 _kernel.setArg<cl_int>(idx++, num_iterations);
297 _kernel.setArg<cl_float>(idx++, epsilon);
298 _kernel.setArg<cl_float3>(idx++, border_limits);
299 _kernel.setArg<cl_float>(idx++, eig_const);
300 _kernel.setArg<cl_int>(idx++, level0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100301 _kernel.setArg<cl_int>(idx++, term_epsilon);
302}
303
304void CLLKTrackerStage1Kernel::run(const Window &window, cl::CommandQueue &queue)
305{
306 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
307 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
308
309 // Set static tensor arguments. Setting here as allocation might be deferred.
310 unsigned int idx = 0;
311 add_2D_tensor_argument(idx, _new_input, window);
312
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100313 enqueue(queue, *this, window, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100314}