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) 2016-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/NEON/functions/NEGaussianPyramid.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NEGaussianPyramidKernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEScaleKernel.h" |
| 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/runtime/NEON/NEScheduler.h" |
| 34 | #include "arm_compute/runtime/NEON/functions/NEGaussian5x5.h" |
| 35 | #include "arm_compute/runtime/Pyramid.h" |
| 36 | #include "arm_compute/runtime/Tensor.h" |
| 37 | #include "arm_compute/runtime/TensorAllocator.h" |
| 38 | |
| 39 | #include <cstddef> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | NEGaussianPyramid::NEGaussianPyramid() |
| 44 | : _input(nullptr), _pyramid(nullptr), _tmp() |
| 45 | { |
| 46 | } |
| 47 | |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 48 | NEGaussianPyramidHalf::NEGaussianPyramidHalf() // NOLINT |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 49 | : _horizontal_border_handler(), |
| 50 | _vertical_border_handler(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 51 | _horizontal_reduction(), |
| 52 | _vertical_reduction() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
| 56 | void NEGaussianPyramidHalf::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 59 | ARM_COMPUTE_ERROR_ON(nullptr == pyramid); |
| 60 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 61 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width()); |
| 62 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height()); |
| 63 | ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale()); |
| 64 | |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 65 | // Constant value to use for vertical fill border when the border mode is CONSTANT |
| 66 | 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; |
| 67 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | /* Get number of pyramid levels */ |
| 69 | const size_t num_levels = pyramid->info()->num_levels(); |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 70 | const size_t num_stages = num_levels - 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
| 72 | _input = input; |
| 73 | _pyramid = pyramid; |
| 74 | |
| 75 | if(num_levels > 1) |
| 76 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | // Apply half scale to the X dimension of the tensor shape |
| 78 | TensorShape tensor_shape = pyramid->info()->tensor_shape(); |
| 79 | tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF); |
| 80 | |
| 81 | PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::S16); |
| 82 | _tmp.init(pyramid_info); |
| 83 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 84 | _horizontal_reduction.clear(); |
| 85 | _vertical_reduction.clear(); |
| 86 | _horizontal_border_handler.clear(); |
| 87 | _vertical_border_handler.clear(); |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 88 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 89 | _horizontal_reduction.resize(num_stages); |
| 90 | _vertical_reduction.resize(num_stages); |
| 91 | _horizontal_border_handler.resize(num_stages); |
| 92 | _vertical_border_handler.resize(num_stages); |
| 93 | |
| 94 | for(size_t i = 0; i < num_stages; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | { |
| 96 | /* Configure horizontal kernel */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 97 | _horizontal_reduction[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | |
| 99 | /* Configure vertical kernel */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 100 | _vertical_reduction[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | |
| 102 | /* Configure border */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 103 | _horizontal_border_handler[i].configure(_pyramid->get_pyramid_level(i), _horizontal_reduction[i].border_size(), border_mode, PixelValue(constant_border_value)); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 104 | |
| 105 | /* Configure border */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 106 | _vertical_border_handler[i].configure(_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] | 107 | } |
| 108 | |
| 109 | _tmp.allocate(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void NEGaussianPyramidHalf::run() |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function"); |
| 116 | |
| 117 | /* Get number of pyramid levels */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 118 | const unsigned int num_levels = _pyramid->info()->num_levels(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
| 120 | /* The first level of the pyramid has the input image */ |
| 121 | _pyramid->get_pyramid_level(0)->copy_from(*_input); |
| 122 | |
| 123 | for(unsigned int i = 0; i < num_levels - 1; ++i) |
| 124 | { |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 125 | NEScheduler::get().schedule(&_horizontal_border_handler[i], Window::DimZ); |
| 126 | NEScheduler::get().schedule(&_horizontal_reduction[i], Window::DimY); |
| 127 | NEScheduler::get().schedule(&_vertical_border_handler[i], Window::DimZ); |
| 128 | NEScheduler::get().schedule(&_vertical_reduction[i], Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 132 | NEGaussianPyramidOrb::NEGaussianPyramidOrb() // NOLINT |
Georgios Pinitas | 3021edf | 2017-09-18 17:55:22 +0100 | [diff] [blame] | 133 | : _gaus5x5(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 134 | _scale_nearest() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | { |
| 136 | } |
| 137 | |
| 138 | void NEGaussianPyramidOrb::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value) |
| 139 | { |
| 140 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 141 | ARM_COMPUTE_ERROR_ON(nullptr == pyramid); |
| 142 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 143 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width()); |
| 144 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height()); |
| 145 | ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale()); |
| 146 | |
| 147 | /* Get number of pyramid levels */ |
| 148 | const size_t num_levels = pyramid->info()->num_levels(); |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 149 | const size_t num_stages = num_levels - 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | |
| 151 | _input = input; |
| 152 | _pyramid = pyramid; |
| 153 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 154 | _gaus5x5.clear(); |
| 155 | _scale_nearest.clear(); |
| 156 | |
| 157 | _gaus5x5.resize(num_stages); |
| 158 | _scale_nearest.resize(num_stages); |
| 159 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | if(num_levels > 1) |
| 161 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8); |
| 163 | _tmp.init(pyramid_info); |
| 164 | |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 165 | for(size_t i = 0; i < num_levels - 1; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | /* Configure gaussian 5x5 */ |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 168 | _gaus5x5[i].configure(_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] | 169 | |
Georgios Pinitas | 3021edf | 2017-09-18 17:55:22 +0100 | [diff] [blame] | 170 | /* Configure scale */ |
Sang-Hoon Park | ccd9496 | 2020-06-09 12:09:24 +0100 | [diff] [blame] | 171 | _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED }); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | _tmp.allocate(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void NEGaussianPyramidOrb::run() |
| 179 | { |
| 180 | ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function"); |
| 181 | |
| 182 | /* Get number of pyramid levels */ |
| 183 | const size_t num_levels = _pyramid->info()->num_levels(); |
| 184 | |
| 185 | /* The first level of the pyramid has the input image */ |
| 186 | _pyramid->get_pyramid_level(0)->copy_from(*_input); |
| 187 | |
| 188 | for(unsigned int i = 0; i < num_levels - 1; ++i) |
| 189 | { |
Georgios Pinitas | 725b173 | 2019-05-20 19:40:47 +0100 | [diff] [blame] | 190 | _gaus5x5[i].run(); |
| 191 | _scale_nearest[i].run(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | } |
| 193 | } |