blob: fbf8812cdd8d4610bfab0e5a9ecbc2bd15b06afc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou299fdd32019-05-01 13:03:59 +01002 * Copyright (c) 2016-2019 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"
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 Pflanzerd0ae8b82017-06-29 14:51:57 +010038#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <cstddef>
41
42using namespace arm_compute;
43
44NEGaussianPyramid::NEGaussianPyramid()
45 : _input(nullptr), _pyramid(nullptr), _tmp()
46{
47}
48
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010049NEGaussianPyramidHalf::NEGaussianPyramidHalf() // NOLINT
Gian Marco37908d92017-11-07 14:38:22 +000050 : _horizontal_border_handler(),
51 _vertical_border_handler(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010052 _horizontal_reduction(),
53 _vertical_reduction()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054{
55}
56
57void 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 Marco37908d92017-11-07 14:38:22 +000066 // 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 Barbier6ff3b192017-09-04 18:44:23 +010069 /* Get number of pyramid levels */
70 const size_t num_levels = pyramid->info()->num_levels();
Georgios Pinitas725b1732019-05-20 19:40:47 +010071 const size_t num_stages = num_levels - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072
73 _input = input;
74 _pyramid = pyramid;
75
76 if(num_levels > 1)
77 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 // Apply half scale to the X dimension of the tensor shape
79 TensorShape tensor_shape = pyramid->info()->tensor_shape();
80 tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
81
82 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::S16);
83 _tmp.init(pyramid_info);
84
Georgios Pinitas725b1732019-05-20 19:40:47 +010085 _horizontal_reduction.clear();
86 _vertical_reduction.clear();
87 _horizontal_border_handler.clear();
88 _vertical_border_handler.clear();
Michalis Spyrou299fdd32019-05-01 13:03:59 +010089
Georgios Pinitas725b1732019-05-20 19:40:47 +010090 _horizontal_reduction.resize(num_stages);
91 _vertical_reduction.resize(num_stages);
92 _horizontal_border_handler.resize(num_stages);
93 _vertical_border_handler.resize(num_stages);
94
95 for(size_t i = 0; i < num_stages; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 {
97 /* Configure horizontal kernel */
Georgios Pinitas725b1732019-05-20 19:40:47 +010098 _horizontal_reduction[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
100 /* Configure vertical kernel */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100101 _vertical_reduction[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
103 /* Configure border */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100104 _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 +0000105
106 /* Configure border */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100107 _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 +0100108 }
109
110 _tmp.allocate();
111 }
112}
113
114void NEGaussianPyramidHalf::run()
115{
116 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
117
118 /* Get number of pyramid levels */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100119 const unsigned int num_levels = _pyramid->info()->num_levels();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
121 /* The first level of the pyramid has the input image */
122 _pyramid->get_pyramid_level(0)->copy_from(*_input);
123
124 for(unsigned int i = 0; i < num_levels - 1; ++i)
125 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100126 NEScheduler::get().schedule(&_horizontal_border_handler[i], Window::DimZ);
127 NEScheduler::get().schedule(&_horizontal_reduction[i], Window::DimY);
128 NEScheduler::get().schedule(&_vertical_border_handler[i], Window::DimZ);
129 NEScheduler::get().schedule(&_vertical_reduction[i], Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 }
131}
132
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100133NEGaussianPyramidOrb::NEGaussianPyramidOrb() // NOLINT
Georgios Pinitas3021edf2017-09-18 17:55:22 +0100134 : _gaus5x5(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100135 _scale_nearest()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136{
137}
138
139void NEGaussianPyramidOrb::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
140{
141 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
142 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
143 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
144 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
145 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
146 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale());
147
148 /* Get number of pyramid levels */
149 const size_t num_levels = pyramid->info()->num_levels();
Georgios Pinitas725b1732019-05-20 19:40:47 +0100150 const size_t num_stages = num_levels - 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
152 _input = input;
153 _pyramid = pyramid;
154
Georgios Pinitas725b1732019-05-20 19:40:47 +0100155 _gaus5x5.clear();
156 _scale_nearest.clear();
157
158 _gaus5x5.resize(num_stages);
159 _scale_nearest.resize(num_stages);
160
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 if(num_levels > 1)
162 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
164 _tmp.init(pyramid_info);
165
Georgios Pinitas725b1732019-05-20 19:40:47 +0100166 for(size_t i = 0; i < num_levels - 1; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 /* Configure gaussian 5x5 */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100169 _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 +0100170
Georgios Pinitas3021edf2017-09-18 17:55:22 +0100171 /* Configure scale */
Georgios Pinitas725b1732019-05-20 19:40:47 +0100172 _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 }
174
175 _tmp.allocate();
176 }
177}
178
179void NEGaussianPyramidOrb::run()
180{
181 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
182
183 /* Get number of pyramid levels */
184 const size_t num_levels = _pyramid->info()->num_levels();
185
186 /* The first level of the pyramid has the input image */
187 _pyramid->get_pyramid_level(0)->copy_from(*_input);
188
189 for(unsigned int i = 0; i < num_levels - 1; ++i)
190 {
Georgios Pinitas725b1732019-05-20 19:40:47 +0100191 _gaus5x5[i].run();
192 _scale_nearest[i].run();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 }
194}