blob: 2278f07a1c7ef3bb42f76264937100d18301ee80 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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{
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010047void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, 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
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010075 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
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 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +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));
90 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 },
92 offsets_it);
93 }
94}
95} // namespace
96
Manuel Bottinifc2f6d02020-08-26 16:28:38 +010097NEScale::NEScale()
98 : _offsets(), _dx(), _dy()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099{
100}
101
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100102void NEScale::configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100104 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100105 ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), info));
George Wort05398a92019-01-25 15:38:33 +0000106
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100107 const bool is_align_corners_used = info.align_corners && arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100109 // Get data layout and width/height indices
110 const DataLayout data_layout = input->info()->data_layout();
111 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
112 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 // Get the tensor shape
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100115 TensorShape shape(output->info()->dimension(idx_width));
116 shape.set(1, output->info()->dimension(idx_height), false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
118 // Compute the ratio between source width/height and destination width/height
Sang-Hoon Park3687ee12020-06-24 13:34:04 +0100119 const auto wr = arm_compute::scale_utils::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), is_align_corners_used);
120 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 +0100121
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100123 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 +0100124
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100125 auto scale_kernel = arm_compute::support::cpp14::make_unique<NEScaleKernel>();
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100126 switch(policy_to_use)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 {
128 case InterpolationPolicy::NEAREST_NEIGHBOR:
129 {
130 TensorInfo tensor_info_offsets(shape, Format::S32);
131 _offsets.allocator()->init(tensor_info_offsets);
132
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100133 scale_kernel->configure(input, nullptr, nullptr, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
135 // Allocate once the configure methods have been called
136 _offsets.allocator()->allocate();
137
138 // Pre-compute offsets for nearest interpolation
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100139 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 break;
141 }
142 case InterpolationPolicy::BILINEAR:
143 {
144 TensorInfo tensor_info_offsets(shape, Format::S32);
145 TensorInfo tensor_info_dxdy(shape, Format::F32);
146
147 _offsets.allocator()->init(tensor_info_offsets);
148 _dx.allocator()->init(tensor_info_dxdy);
149 _dy.allocator()->init(tensor_info_dxdy);
150
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100151 scale_kernel->configure(input, &_dx, &_dy, &_offsets, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
153 // Allocate once the configure methods have been called
154 _offsets.allocator()->allocate();
155 _dx.allocator()->allocate();
156 _dy.allocator()->allocate();
157
158 // Pre-compute dx, dy and offsets for bilinear interpolation
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100159 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, info.sampling_policy, is_align_corners_used);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 break;
161 }
162 case InterpolationPolicy::AREA:
163 {
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100164 scale_kernel->configure(input, nullptr, nullptr, nullptr, output, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 break;
166 }
167 default:
168 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
169 }
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100170 _kernel = std::move(scale_kernel);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100171}
172
173Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100174{
175 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100176 ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100177
178 ITensorInfo *offsets = nullptr;
179 ITensorInfo *dx = nullptr;
180 ITensorInfo *dy = nullptr;
181
182 // Get data layout and width/height indices
183 const DataLayout data_layout = input->data_layout();
184 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
185 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
186
187 // Get the tensor shape of auxilary buffers
188 const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
189
190 TensorInfo tensor_info_offsets(shape, Format::S32);
191 TensorInfo tensor_info_dx(shape, Format::F32);
192 TensorInfo tensor_info_dy(shape, Format::F32);
193
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100194 switch(info.interpolation_policy)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100195 {
196 case InterpolationPolicy::NEAREST_NEIGHBOR:
197 offsets = &tensor_info_offsets;
198 break;
199 case InterpolationPolicy::BILINEAR:
200 offsets = &tensor_info_offsets;
201 dx = &tensor_info_dx;
202 dy = &tensor_info_dy;
203 break;
204 default:
205 break;
206 }
207
Sang-Hoon Parkc2617982020-05-20 22:13:47 +0100208 ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), info));
209 return Status{};
210}
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100211} // namespace arm_compute