blob: 542f5961fa3ad931aad226e03bdd38a4d51d5067 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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 */
24#include "arm_compute/runtime/CL/functions/CLOpticalFlow.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/CL/kernels/CLLKTrackerKernel.h"
28#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Window.h"
31#include "arm_compute/runtime/CL/CLPyramid.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "arm_compute/runtime/CL/CLTensor.h"
34#include "arm_compute/runtime/CL/CLTensorAllocator.h"
35#include "arm_compute/runtime/CL/functions/CLScharr3x3.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010036#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
39
40CLOpticalFlow::CLOpticalFlow()
41 : _tracker_init_kernel(), _tracker_stage0_kernel(), _tracker_stage1_kernel(), _tracker_finalize_kernel(), _func_scharr(), _scharr_gx(), _scharr_gy(), _old_points(nullptr),
42 _new_points_estimates(nullptr), _new_points(nullptr), _old_points_internal(), _new_points_internal(), _coefficient_table(), _old_values(), _num_levels(0)
43{
44}
45
46void CLOpticalFlow::configure(const CLPyramid *old_pyramid, const CLPyramid *new_pyramid,
47 const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, ICLKeyPointArray *new_points,
48 Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, bool use_initial_estimate,
49 BorderMode border_mode, uint8_t constant_border_value)
50{
51 ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid);
52 ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid);
53 ARM_COMPUTE_ERROR_ON(nullptr == old_points);
54 ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates);
55 ARM_COMPUTE_ERROR_ON(nullptr == new_points);
56 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels());
57 ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels());
58 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width());
59 ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height());
60 ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values());
61
62 // Set member variables
63 _old_points = old_points;
64 _new_points_estimates = new_points_estimates;
65 _new_points = new_points;
66 _num_levels = old_pyramid->info()->num_levels();
67
68 const float pyr_scale = old_pyramid->info()->scale();
69 const int list_length = old_points->num_values();
70 const int old_values_list_length = list_length * window_dimension * window_dimension;
71
72 // Create kernels and tensors
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010073 _tracker_init_kernel = arm_compute::support::cpp14::make_unique<CLLKTrackerInitKernel[]>(_num_levels);
74 _tracker_stage0_kernel = arm_compute::support::cpp14::make_unique<CLLKTrackerStage0Kernel[]>(_num_levels);
75 _tracker_stage1_kernel = arm_compute::support::cpp14::make_unique<CLLKTrackerStage1Kernel[]>(_num_levels);
76 _func_scharr = arm_compute::support::cpp14::make_unique<CLScharr3x3[]>(_num_levels);
77 _scharr_gx = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_levels);
78 _scharr_gy = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_levels);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079
80 // Create internal keypoint arrays
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010081 _old_points_internal = arm_compute::support::cpp14::make_unique<CLLKInternalKeypointArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 _old_points_internal->resize(list_length);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010083 _new_points_internal = arm_compute::support::cpp14::make_unique<CLLKInternalKeypointArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 _new_points_internal->resize(list_length);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010085 _coefficient_table = arm_compute::support::cpp14::make_unique<CLCoefficientTableArray>(list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 _coefficient_table->resize(list_length);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010087 _old_values = arm_compute::support::cpp14::make_unique<CLOldValueArray>(old_values_list_length);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 _old_values->resize(old_values_list_length);
89 _new_points->resize(list_length);
90
91 for(size_t i = 0; i < _num_levels; ++i)
92 {
93 // Get images from the ith level of old and right pyramid
94 ICLImage *old_ith_input = old_pyramid->get_pyramid_level(i);
95 ICLImage *new_ith_input = new_pyramid->get_pyramid_level(i);
96
97 // Get width and height of images
98 const unsigned int width_ith = old_ith_input->info()->dimension(0);
99 const unsigned int height_ith = new_ith_input->info()->dimension(1);
100
101 // Initialize Scharr tensors
102 TensorInfo tensor_info(TensorShape(width_ith, height_ith), 1, DataType::S16);
103 _scharr_gx[i].allocator()->init(tensor_info);
104 _scharr_gy[i].allocator()->init(tensor_info);
105
106 // Init Scharr kernel
107 _func_scharr[i].configure(old_ith_input, &_scharr_gx[i], &_scharr_gy[i], border_mode, constant_border_value);
108
109 // Init Lucas-Kanade init kernel
110 _tracker_init_kernel[i].configure(old_points, new_points_estimates, _old_points_internal.get(), _new_points_internal.get(), use_initial_estimate, i, _num_levels, pyr_scale);
111
112 // Init Lucas-Kanade stage0 kernel
113 _tracker_stage0_kernel[i].configure(old_ith_input, &_scharr_gx[i], &_scharr_gy[i],
114 _old_points_internal.get(), _new_points_internal.get(), _coefficient_table.get(), _old_values.get(),
115 window_dimension, i);
116
117 // Init Lucas-Kanade stage1 kernel
118 _tracker_stage1_kernel[i].configure(new_ith_input, _new_points_internal.get(), _coefficient_table.get(), _old_values.get(),
119 termination, epsilon, num_iterations, window_dimension, i);
120
121 // Allocate intermediate buffers
122 _scharr_gx[i].allocator()->allocate();
123 _scharr_gy[i].allocator()->allocate();
124 }
125
126 // Finalize Lucas-Kanade
127 _tracker_finalize_kernel.configure(_new_points_internal.get(), new_points);
128}
129
130void CLOpticalFlow::run()
131{
132 ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function");
133
134 for(unsigned int level = _num_levels; level > 0; --level)
135 {
136 // Run Scharr kernel
137 _func_scharr[level - 1].run();
138
139 // Run Lucas-Kanade init kernel
140 CLScheduler::get().enqueue(_tracker_init_kernel[level - 1]);
141
142 // Run Lucas-Kanade stage0 kernel
143 CLScheduler::get().enqueue(_tracker_stage0_kernel[level - 1]);
144
145 // Run Lucas-Kanade stage1 kernel
146 CLScheduler::get().enqueue(_tracker_stage1_kernel[level - 1]);
147 }
148
149 CLScheduler::get().enqueue(_tracker_finalize_kernel, true);
150}