Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [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/CLGaussianPyramid.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLGaussianPyramidKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLScaleKernel.h" |
| 29 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/PixelValue.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include "arm_compute/runtime/CL/CLPyramid.h" |
| 36 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 37 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 38 | #include "arm_compute/runtime/CL/CLTensorAllocator.h" |
| 39 | #include "arm_compute/runtime/CL/functions/CLGaussian5x5.h" |
| 40 | |
| 41 | #include <cstddef> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | |
| 45 | CLGaussianPyramid::CLGaussianPyramid() |
| 46 | : _input(nullptr), _pyramid(nullptr), _tmp() |
| 47 | { |
| 48 | } |
| 49 | |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 50 | CLGaussianPyramidHalf::CLGaussianPyramidHalf() // NOLINT |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 51 | : _horizontal_border_handler(), |
| 52 | _vertical_border_handler(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 53 | _horizontal_reduction(), |
| 54 | _vertical_reduction() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
| 58 | void CLGaussianPyramidHalf::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 59 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 60 | configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value); |
| 61 | } |
| 62 | |
| 63 | void CLGaussianPyramidHalf::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 64 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 66 | ARM_COMPUTE_ERROR_ON(pyramid == nullptr); |
| 67 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 68 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width()); |
| 69 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height()); |
| 70 | ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale()); |
| 71 | |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 72 | // Constant value to use for vertical fill border when the border mode is CONSTANT |
| 73 | const uint16_t pixel_value_u16 = static_cast<uint16_t>(constant_border_value) * 2 + static_cast<uint16_t>(constant_border_value) * 8 + static_cast<uint16_t>(constant_border_value) * 6; |
| 74 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | /* Get number of pyramid levels */ |
| 76 | const size_t num_levels = pyramid->info()->num_levels(); |
| 77 | |
| 78 | _input = input; |
| 79 | _pyramid = pyramid; |
| 80 | |
| 81 | if(num_levels > 1) |
| 82 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 83 | _horizontal_border_handler.resize(num_levels - 1); |
| 84 | _vertical_border_handler.resize(num_levels - 1); |
| 85 | _horizontal_reduction.resize(num_levels - 1); |
| 86 | _vertical_reduction.resize(num_levels - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | |
| 88 | // Apply half scale to the X dimension of the tensor shape |
| 89 | TensorShape tensor_shape = pyramid->info()->tensor_shape(); |
| 90 | tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF); |
| 91 | |
| 92 | PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::U16); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | _tmp.init(pyramid_info); |
| 94 | |
| 95 | for(size_t i = 0; i < num_levels - 1; ++i) |
| 96 | { |
| 97 | /* Configure horizontal kernel */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 98 | _horizontal_reduction[i].configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
| 100 | /* Configure vertical kernel */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 101 | _vertical_reduction[i].configure(compile_context, _tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | |
| 103 | /* Configure border */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 104 | _horizontal_border_handler[i].configure(compile_context, _pyramid->get_pyramid_level(i), _horizontal_reduction[i].border_size(), border_mode, PixelValue(constant_border_value)); |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 105 | |
| 106 | /* Configure border */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 107 | _vertical_border_handler[i].configure(compile_context, _tmp.get_pyramid_level(i), _vertical_reduction[i].border_size(), border_mode, PixelValue(pixel_value_u16)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | } |
| 109 | _tmp.allocate(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void CLGaussianPyramidHalf::run() |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function"); |
| 116 | |
| 117 | /* Get number of pyramid levels */ |
| 118 | const size_t num_levels = _pyramid->info()->num_levels(); |
| 119 | |
| 120 | /* The first level of the pyramid has the input image */ |
| 121 | _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */); |
| 122 | _input->map(CLScheduler::get().queue(), true /* blocking */); |
| 123 | _pyramid->get_pyramid_level(0)->copy_from(*_input); |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 124 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | _input->unmap(CLScheduler::get().queue()); |
| 126 | _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue()); |
| 127 | |
| 128 | for(unsigned int i = 0; i < num_levels - 1; ++i) |
| 129 | { |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 130 | CLScheduler::get().enqueue(_horizontal_border_handler[i], false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | CLScheduler::get().enqueue(_horizontal_reduction[i], false); |
Sanghoon Lee | 1cd4149 | 2018-03-15 11:48:48 +0000 | [diff] [blame] | 132 | CLScheduler::get().enqueue(_vertical_border_handler[i], false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | CLScheduler::get().enqueue(_vertical_reduction[i], false); |
| 134 | } |
| 135 | } |
| 136 | |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 137 | CLGaussianPyramidOrb::CLGaussianPyramidOrb() // NOLINT |
| 138 | : _gauss5x5(), |
| 139 | _scale_nearest() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
| 143 | void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 144 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 145 | configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value); |
| 146 | } |
| 147 | |
| 148 | void CLGaussianPyramidOrb::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 149 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 151 | ARM_COMPUTE_ERROR_ON(nullptr == pyramid); |
| 152 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 153 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width()); |
| 154 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height()); |
| 155 | ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale()); |
| 156 | |
| 157 | /* Get number of pyramid levels */ |
| 158 | const size_t num_levels = pyramid->info()->num_levels(); |
| 159 | |
| 160 | _input = input; |
| 161 | _pyramid = pyramid; |
| 162 | |
| 163 | if(num_levels > 1) |
| 164 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 165 | _gauss5x5.resize(num_levels - 1); |
| 166 | _scale_nearest.resize(num_levels - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | |
| 168 | PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8); |
| 169 | |
| 170 | _tmp.init(pyramid_info); |
| 171 | |
| 172 | for(size_t i = 0; i < num_levels - 1; ++i) |
| 173 | { |
| 174 | /* Configure gaussian 5x5 */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 175 | _gauss5x5[i].configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
| 177 | /* Configure scale image kernel */ |
Sang-Hoon Park | ccd9496 | 2020-06-09 12:09:24 +0100 | [diff] [blame^] | 178 | _scale_nearest[i].configure(compile_context, _tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, PixelValue(), SamplingPolicy::CENTER }); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | _tmp.allocate(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void CLGaussianPyramidOrb::run() |
| 186 | { |
| 187 | ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function"); |
| 188 | |
| 189 | /* Get number of pyramid levels */ |
| 190 | const size_t num_levels = _pyramid->info()->num_levels(); |
| 191 | |
| 192 | /* The first level of the pyramid has the input image */ |
| 193 | _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */); |
| 194 | _input->map(CLScheduler::get().queue(), true /* blocking */); |
| 195 | _pyramid->get_pyramid_level(0)->copy_from(*_input); |
| 196 | _input->unmap(CLScheduler::get().queue()); |
| 197 | _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue()); |
| 198 | |
| 199 | for(unsigned int i = 0; i < num_levels - 1; ++i) |
| 200 | { |
| 201 | _gauss5x5[i].run(); |
| 202 | CLScheduler::get().enqueue(_scale_nearest[i]); |
| 203 | } |
| 204 | } |