1 /*
2 * Copyright (c) 2017-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/CL/functions/CLGaussianPyramid.h"
25
26 #include "arm_compute/core/CL/ICLTensor.h"
27 #include "arm_compute/core/Error.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/core/Window.h"
32 #include "arm_compute/runtime/CL/CLPyramid.h"
33 #include "arm_compute/runtime/CL/CLScheduler.h"
34 #include "arm_compute/runtime/CL/CLTensor.h"
35 #include "arm_compute/runtime/CL/CLTensorAllocator.h"
36 #include "arm_compute/runtime/CL/functions/CLGaussian5x5.h"
37 #include "src/core/CL/kernels/CLFillBorderKernel.h"
38 #include "src/core/CL/kernels/CLGaussian5x5Kernel.h"
39 #include "src/core/CL/kernels/CLGaussianPyramidKernel.h"
40 #include "src/core/CL/kernels/CLScaleKernel.h"
41 #include "support/MemorySupport.h"
42
43 #include <cstddef>
44
45 using namespace arm_compute;
46
CLGaussianPyramid()47 CLGaussianPyramid::CLGaussianPyramid()
48 : _input(nullptr), _pyramid(nullptr), _tmp()
49 {
50 }
51
52 CLGaussianPyramid::~CLGaussianPyramid() = default;
53
CLGaussianPyramidHalf()54 CLGaussianPyramidHalf::CLGaussianPyramidHalf() // NOLINT
55 : _horizontal_border_handler(),
56 _vertical_border_handler(),
57 _horizontal_reduction(),
58 _vertical_reduction()
59 {
60 }
61
62 CLGaussianPyramidHalf::~CLGaussianPyramidHalf() = default;
63
configure(ICLTensor * input,CLPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)64 void CLGaussianPyramidHalf::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
65 {
66 configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value);
67 }
68
configure(const CLCompileContext & compile_context,ICLTensor * input,CLPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)69 void CLGaussianPyramidHalf::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
70 {
71 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
72 ARM_COMPUTE_ERROR_ON(pyramid == nullptr);
73 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
74 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
75 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
76 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale());
77
78 // Constant value to use for vertical fill border when the border mode is CONSTANT
79 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;
80
81 /* Get number of pyramid levels */
82 const size_t num_levels = pyramid->info()->num_levels();
83
84 _input = input;
85 _pyramid = pyramid;
86
87 if(num_levels > 1)
88 {
89 _horizontal_border_handler.reserve(num_levels - 1);
90 _vertical_border_handler.reserve(num_levels - 1);
91 _horizontal_reduction.reserve(num_levels - 1);
92 _vertical_reduction.reserve(num_levels - 1);
93
94 // Apply half scale to the X dimension of the tensor shape
95 TensorShape tensor_shape = pyramid->info()->tensor_shape();
96 tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
97
98 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::U16);
99 _tmp.init(pyramid_info);
100
101 for(size_t i = 0; i < num_levels - 1; ++i)
102 {
103 /* Configure horizontal kernel */
104 _horizontal_reduction.emplace_back(support::cpp14::make_unique<CLGaussianPyramidHorKernel>());
105 _horizontal_reduction.back()->configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i));
106
107 /* Configure vertical kernel */
108 _vertical_reduction.emplace_back(support::cpp14::make_unique<CLGaussianPyramidVertKernel>());
109 _vertical_reduction.back()->configure(compile_context, _tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1));
110
111 /* Configure border */
112 _horizontal_border_handler.emplace_back(support::cpp14::make_unique<CLFillBorderKernel>());
113 _horizontal_border_handler.back()->configure(compile_context, _pyramid->get_pyramid_level(i), _horizontal_reduction.back()->border_size(), border_mode, PixelValue(constant_border_value));
114
115 /* Configure border */
116 _vertical_border_handler.emplace_back(support::cpp14::make_unique<CLFillBorderKernel>());
117 _vertical_border_handler.back()->configure(compile_context, _tmp.get_pyramid_level(i), _vertical_reduction.back()->border_size(), border_mode, PixelValue(pixel_value_u16));
118 }
119 _tmp.allocate();
120 }
121 }
122
run()123 void CLGaussianPyramidHalf::run()
124 {
125 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
126
127 /* Get number of pyramid levels */
128 const size_t num_levels = _pyramid->info()->num_levels();
129
130 /* The first level of the pyramid has the input image */
131 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
132 _input->map(CLScheduler::get().queue(), true /* blocking */);
133 _pyramid->get_pyramid_level(0)->copy_from(*_input);
134
135 _input->unmap(CLScheduler::get().queue());
136 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
137
138 for(unsigned int i = 0; i < num_levels - 1; ++i)
139 {
140 CLScheduler::get().enqueue(*_horizontal_border_handler[i], false);
141 CLScheduler::get().enqueue(*_horizontal_reduction[i], false);
142 CLScheduler::get().enqueue(*_vertical_border_handler[i], false);
143 CLScheduler::get().enqueue(*_vertical_reduction[i], false);
144 }
145 }
146
CLGaussianPyramidOrb()147 CLGaussianPyramidOrb::CLGaussianPyramidOrb() // NOLINT
148 : _gauss5x5(),
149 _scale_nearest()
150 {
151 }
152
configure(ICLTensor * input,CLPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)153 void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
154 {
155 configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value);
156 }
157
configure(const CLCompileContext & compile_context,ICLTensor * input,CLPyramid * pyramid,BorderMode border_mode,uint8_t constant_border_value)158 void CLGaussianPyramidOrb::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
159 {
160 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
161 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
162 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
163 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
164 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
165 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale());
166
167 /* Get number of pyramid levels */
168 const size_t num_levels = pyramid->info()->num_levels();
169
170 _input = input;
171 _pyramid = pyramid;
172
173 if(num_levels > 1)
174 {
175 _gauss5x5.resize(num_levels - 1);
176 _scale_nearest.reserve(num_levels - 1);
177
178 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
179
180 _tmp.init(pyramid_info);
181
182 for(size_t i = 0; i < num_levels - 1; ++i)
183 {
184 /* Configure gaussian 5x5 */
185 _gauss5x5[i].configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
186
187 /* Configure scale image kernel */
188 _scale_nearest.emplace_back(support::cpp14::make_unique<CLScaleKernel>());
189 _scale_nearest.back()->configure(compile_context, _tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, PixelValue(), SamplingPolicy::CENTER });
190 }
191
192 _tmp.allocate();
193 }
194 }
195
run()196 void CLGaussianPyramidOrb::run()
197 {
198 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
199
200 /* Get number of pyramid levels */
201 const size_t num_levels = _pyramid->info()->num_levels();
202
203 /* The first level of the pyramid has the input image */
204 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
205 _input->map(CLScheduler::get().queue(), true /* blocking */);
206 _pyramid->get_pyramid_level(0)->copy_from(*_input);
207 _input->unmap(CLScheduler::get().queue());
208 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
209
210 for(unsigned int i = 0; i < num_levels - 1; ++i)
211 {
212 _gauss5x5[i].run();
213 CLScheduler::get().enqueue(*_scale_nearest[i]);
214 }
215 }
216