1 /*
2 * Copyright (c) 2016-2020 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"
27 #include "arm_compute/core/ITensor.h"
28 #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"
32 #include "arm_compute/runtime/Pyramid.h"
33 #include "arm_compute/runtime/Tensor.h"
34 #include "arm_compute/runtime/TensorAllocator.h"
35 #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"
40
41 #include <cstddef>
42
43 using namespace arm_compute;
44
NEGaussianPyramid()45 NEGaussianPyramid::NEGaussianPyramid()
46 : _input(nullptr), _pyramid(nullptr), _tmp()
47 {
48 }
49
50 NEGaussianPyramidHalf::~NEGaussianPyramidHalf() = default;
51
NEGaussianPyramidHalf()52 NEGaussianPyramidHalf::NEGaussianPyramidHalf() // NOLINT
53 : _horizontal_border_handler(),
54 _vertical_border_handler(),
55 _horizontal_reduction(),
56 _vertical_reduction()
57 {
58 }
59
configure(const ITensor * input,IPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)60 void 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
69 // 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
72 /* Get number of pyramid levels */
73 const size_t num_levels = pyramid->info()->num_levels();
74 const size_t num_stages = num_levels - 1;
75
76 _input = input;
77 _pyramid = pyramid;
78
79 if(num_levels > 1)
80 {
81 // 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
88 _horizontal_reduction.clear();
89 _vertical_reduction.clear();
90 _horizontal_border_handler.clear();
91 _vertical_border_handler.clear();
92
93 _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)
99 {
100 /* Configure horizontal kernel */
101 _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));
103
104 /* Configure vertical kernel */
105 _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));
107
108 /* Configure border */
109 _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));
111
112 /* Configure border */
113 _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));
115 }
116
117 _tmp.allocate();
118 }
119 }
120
run()121 void NEGaussianPyramidHalf::run()
122 {
123 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
124
125 /* Get number of pyramid levels */
126 const unsigned int num_levels = _pyramid->info()->num_levels();
127
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 {
133 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);
137 }
138 }
139
140 NEGaussianPyramidOrb::~NEGaussianPyramidOrb() = default;
141
NEGaussianPyramidOrb()142 NEGaussianPyramidOrb::NEGaussianPyramidOrb() // NOLINT
143 : _gaus5x5(),
144 _scale_nearest()
145 {
146 }
147
configure(const ITensor * input,IPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)148 void 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();
159 const size_t num_stages = num_levels - 1;
160
161 _input = input;
162 _pyramid = pyramid;
163
164 _gaus5x5.clear();
165 _scale_nearest.clear();
166
167 _gaus5x5.resize(num_stages);
168 _scale_nearest.resize(num_stages);
169
170 if(num_levels > 1)
171 {
172 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
173 _tmp.init(pyramid_info);
174
175 for(size_t i = 0; i < num_levels - 1; ++i)
176 {
177 /* Configure gaussian 5x5 */
178 _gaus5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
179
180 /* Configure scale */
181 _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 });
182 }
183
184 _tmp.allocate();
185 }
186 }
187
run()188 void 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 {
200 _gaus5x5[i].run();
201 _scale_nearest[i].run();
202 }
203 }
204