blob: 30fe70f0ab61cb8e2960bb4dd7e6e7f06e77ee29 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/PixelValue.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/runtime/Pyramid.h"
33#include "arm_compute/runtime/Tensor.h"
34#include "arm_compute/runtime/TensorAllocator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010035#include "src/core/NEON/kernels/NEFillBorderKernel.h"
36#include "src/core/NEON/kernels/NEGaussian5x5Kernel.h"
37#include "src/core/NEON/kernels/NEGaussianPyramidKernel.h"
38#include "src/core/NEON/kernels/NEScaleKernel.h"
39#include "support/MemorySupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
41#include <cstddef>
42
43using namespace arm_compute;
44
45NEGaussianPyramid::NEGaussianPyramid()
46 : _input(nullptr), _pyramid(nullptr), _tmp()
47{
48}
49
Michalis Spyrouebcebf12020-10-21 00:04:14 +010050NEGaussianPyramidHalf::~NEGaussianPyramidHalf() = default;
51
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010052NEGaussianPyramidHalf::NEGaussianPyramidHalf() // NOLINT
Gian Marco37908d92017-11-07 14:38:22 +000053 : _horizontal_border_handler(),
54 _vertical_border_handler(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010055 _horizontal_reduction(),
56 _vertical_reduction()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
58}
59
60void NEGaussianPyramidHalf::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
61{
62 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
63 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
64 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
65 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
66 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
67 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale());
68
Gian Marco37908d92017-11-07 14:38:22 +000069 // Constant value to use for vertical fill border when the border mode is CONSTANT
70 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;
71
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 /* Get number of pyramid levels */
73 const size_t num_levels = pyramid->info()->num_levels();
Georgios Pinitas725b1732019-05-20 19:40:47 +010074 const size_t num_stages = num_levels - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
76 _input = input;
77 _pyramid = pyramid;
78
79 if(num_levels > 1)
80 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 // Apply half scale to the X dimension of the tensor shape
82 TensorShape tensor_shape = pyramid->info()->tensor_shape();
83 tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
84
85 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::S16);
86 _tmp.init(pyramid_info);
87
Georgios Pinitas725b1732019-05-20 19:40:47 +010088 _horizontal_reduction.clear();
89 _vertical_reduction.clear();
90 _horizontal_border_handler.clear();
91 _vertical_border_handler.clear();
Michalis Spyrou299fdd32019-05-01 13:03:59 +010092
Georgios Pinitas725b1732019-05-20 19:40:47 +010093 _horizontal_reduction.resize(num_stages);
94 _vertical_reduction.resize(num_stages);
95 _horizontal_border_handler.resize(num_stages);
96 _vertical_border_handler.resize(num_stages);
97
98 for(size_t i = 0; i < num_stages; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 {
100 /* Configure horizontal kernel */
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100101 _horizontal_reduction[i] = arm_compute::support::cpp14::make_unique<NEGaussianPyramidHorKernel>();
102 _horizontal_reduction[i]->configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104 /* Configure vertical kernel */
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100105 _vertical_reduction[i] = arm_compute::support::cpp14::make_unique<NEGaussianPyramidVertKernel>();
106 _vertical_reduction[i]->configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
108 /* Configure border */
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100109 _horizontal_border_handler[i] = arm_compute::support::cpp14::make_unique<NEFillBorderKernel>();
110 _horizontal_border_handler[i]->configure(_pyramid->get_pyramid_level(i), _horizontal_reduction[i]->border_size(), border_mode, PixelValue(constant_border_value));
Gian Marco37908d92017-11-07 14:38:22 +0000111
112 /* Configure border */
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100113 _vertical_border_handler[i] = arm_compute::support::cpp14::make_unique<NEFillBorderKernel>();
114 _vertical_border_handler[i]->configure(_tmp.get_pyramid_level(i), _vertical_reduction[i]->border_size(), border_mode, PixelValue(pixel_value_u16));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 }
116
117 _tmp.allocate();
118 }
119}
120
121void NEGaussianPyramidHalf::run()
122{
123 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
124
125 /* Get number of pyramid levels */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100126 const unsigned int num_levels = _pyramid->info()->num_levels();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
128 /* The first level of the pyramid has the input image */
129 _pyramid->get_pyramid_level(0)->copy_from(*_input);
130
131 for(unsigned int i = 0; i < num_levels - 1; ++i)
132 {
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100133 NEScheduler::get().schedule(_horizontal_border_handler[i].get(), Window::DimZ);
134 NEScheduler::get().schedule(_horizontal_reduction[i].get(), Window::DimY);
135 NEScheduler::get().schedule(_vertical_border_handler[i].get(), Window::DimZ);
136 NEScheduler::get().schedule(_vertical_reduction[i].get(), Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 }
138}
139
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100140NEGaussianPyramidOrb::~NEGaussianPyramidOrb() = default;
141
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100142NEGaussianPyramidOrb::NEGaussianPyramidOrb() // NOLINT
Georgios Pinitas3021edf2017-09-18 17:55:22 +0100143 : _gaus5x5(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100144 _scale_nearest()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145{
146}
147
148void NEGaussianPyramidOrb::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
149{
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();
Georgios Pinitas725b1732019-05-20 19:40:47 +0100159 const size_t num_stages = num_levels - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 _input = input;
162 _pyramid = pyramid;
163
Georgios Pinitas725b1732019-05-20 19:40:47 +0100164 _gaus5x5.clear();
165 _scale_nearest.clear();
166
167 _gaus5x5.resize(num_stages);
168 _scale_nearest.resize(num_stages);
169
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 if(num_levels > 1)
171 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
173 _tmp.init(pyramid_info);
174
Georgios Pinitas725b1732019-05-20 19:40:47 +0100175 for(size_t i = 0; i < num_levels - 1; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 /* Configure gaussian 5x5 */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100178 _gaus5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
Georgios Pinitas3021edf2017-09-18 17:55:22 +0100180 /* Configure scale */
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100181 _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED, PixelValue(), SamplingPolicy::CENTER, false });
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 }
183
184 _tmp.allocate();
185 }
186}
187
188void NEGaussianPyramidOrb::run()
189{
190 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
191
192 /* Get number of pyramid levels */
193 const size_t num_levels = _pyramid->info()->num_levels();
194
195 /* The first level of the pyramid has the input image */
196 _pyramid->get_pyramid_level(0)->copy_from(*_input);
197
198 for(unsigned int i = 0; i < num_levels - 1; ++i)
199 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100200 _gaus5x5[i].run();
201 _scale_nearest[i].run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 }
203}