blob: 9fe35f6f0e640b41275a78de18f2ea408205c643 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-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/CL/functions/CLGaussianPyramid.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.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/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010037#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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
42#include <cstddef>
43
44using namespace arm_compute;
45
46CLGaussianPyramid::CLGaussianPyramid()
47 : _input(nullptr), _pyramid(nullptr), _tmp()
48{
49}
50
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010051CLGaussianPyramid::~CLGaussianPyramid() = default;
52
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010053CLGaussianPyramidHalf::CLGaussianPyramidHalf() // NOLINT
Sanghoon Lee1cd41492018-03-15 11:48:48 +000054 : _horizontal_border_handler(),
55 _vertical_border_handler(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010056 _horizontal_reduction(),
57 _vertical_reduction()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
59}
60
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010061CLGaussianPyramidHalf::~CLGaussianPyramidHalf() = default;
62
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063void CLGaussianPyramidHalf::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
64{
Manuel Bottini2b84be52020-04-08 10:15:51 +010065 configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value);
66}
67
68void CLGaussianPyramidHalf::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
69{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
71 ARM_COMPUTE_ERROR_ON(pyramid == nullptr);
72 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
73 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
74 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
75 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale());
76
Sanghoon Lee1cd41492018-03-15 11:48:48 +000077 // Constant value to use for vertical fill border when the border mode is CONSTANT
78 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;
79
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 /* Get number of pyramid levels */
81 const size_t num_levels = pyramid->info()->num_levels();
82
83 _input = input;
84 _pyramid = pyramid;
85
86 if(num_levels > 1)
87 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010088 _horizontal_border_handler.reserve(num_levels - 1);
89 _vertical_border_handler.reserve(num_levels - 1);
90 _horizontal_reduction.reserve(num_levels - 1);
91 _vertical_reduction.reserve(num_levels - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
93 // Apply half scale to the X dimension of the tensor shape
94 TensorShape tensor_shape = pyramid->info()->tensor_shape();
95 tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
96
97 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::U16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 _tmp.init(pyramid_info);
99
100 for(size_t i = 0; i < num_levels - 1; ++i)
101 {
102 /* Configure horizontal kernel */
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000103 _horizontal_reduction.emplace_back(std::make_unique<CLGaussianPyramidHorKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100104 _horizontal_reduction.back()->configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106 /* Configure vertical kernel */
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000107 _vertical_reduction.emplace_back(std::make_unique<CLGaussianPyramidVertKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100108 _vertical_reduction.back()->configure(compile_context, _tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110 /* Configure border */
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000111 _horizontal_border_handler.emplace_back(std::make_unique<CLFillBorderKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100112 _horizontal_border_handler.back()->configure(compile_context, _pyramid->get_pyramid_level(i), _horizontal_reduction.back()->border_size(), border_mode, PixelValue(constant_border_value));
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000113
114 /* Configure border */
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000115 _vertical_border_handler.emplace_back(std::make_unique<CLFillBorderKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100116 _vertical_border_handler.back()->configure(compile_context, _tmp.get_pyramid_level(i), _vertical_reduction.back()->border_size(), border_mode, PixelValue(pixel_value_u16));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 }
118 _tmp.allocate();
119 }
120}
121
122void CLGaussianPyramidHalf::run()
123{
124 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
125
126 /* Get number of pyramid levels */
127 const size_t num_levels = _pyramid->info()->num_levels();
128
129 /* The first level of the pyramid has the input image */
130 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
131 _input->map(CLScheduler::get().queue(), true /* blocking */);
132 _pyramid->get_pyramid_level(0)->copy_from(*_input);
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000133
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 _input->unmap(CLScheduler::get().queue());
135 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
136
137 for(unsigned int i = 0; i < num_levels - 1; ++i)
138 {
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100139 CLScheduler::get().enqueue(*_horizontal_border_handler[i], false);
140 CLScheduler::get().enqueue(*_horizontal_reduction[i], false);
141 CLScheduler::get().enqueue(*_vertical_border_handler[i], false);
142 CLScheduler::get().enqueue(*_vertical_reduction[i], false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 }
144}
145
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100146CLGaussianPyramidOrb::CLGaussianPyramidOrb() // NOLINT
147 : _gauss5x5(),
148 _scale_nearest()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149{
150}
151
152void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
153{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100154 configure(CLKernelLibrary::get().get_compile_context(), input, pyramid, border_mode, constant_border_value);
155}
156
157void CLGaussianPyramidOrb::configure(const CLCompileContext &compile_context, ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
158{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
160 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
161 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
162 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
163 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
164 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale());
165
166 /* Get number of pyramid levels */
167 const size_t num_levels = pyramid->info()->num_levels();
168
169 _input = input;
170 _pyramid = pyramid;
171
172 if(num_levels > 1)
173 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100174 _gauss5x5.resize(num_levels - 1);
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100175 _scale_nearest.reserve(num_levels - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
177 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
178
179 _tmp.init(pyramid_info);
180
181 for(size_t i = 0; i < num_levels - 1; ++i)
182 {
183 /* Configure gaussian 5x5 */
Manuel Bottini2b84be52020-04-08 10:15:51 +0100184 _gauss5x5[i].configure(compile_context, _pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185
186 /* Configure scale image kernel */
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000187 _scale_nearest.emplace_back(std::make_unique<CLScaleKernel>());
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100188 _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 });
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 }
190
191 _tmp.allocate();
192 }
193}
194
195void CLGaussianPyramidOrb::run()
196{
197 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
198
199 /* Get number of pyramid levels */
200 const size_t num_levels = _pyramid->info()->num_levels();
201
202 /* The first level of the pyramid has the input image */
203 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
204 _input->map(CLScheduler::get().queue(), true /* blocking */);
205 _pyramid->get_pyramid_level(0)->copy_from(*_input);
206 _input->unmap(CLScheduler::get().queue());
207 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
208
209 for(unsigned int i = 0; i < num_levels - 1; ++i)
210 {
211 _gauss5x5[i].run();
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100212 CLScheduler::get().enqueue(*_scale_nearest[i]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 }
214}