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