blob: eefdfdbaa78a3cb00e9918c00ec920ba16947c02 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +00002 * Copyright (c) 2016-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/NEON/functions/NEScale.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.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/Window.h"
Sang-Hoon Park94d50512020-07-02 10:49:39 +010033#include "arm_compute/core/utils/misc/Rounding.h"
Georgios Pinitas658039b2017-09-15 16:30:50 +010034#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/runtime/TensorAllocator.h"
36
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010037#include "src/core/utils/ScaleUtils.h"
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include <cmath>
40#include <cstddef>
41#include <utility>
42
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010043namespace arm_compute
44{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045namespace
46{
Sang-Hoon Park94d50512020-07-02 10:49:39 +010047void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size, SamplingPolicy sampling_policy, bool align_corners)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
49 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070050 ARM_COMPUTE_UNUSED(sampling_policy);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000051 float sampling_offset = 0.0f;
52 if(sampling_policy == SamplingPolicy::CENTER)
53 {
54 sampling_offset = 0.5f;
55 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
57 Window win;
58 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
59 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
60
61 if(dx != nullptr && dy != nullptr)
62 {
63 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
64 Iterator offsets_it(offsets, win);
65 Iterator dx_it(dx, win);
66 Iterator dy_it(dy, win);
67
68 execute_window_loop(win, [&](const Coordinates & id)
69 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000070 const float in_x = (id.x() + sampling_offset) * wr - sampling_offset;
71 const float in_y = (id.y() + sampling_offset) * hr - sampling_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 const int in_xi = std::floor(in_x);
73 const int in_yi = std::floor(in_y);
74
Georgios Pinitasfa7ad562018-05-15 17:38:40 +010075 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * static_cast<int>(input_element_size);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
77 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
78 },
79 offsets_it, dx_it, dy_it);
80 }
81 else
82 {
83 // Pre-compute the offset for NEAREST interpolation
84 Iterator offsets_it(offsets, win);
85
86 execute_window_loop(win, [&](const Coordinates & id)
87 {
Sang-Hoon Park94d50512020-07-02 10:49:39 +010088 const float float_in_xi = (id.x() + sampling_offset) * wr;
89 const auto in_xi = static_cast<size_t>(align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(float_in_xi) : std::floor(float_in_xi));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
92 },
93 offsets_it);
94 }
95}
96} // namespace
97
Georgios Pinitas3021edf2017-09-18 17:55:22 +010098NEScale::NEScale() // NOLINT
99 : _offsets(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +0100100 _dx(),
Georgios Pinitas658039b2017-09-15 16:30:50 +0100101 _dy(),
102 _scale_kernel(),
George Wort05398a92019-01-25 15:38:33 +0000103 _border_handler(),
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100104 _use_padding(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105{
106}
107
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100108void NEScale::configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100110 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100111 ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), info));
George Wort05398a92019-01-25 15:38:33 +0000112
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100113 _use_padding = info.use_padding;
Sang-Hoon Park3687ee12020-06-24 13:34:04 +0100114 const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy)
115 && arm_compute::scale_utils::is_align_corners_allowed_output_shape(output->info()->tensor_shape(), output->info()->data_layout());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100117 // Get data layout and width/height indices
118 const DataLayout data_layout = input->info()->data_layout();
119 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
120 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 // Get the tensor shape
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100123 const TensorShape shape(output->info()->dimension(idx_width), output->info()->dimension(idx_height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125 // Compute the ratio between source width/height and destination width/height
Sang-Hoon Park3687ee12020-06-24 13:34:04 +0100126 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), is_align_corners_used);
127 const auto hr = arm_compute::scale_utils::calculate_resize_ratio(input->info()->dimension(idx_height), output->info()->dimension(idx_height), is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 // Get the element size of the input image
130 const size_t input_element_size = input->info()->element_size();
131
132 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100133 const auto policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100135 switch(policy_to_use)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 {
137 case InterpolationPolicy::NEAREST_NEIGHBOR:
138 {
139 TensorInfo tensor_info_offsets(shape, Format::S32);
140 _offsets.allocator()->init(tensor_info_offsets);
141
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100142 _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
144 // Allocate once the configure methods have been called
145 _offsets.allocator()->allocate();
146
147 // Pre-compute offsets for nearest interpolation
Sang-Hoon Park94d50512020-07-02 10:49:39 +0100148 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 break;
150 }
151 case InterpolationPolicy::BILINEAR:
152 {
153 TensorInfo tensor_info_offsets(shape, Format::S32);
154 TensorInfo tensor_info_dxdy(shape, Format::F32);
155
156 _offsets.allocator()->init(tensor_info_offsets);
157 _dx.allocator()->init(tensor_info_dxdy);
158 _dy.allocator()->init(tensor_info_dxdy);
159
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100160 _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
162 // Allocate once the configure methods have been called
163 _offsets.allocator()->allocate();
164 _dx.allocator()->allocate();
165 _dy.allocator()->allocate();
166
167 // Pre-compute dx, dy and offsets for bilinear interpolation
Sang-Hoon Park94d50512020-07-02 10:49:39 +0100168 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 break;
170 }
171 case InterpolationPolicy::AREA:
172 {
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100173 _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 break;
175 }
176 default:
177 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
178 }
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100179 if(info.use_padding)
George Wort05398a92019-01-25 15:38:33 +0000180 {
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100181 _border_handler.configure(input, _scale_kernel.border_size(), info.border_mode, info.constant_border_value);
George Wort05398a92019-01-25 15:38:33 +0000182 }
Georgios Pinitas658039b2017-09-15 16:30:50 +0100183}
184
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100185void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding,
186 bool align_corners)
187{
188 configure(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners });
189}
190
191Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100192{
193 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100194 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100195
196 ITensorInfo *offsets = nullptr;
197 ITensorInfo *dx = nullptr;
198 ITensorInfo *dy = nullptr;
199
200 // Get data layout and width/height indices
201 const DataLayout data_layout = input->data_layout();
202 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
203 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
204
205 // Get the tensor shape of auxilary buffers
206 const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
207
208 TensorInfo tensor_info_offsets(shape, Format::S32);
209 TensorInfo tensor_info_dx(shape, Format::F32);
210 TensorInfo tensor_info_dy(shape, Format::F32);
211
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100212 switch(info.interpolation_policy)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100213 {
214 case InterpolationPolicy::NEAREST_NEIGHBOR:
215 offsets = &tensor_info_offsets;
216 break;
217 case InterpolationPolicy::BILINEAR:
218 offsets = &tensor_info_offsets;
219 dx = &tensor_info_dx;
220 dy = &tensor_info_dy;
221 break;
222 default:
223 break;
224 }
225
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100226 ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), info));
227 return Status{};
228}
229
230Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy,
231 BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners)
232{
233 ARM_COMPUTE_RETURN_ON_ERROR(NEScale::validate(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners }));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100234 return Status{};
235}
236
Georgios Pinitas658039b2017-09-15 16:30:50 +0100237void NEScale::run()
238{
George Wort05398a92019-01-25 15:38:33 +0000239 if(_use_padding)
240 {
241 NEScheduler::get().schedule(&_border_handler, Window::DimZ);
242 }
Georgios Pinitas658039b2017-09-15 16:30:50 +0100243 NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244}
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100245} // namespace arm_compute