blob: 76e0ac5f0bf4702a0145d1934a7e95ec0471508e [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 */
24#include "arm_compute/runtime/CL/functions/CLOpticalFlow.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Window.h"
30#include "arm_compute/runtime/CL/CLPyramid.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "arm_compute/runtime/CL/CLTensor.h"
33#include "arm_compute/runtime/CL/CLTensorAllocator.h"
34#include "arm_compute/runtime/CL/functions/CLScharr3x3.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035#include "src/core/CL/kernels/CLFillBorderKernel.h"
36#include "src/core/CL/kernels/CLLKTrackerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
39
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010040CLOpticalFlow::CLOpticalFlow(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
41 : _memory_group(std::move(memory_manager)),
42 _tracker_init_kernel(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010043 _tracker_stage0_kernel(),
44 _tracker_stage1_kernel(),
Georgios Pinitas40f51a62020-11-21 03:04:18 +000045 _tracker_finalize_kernel(std::make_unique<CLLKTrackerFinalizeKernel>()),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010046 _func_scharr(),
47 _scharr_gx(),
48 _scharr_gy(),
49 _old_points(nullptr),
50 _new_points_estimates(nullptr),
51 _new_points(nullptr),
52 _old_points_internal(),
53 _new_points_internal(),
54 _coefficient_table(),
55 _old_values(),
56 _num_levels(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
58}
59
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010060CLOpticalFlow::~CLOpticalFlow() = default;
61
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062void CLOpticalFlow::configure(const CLPyramid *old_pyramid, const CLPyramid *new_pyramid,
63 const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, ICLKeyPointArray *new_points,
64 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, bool use_initial_estimate,
65 BorderMode border_mode, uint8_t constant_border_value)
66{
Manuel Bottini2b84be52020-04-08 10:15:51 +010067 configure(CLKernelLibrary::get().get_compile_context(), old_pyramid, new_pyramid, old_points, new_points_estimates, new_points, termination, epsilon, num_iterations, window_dimension,
68 use_initial_estimate, border_mode, constant_border_value);
69}
70
71void CLOpticalFlow::configure(const CLCompileContext &compile_context, const CLPyramid *old_pyramid, const CLPyramid *new_pyramid,
72 const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, ICLKeyPointArray *new_points,
73 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, bool use_initial_estimate,
74 BorderMode border_mode, uint8_t constant_border_value)
75{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid);
77 ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid);
78 ARM_COMPUTE_ERROR_ON(nullptr == old_points);
79 ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates);
80 ARM_COMPUTE_ERROR_ON(nullptr == new_points);
81 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels());
82 ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels());
83 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width());
84 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height());
85 ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values());
86
87 // Set member variables
88 _old_points = old_points;
89 _new_points_estimates = new_points_estimates;
90 _new_points = new_points;
91 _num_levels = old_pyramid->info()->num_levels();
92
93 const float pyr_scale = old_pyramid->info()->scale();
94 const int list_length = old_points->num_values();
95 const int old_values_list_length = list_length * window_dimension * window_dimension;
96
97 // Create kernels and tensors
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010098 _tracker_init_kernel.reserve(_num_levels);
99 _tracker_stage0_kernel.reserve(_num_levels);
100 _tracker_stage1_kernel.reserve(_num_levels);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100101 _func_scharr.resize(_num_levels);
102 _scharr_gx.resize(_num_levels);
103 _scharr_gy.resize(_num_levels);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
105 // Create internal keypoint arrays
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000106 _old_points_internal = std::make_unique<CLLKInternalKeypointArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 _old_points_internal->resize(list_length);
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000108 _new_points_internal = std::make_unique<CLLKInternalKeypointArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 _new_points_internal->resize(list_length);
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000110 _coefficient_table = std::make_unique<CLCoefficientTableArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 _coefficient_table->resize(list_length);
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 _old_values = std::make_unique<CLOldValueArray>(old_values_list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 _old_values->resize(old_values_list_length);
114 _new_points->resize(list_length);
115
116 for(size_t i = 0; i < _num_levels; ++i)
117 {
118 // Get images from the ith level of old and right pyramid
119 ICLImage *old_ith_input = old_pyramid->get_pyramid_level(i);
120 ICLImage *new_ith_input = new_pyramid->get_pyramid_level(i);
121
122 // Get width and height of images
123 const unsigned int width_ith = old_ith_input->info()->dimension(0);
124 const unsigned int height_ith = new_ith_input->info()->dimension(1);
125
126 // Initialize Scharr tensors
127 TensorInfo tensor_info(TensorShape(width_ith, height_ith), 1, DataType::S16);
128 _scharr_gx[i].allocator()->init(tensor_info);
129 _scharr_gy[i].allocator()->init(tensor_info);
130
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100131 // Manage intermediate buffers
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100132 _memory_group.manage(&_scharr_gx[i]);
133 _memory_group.manage(&_scharr_gy[i]);
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 // Init Scharr kernel
Manuel Bottini2b84be52020-04-08 10:15:51 +0100136 _func_scharr[i].configure(compile_context, old_ith_input, &_scharr_gx[i], &_scharr_gy[i], border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
138 // Init Lucas-Kanade init kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000139 _tracker_init_kernel.emplace_back(std::make_unique<CLLKTrackerInitKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100140 _tracker_init_kernel.back()->configure(compile_context, old_points, new_points_estimates, _old_points_internal.get(), _new_points_internal.get(), use_initial_estimate, i, _num_levels, pyr_scale);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141
142 // Init Lucas-Kanade stage0 kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000143 _tracker_stage0_kernel.emplace_back(std::make_unique<CLLKTrackerStage0Kernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100144 _tracker_stage0_kernel.back()->configure(compile_context, old_ith_input, &_scharr_gx[i], &_scharr_gy[i],
145 _old_points_internal.get(), _new_points_internal.get(), _coefficient_table.get(), _old_values.get(),
146 window_dimension, i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
148 // Init Lucas-Kanade stage1 kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000149 _tracker_stage1_kernel.emplace_back(std::make_unique<CLLKTrackerStage1Kernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100150 _tracker_stage1_kernel.back()->configure(compile_context, new_ith_input, _new_points_internal.get(), _coefficient_table.get(), _old_values.get(),
151 termination, epsilon, num_iterations, window_dimension, i);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
153 // Allocate intermediate buffers
154 _scharr_gx[i].allocator()->allocate();
155 _scharr_gy[i].allocator()->allocate();
156 }
157
158 // Finalize Lucas-Kanade
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100159 _tracker_finalize_kernel->configure(compile_context, _new_points_internal.get(), new_points);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160}
161
162void CLOpticalFlow::run()
163{
164 ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function");
165
Georgios Pinitasda953f22019-04-02 17:27:03 +0100166 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100167
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 for(unsigned int level = _num_levels; level > 0; --level)
169 {
170 // Run Scharr kernel
171 _func_scharr[level - 1].run();
172
173 // Run Lucas-Kanade init kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100174 CLScheduler::get().enqueue(*_tracker_init_kernel[level - 1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
176 // Run Lucas-Kanade stage0 kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100177 CLScheduler::get().enqueue(*_tracker_stage0_kernel[level - 1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178
179 // Run Lucas-Kanade stage1 kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100180 CLScheduler::get().enqueue(*_tracker_stage1_kernel[level - 1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 }
182
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100183 CLScheduler::get().enqueue(*_tracker_finalize_kernel, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184}