blob: fd82769004f73bdfac55da0223a6a424eade8823 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sanghoon Lee1cd41492018-03-15 11:48:48 +00002 * Copyright (c) 2017-2018 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"
27#include "arm_compute/core/CL/kernels/CLGaussianPyramidKernel.h"
28#include "arm_compute/core/CL/kernels/CLScaleKernel.h"
29#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/PixelValue.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010034#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#include "arm_compute/runtime/CL/CLPyramid.h"
37#include "arm_compute/runtime/CL/CLScheduler.h"
38#include "arm_compute/runtime/CL/CLTensor.h"
39#include "arm_compute/runtime/CL/CLTensorAllocator.h"
40#include "arm_compute/runtime/CL/functions/CLGaussian5x5.h"
41
42#include <cstddef>
43
44using namespace arm_compute;
45
46CLGaussianPyramid::CLGaussianPyramid()
47 : _input(nullptr), _pyramid(nullptr), _tmp()
48{
49}
50
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010051CLGaussianPyramidHalf::CLGaussianPyramidHalf() // NOLINT
Sanghoon Lee1cd41492018-03-15 11:48:48 +000052 : _horizontal_border_handler(),
53 _vertical_border_handler(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010054 _horizontal_reduction(),
55 _vertical_reduction()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056{
57}
58
59void CLGaussianPyramidHalf::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
60{
61 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
62 ARM_COMPUTE_ERROR_ON(pyramid == nullptr);
63 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
64 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
65 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
66 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale());
67
Sanghoon Lee1cd41492018-03-15 11:48:48 +000068 // Constant value to use for vertical fill border when the border mode is CONSTANT
69 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;
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 /* Get number of pyramid levels */
72 const size_t num_levels = pyramid->info()->num_levels();
73
74 _input = input;
75 _pyramid = pyramid;
76
77 if(num_levels > 1)
78 {
Sanghoon Lee1cd41492018-03-15 11:48:48 +000079 _horizontal_border_handler = arm_compute::support::cpp14::make_unique<CLFillBorderKernel[]>(num_levels - 1);
80 _vertical_border_handler = arm_compute::support::cpp14::make_unique<CLFillBorderKernel[]>(num_levels - 1);
81 _horizontal_reduction = arm_compute::support::cpp14::make_unique<CLGaussianPyramidHorKernel[]>(num_levels - 1);
82 _vertical_reduction = arm_compute::support::cpp14::make_unique<CLGaussianPyramidVertKernel[]>(num_levels - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083
84 // Apply half scale to the X dimension of the tensor shape
85 TensorShape tensor_shape = pyramid->info()->tensor_shape();
86 tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
87
88 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::U16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 _tmp.init(pyramid_info);
90
91 for(size_t i = 0; i < num_levels - 1; ++i)
92 {
93 /* Configure horizontal kernel */
Sanghoon Lee1cd41492018-03-15 11:48:48 +000094 _horizontal_reduction[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96 /* Configure vertical kernel */
Sanghoon Lee1cd41492018-03-15 11:48:48 +000097 _vertical_reduction[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
99 /* Configure border */
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000100 _horizontal_border_handler[i].configure(_pyramid->get_pyramid_level(i), _horizontal_reduction[i].border_size(), border_mode, PixelValue(constant_border_value));
101
102 /* Configure border */
103 _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 +0100104 }
105 _tmp.allocate();
106 }
107}
108
109void CLGaussianPyramidHalf::run()
110{
111 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
112
113 /* Get number of pyramid levels */
114 const size_t num_levels = _pyramid->info()->num_levels();
115
116 /* The first level of the pyramid has the input image */
117 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
118 _input->map(CLScheduler::get().queue(), true /* blocking */);
119 _pyramid->get_pyramid_level(0)->copy_from(*_input);
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000120
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 _input->unmap(CLScheduler::get().queue());
122 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
123
124 for(unsigned int i = 0; i < num_levels - 1; ++i)
125 {
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000126 CLScheduler::get().enqueue(_horizontal_border_handler[i], false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 CLScheduler::get().enqueue(_horizontal_reduction[i], false);
Sanghoon Lee1cd41492018-03-15 11:48:48 +0000128 CLScheduler::get().enqueue(_vertical_border_handler[i], false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 CLScheduler::get().enqueue(_vertical_reduction[i], false);
130 }
131}
132
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100133CLGaussianPyramidOrb::CLGaussianPyramidOrb() // NOLINT
134 : _gauss5x5(),
135 _scale_nearest()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136{
137}
138
139void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *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();
150
151 _input = input;
152 _pyramid = pyramid;
153
154 if(num_levels > 1)
155 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100156 _gauss5x5 = arm_compute::support::cpp14::make_unique<CLGaussian5x5[]>(num_levels - 1);
157 _scale_nearest = arm_compute::support::cpp14::make_unique<CLScaleKernel[]>(num_levels - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
159 PyramidInfo pyramid_info(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
160
161 _tmp.init(pyramid_info);
162
163 for(size_t i = 0; i < num_levels - 1; ++i)
164 {
165 /* Configure gaussian 5x5 */
166 _gauss5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
167
168 /* Configure scale image kernel */
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100169 _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, SamplingPolicy::CENTER);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 }
171
172 _tmp.allocate();
173 }
174}
175
176void CLGaussianPyramidOrb::run()
177{
178 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
179
180 /* Get number of pyramid levels */
181 const size_t num_levels = _pyramid->info()->num_levels();
182
183 /* The first level of the pyramid has the input image */
184 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
185 _input->map(CLScheduler::get().queue(), true /* blocking */);
186 _pyramid->get_pyramid_level(0)->copy_from(*_input);
187 _input->unmap(CLScheduler::get().queue());
188 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
189
190 for(unsigned int i = 0; i < num_levels - 1; ++i)
191 {
192 _gauss5x5[i].run();
193 CLScheduler::get().enqueue(_scale_nearest[i]);
194 }
195}