blob: 27c057592cfe405a4a2149462165597e0de06fd3 [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"
Georgios Pinitas658039b2017-09-15 16:30:50 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010035#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <cmath>
38#include <cstddef>
39#include <utility>
40
41using namespace arm_compute;
42
43namespace
44{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070045void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size, SamplingPolicy sampling_policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
47 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070048 ARM_COMPUTE_UNUSED(sampling_policy);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000049 float sampling_offset = 0.0f;
50 if(sampling_policy == SamplingPolicy::CENTER)
51 {
52 sampling_offset = 0.5f;
53 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
55 Window win;
56 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
57 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
58
59 if(dx != nullptr && dy != nullptr)
60 {
61 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
62 Iterator offsets_it(offsets, win);
63 Iterator dx_it(dx, win);
64 Iterator dy_it(dy, win);
65
66 execute_window_loop(win, [&](const Coordinates & id)
67 {
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +000068 const float in_x = (id.x() + sampling_offset) * wr - sampling_offset;
69 const float in_y = (id.y() + sampling_offset) * hr - sampling_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 const int in_xi = std::floor(in_x);
71 const int in_yi = std::floor(in_y);
72
Georgios Pinitasfa7ad562018-05-15 17:38:40 +010073 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * static_cast<int>(input_element_size);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
75 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
76 },
77 offsets_it, dx_it, dy_it);
78 }
79 else
80 {
81 // Pre-compute the offset for NEAREST interpolation
82 Iterator offsets_it(offsets, win);
83
84 execute_window_loop(win, [&](const Coordinates & id)
85 {
Michalis Spyroud4733862019-07-09 14:21:06 +010086 const size_t in_xi = std::floor((id.x() + sampling_offset) * wr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
88 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
89 },
90 offsets_it);
91 }
92}
93} // namespace
94
Georgios Pinitas3021edf2017-09-18 17:55:22 +010095NEScale::NEScale() // NOLINT
96 : _offsets(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010097 _dx(),
Georgios Pinitas658039b2017-09-15 16:30:50 +010098 _dy(),
99 _scale_kernel(),
George Wort05398a92019-01-25 15:38:33 +0000100 _border_handler(),
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000101 _use_padding(true),
102 _align_corners(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
104}
105
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000106void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding,
107 bool align_corners)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108{
Georgios Pinitas20b43132018-05-14 16:05:23 +0100109 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
George Wort05398a92019-01-25 15:38:33 +0000110 ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), policy, border_mode, constant_border_value, sampling_policy, use_padding));
111
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000112 _use_padding = use_padding;
113 _align_corners = policy == InterpolationPolicy::BILINEAR
114 && sampling_policy == SamplingPolicy::TOP_LEFT
115 && align_corners;
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 Parkbb123bd2020-01-03 10:57:30 +0000126 const auto wr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), _align_corners);
127 const auto hr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_height), output->info()->dimension(idx_height), _align_corners);
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
133 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
134 {
135 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
136 }
137
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 switch(policy)
139 {
140 case InterpolationPolicy::NEAREST_NEIGHBOR:
141 {
142 TensorInfo tensor_info_offsets(shape, Format::S32);
143 _offsets.allocator()->init(tensor_info_offsets);
144
George Wort05398a92019-01-25 15:38:33 +0000145 _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 // Allocate once the configure methods have been called
148 _offsets.allocator()->allocate();
149
150 // Pre-compute offsets for nearest interpolation
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700151 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 break;
153 }
154 case InterpolationPolicy::BILINEAR:
155 {
156 TensorInfo tensor_info_offsets(shape, Format::S32);
157 TensorInfo tensor_info_dxdy(shape, Format::F32);
158
159 _offsets.allocator()->init(tensor_info_offsets);
160 _dx.allocator()->init(tensor_info_dxdy);
161 _dy.allocator()->init(tensor_info_dxdy);
162
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000163 _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164
165 // Allocate once the configure methods have been called
166 _offsets.allocator()->allocate();
167 _dx.allocator()->allocate();
168 _dy.allocator()->allocate();
169
170 // Pre-compute dx, dy and offsets for bilinear interpolation
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700171 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 break;
173 }
174 case InterpolationPolicy::AREA:
175 {
George Wort05398a92019-01-25 15:38:33 +0000176 _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 break;
178 }
179 default:
180 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
181 }
George Wort05398a92019-01-25 15:38:33 +0000182 if(use_padding)
183 {
184 _border_handler.configure(input, _scale_kernel.border_size(), border_mode, constant_border_value);
185 }
Georgios Pinitas658039b2017-09-15 16:30:50 +0100186}
187
Georgios Pinitas20b43132018-05-14 16:05:23 +0100188Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000189 BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners)
Georgios Pinitas20b43132018-05-14 16:05:23 +0100190{
191 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathan3ac2f3a2019-01-17 15:16:19 +0000192 ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT);
Georgios Pinitas20b43132018-05-14 16:05:23 +0100193 ARM_COMPUTE_UNUSED(border_mode, constant_border_value);
194
195 ITensorInfo *offsets = nullptr;
196 ITensorInfo *dx = nullptr;
197 ITensorInfo *dy = nullptr;
198
199 // Get data layout and width/height indices
200 const DataLayout data_layout = input->data_layout();
201 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
202 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
203
204 // Get the tensor shape of auxilary buffers
205 const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
206
207 TensorInfo tensor_info_offsets(shape, Format::S32);
208 TensorInfo tensor_info_dx(shape, Format::F32);
209 TensorInfo tensor_info_dy(shape, Format::F32);
210
211 switch(policy)
212 {
213 case InterpolationPolicy::NEAREST_NEIGHBOR:
214 offsets = &tensor_info_offsets;
215 break;
216 case InterpolationPolicy::BILINEAR:
217 offsets = &tensor_info_offsets;
218 dx = &tensor_info_dx;
219 dy = &tensor_info_dy;
220 break;
221 default:
222 break;
223 }
224
225 ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(),
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000226 policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners));
Georgios Pinitas20b43132018-05-14 16:05:23 +0100227 return Status{};
228}
229
Georgios Pinitas658039b2017-09-15 16:30:50 +0100230void NEScale::run()
231{
George Wort05398a92019-01-25 15:38:33 +0000232 if(_use_padding)
233 {
234 NEScheduler::get().schedule(&_border_handler, Window::DimZ);
235 }
Georgios Pinitas658039b2017-09-15 16:30:50 +0100236 NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237}