Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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/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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #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" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 36 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 40 | CLOpticalFlow::CLOpticalFlow(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 41 | : _memory_group(std::move(memory_manager)), |
| 42 | _tracker_init_kernel(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 43 | _tracker_stage0_kernel(), |
| 44 | _tracker_stage1_kernel(), |
| 45 | _tracker_finalize_kernel(), |
| 46 | _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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
| 60 | void CLOpticalFlow::configure(const CLPyramid *old_pyramid, const CLPyramid *new_pyramid, |
| 61 | const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, ICLKeyPointArray *new_points, |
| 62 | Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, bool use_initial_estimate, |
| 63 | BorderMode border_mode, uint8_t constant_border_value) |
| 64 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 65 | configure(CLKernelLibrary::get().get_compile_context(), old_pyramid, new_pyramid, old_points, new_points_estimates, new_points, termination, epsilon, num_iterations, window_dimension, |
| 66 | use_initial_estimate, border_mode, constant_border_value); |
| 67 | } |
| 68 | |
| 69 | void CLOpticalFlow::configure(const CLCompileContext &compile_context, const CLPyramid *old_pyramid, const CLPyramid *new_pyramid, |
| 70 | const ICLKeyPointArray *old_points, const ICLKeyPointArray *new_points_estimates, ICLKeyPointArray *new_points, |
| 71 | Termination termination, float epsilon, size_t num_iterations, size_t window_dimension, bool use_initial_estimate, |
| 72 | BorderMode border_mode, uint8_t constant_border_value) |
| 73 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON(nullptr == old_pyramid); |
| 75 | ARM_COMPUTE_ERROR_ON(nullptr == new_pyramid); |
| 76 | ARM_COMPUTE_ERROR_ON(nullptr == old_points); |
| 77 | ARM_COMPUTE_ERROR_ON(nullptr == new_points_estimates); |
| 78 | ARM_COMPUTE_ERROR_ON(nullptr == new_points); |
| 79 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->num_levels() != new_pyramid->info()->num_levels()); |
| 80 | ARM_COMPUTE_ERROR_ON(0 == old_pyramid->info()->num_levels()); |
| 81 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->width() != new_pyramid->info()->width()); |
| 82 | ARM_COMPUTE_ERROR_ON(old_pyramid->info()->height() != new_pyramid->info()->height()); |
| 83 | ARM_COMPUTE_ERROR_ON(use_initial_estimate && old_points->num_values() != new_points_estimates->num_values()); |
| 84 | |
| 85 | // Set member variables |
| 86 | _old_points = old_points; |
| 87 | _new_points_estimates = new_points_estimates; |
| 88 | _new_points = new_points; |
| 89 | _num_levels = old_pyramid->info()->num_levels(); |
| 90 | |
| 91 | const float pyr_scale = old_pyramid->info()->scale(); |
| 92 | const int list_length = old_points->num_values(); |
| 93 | const int old_values_list_length = list_length * window_dimension * window_dimension; |
| 94 | |
| 95 | // Create kernels and tensors |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 96 | _tracker_init_kernel.resize(_num_levels); |
| 97 | _tracker_stage0_kernel.resize(_num_levels); |
| 98 | _tracker_stage1_kernel.resize(_num_levels); |
| 99 | _func_scharr.resize(_num_levels); |
| 100 | _scharr_gx.resize(_num_levels); |
| 101 | _scharr_gy.resize(_num_levels); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
| 103 | // Create internal keypoint arrays |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 104 | _old_points_internal = arm_compute::support::cpp14::make_unique<CLLKInternalKeypointArray>(list_length); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | _old_points_internal->resize(list_length); |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 106 | _new_points_internal = arm_compute::support::cpp14::make_unique<CLLKInternalKeypointArray>(list_length); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | _new_points_internal->resize(list_length); |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 108 | _coefficient_table = arm_compute::support::cpp14::make_unique<CLCoefficientTableArray>(list_length); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | _coefficient_table->resize(list_length); |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 110 | _old_values = arm_compute::support::cpp14::make_unique<CLOldValueArray>(old_values_list_length); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | _old_values->resize(old_values_list_length); |
| 112 | _new_points->resize(list_length); |
| 113 | |
| 114 | for(size_t i = 0; i < _num_levels; ++i) |
| 115 | { |
| 116 | // Get images from the ith level of old and right pyramid |
| 117 | ICLImage *old_ith_input = old_pyramid->get_pyramid_level(i); |
| 118 | ICLImage *new_ith_input = new_pyramid->get_pyramid_level(i); |
| 119 | |
| 120 | // Get width and height of images |
| 121 | const unsigned int width_ith = old_ith_input->info()->dimension(0); |
| 122 | const unsigned int height_ith = new_ith_input->info()->dimension(1); |
| 123 | |
| 124 | // Initialize Scharr tensors |
| 125 | TensorInfo tensor_info(TensorShape(width_ith, height_ith), 1, DataType::S16); |
| 126 | _scharr_gx[i].allocator()->init(tensor_info); |
| 127 | _scharr_gy[i].allocator()->init(tensor_info); |
| 128 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 129 | // Manage intermediate buffers |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 130 | _memory_group.manage(&_scharr_gx[i]); |
| 131 | _memory_group.manage(&_scharr_gy[i]); |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 132 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | // Init Scharr kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 134 | _func_scharr[i].configure(compile_context, old_ith_input, &_scharr_gx[i], &_scharr_gy[i], border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | |
| 136 | // Init Lucas-Kanade init kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 137 | _tracker_init_kernel[i].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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
| 139 | // Init Lucas-Kanade stage0 kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 140 | _tracker_stage0_kernel[i].configure(compile_context, old_ith_input, &_scharr_gx[i], &_scharr_gy[i], |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | _old_points_internal.get(), _new_points_internal.get(), _coefficient_table.get(), _old_values.get(), |
| 142 | window_dimension, i); |
| 143 | |
| 144 | // Init Lucas-Kanade stage1 kernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 145 | _tracker_stage1_kernel[i].configure(compile_context, new_ith_input, _new_points_internal.get(), _coefficient_table.get(), _old_values.get(), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | termination, epsilon, num_iterations, window_dimension, i); |
| 147 | |
| 148 | // Allocate intermediate buffers |
| 149 | _scharr_gx[i].allocator()->allocate(); |
| 150 | _scharr_gy[i].allocator()->allocate(); |
| 151 | } |
| 152 | |
| 153 | // Finalize Lucas-Kanade |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 154 | _tracker_finalize_kernel.configure(compile_context, _new_points_internal.get(), new_points); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void CLOpticalFlow::run() |
| 158 | { |
| 159 | ARM_COMPUTE_ERROR_ON_MSG(_num_levels == 0, "Unconfigured function"); |
| 160 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 161 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 162 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | for(unsigned int level = _num_levels; level > 0; --level) |
| 164 | { |
| 165 | // Run Scharr kernel |
| 166 | _func_scharr[level - 1].run(); |
| 167 | |
| 168 | // Run Lucas-Kanade init kernel |
| 169 | CLScheduler::get().enqueue(_tracker_init_kernel[level - 1]); |
| 170 | |
| 171 | // Run Lucas-Kanade stage0 kernel |
| 172 | CLScheduler::get().enqueue(_tracker_stage0_kernel[level - 1]); |
| 173 | |
| 174 | // Run Lucas-Kanade stage1 kernel |
| 175 | CLScheduler::get().enqueue(_tracker_stage1_kernel[level - 1]); |
| 176 | } |
| 177 | |
| 178 | CLScheduler::get().enqueue(_tracker_finalize_kernel, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | } |