blob: 89d376a1ada44431e77f85a41f55ecd0703377ca [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
2 * Copyright (c) 2017 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/CL/kernels/CLGaussianPyramidKernel.h"
28#include "arm_compute/core/CL/kernels/CLScaleKernel.h"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/PixelValue.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include "arm_compute/runtime/CL/CLPyramid.h"
37#include "arm_compute/runtime/CL/CLPyramid.h"
38#include "arm_compute/runtime/CL/CLScheduler.h"
39#include "arm_compute/runtime/CL/CLTensor.h"
40#include "arm_compute/runtime/CL/CLTensorAllocator.h"
41#include "arm_compute/runtime/CL/functions/CLGaussian5x5.h"
42
43#include <cstddef>
44
45using namespace arm_compute;
46
47CLGaussianPyramid::CLGaussianPyramid()
48 : _input(nullptr), _pyramid(nullptr), _tmp()
49{
50}
51
52CLGaussianPyramidHalf::CLGaussianPyramidHalf()
53 : _border_handler(), _horizontal_reduction(), _vertical_reduction()
54{
55}
56
57void CLGaussianPyramidHalf::configure(ICLTensor *input, CLPyramid *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(pyramid == nullptr);
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
66 /* Get number of pyramid levels */
67 const size_t num_levels = pyramid->info()->num_levels();
68
69 _input = input;
70 _pyramid = pyramid;
71
72 if(num_levels > 1)
73 {
74 _border_handler = arm_compute::cpp14::make_unique<CLFillBorderKernel[]>(num_levels - 1);
75 _horizontal_reduction = arm_compute::cpp14::make_unique<CLGaussianPyramidHorKernel[]>(num_levels - 1);
76 _vertical_reduction = arm_compute::cpp14::make_unique<CLGaussianPyramidVertKernel[]>(num_levels - 1);
77
78 // 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;
83 pyramid_info.init(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::U16);
84
85 _tmp.init_auto_padding(pyramid_info);
86 _tmp.allocate();
87
88 for(size_t i = 0; i < num_levels - 1; ++i)
89 {
90 /* Configure border */
91 _border_handler[i].configure(_pyramid->get_pyramid_level(i), 2, border_mode, PixelValue(constant_border_value));
92
93 /* Configure horizontal kernel */
94 _horizontal_reduction[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode == BorderMode::UNDEFINED);
95
96 /* Configure vertical kernel */
97 _vertical_reduction[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), border_mode == BorderMode::UNDEFINED);
98 }
99 }
100}
101
102void CLGaussianPyramidHalf::run()
103{
104 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
105
106 /* Get number of pyramid levels */
107 const size_t num_levels = _pyramid->info()->num_levels();
108
109 /* The first level of the pyramid has the input image */
110 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
111 _input->map(CLScheduler::get().queue(), true /* blocking */);
112 _pyramid->get_pyramid_level(0)->copy_from(*_input);
113 _input->unmap(CLScheduler::get().queue());
114 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
115
116 for(unsigned int i = 0; i < num_levels - 1; ++i)
117 {
118 CLScheduler::get().enqueue(_border_handler[i], false);
119 CLScheduler::get().enqueue(_horizontal_reduction[i], false);
120 CLScheduler::get().enqueue(_vertical_reduction[i], false);
121 }
122}
123
124CLGaussianPyramidOrb::CLGaussianPyramidOrb()
125 : _gauss5x5(), _scale_nearest()
126{
127}
128
129void CLGaussianPyramidOrb::configure(ICLTensor *input, CLPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
130{
131 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
132 ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
133 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
134 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
135 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
136 ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale());
137
138 /* Get number of pyramid levels */
139 const size_t num_levels = pyramid->info()->num_levels();
140
141 _input = input;
142 _pyramid = pyramid;
143
144 if(num_levels > 1)
145 {
146 _gauss5x5 = arm_compute::cpp14::make_unique<CLGaussian5x5[]>(num_levels - 1);
147 _scale_nearest = arm_compute::cpp14::make_unique<CLScaleKernel[]>(num_levels - 1);
148
149 PyramidInfo pyramid_info;
150 pyramid_info.init(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
151
152 _tmp.init_auto_padding(pyramid_info);
153 _tmp.allocate();
154
155 for(size_t i = 0; i < num_levels - 1; ++i)
156 {
157 /* Configure gaussian 5x5 */
158 _gauss5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
159
160 /* Configure scale image kernel */
161 _scale_nearest[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR, border_mode == BorderMode::UNDEFINED);
162 }
163 }
164}
165
166void CLGaussianPyramidOrb::run()
167{
168 ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
169
170 /* Get number of pyramid levels */
171 const size_t num_levels = _pyramid->info()->num_levels();
172
173 /* The first level of the pyramid has the input image */
174 _pyramid->get_pyramid_level(0)->map(CLScheduler::get().queue(), true /* blocking */);
175 _input->map(CLScheduler::get().queue(), true /* blocking */);
176 _pyramid->get_pyramid_level(0)->copy_from(*_input);
177 _input->unmap(CLScheduler::get().queue());
178 _pyramid->get_pyramid_level(0)->unmap(CLScheduler::get().queue());
179
180 for(unsigned int i = 0; i < num_levels - 1; ++i)
181 {
182 _gauss5x5[i].run();
183 CLScheduler::get().enqueue(_scale_nearest[i]);
184 }
185}