Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/PixelValue.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/TensorAllocator.h" |
| 35 | |
| 36 | #include <cmath> |
| 37 | #include <cstddef> |
| 38 | #include <utility> |
| 39 | |
Sang-Hoon Park | ccd9496 | 2020-06-09 12:09:24 +0100 | [diff] [blame^] | 40 | namespace arm_compute |
| 41 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 44 | void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size, SamplingPolicy sampling_policy) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | { |
| 46 | ARM_COMPUTE_ERROR_ON(nullptr == offsets); |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 47 | ARM_COMPUTE_UNUSED(sampling_policy); |
Vidhya Sudhan Loganathan | 3ac2f3a | 2019-01-17 15:16:19 +0000 | [diff] [blame] | 48 | float sampling_offset = 0.0f; |
| 49 | if(sampling_policy == SamplingPolicy::CENTER) |
| 50 | { |
| 51 | sampling_offset = 0.5f; |
| 52 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | |
| 54 | Window win; |
| 55 | win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1)); |
| 56 | win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1)); |
| 57 | |
| 58 | if(dx != nullptr && dy != nullptr) |
| 59 | { |
| 60 | // Pre-compute the offset and pixel's distance for BILINEAR interpolation |
| 61 | Iterator offsets_it(offsets, win); |
| 62 | Iterator dx_it(dx, win); |
| 63 | Iterator dy_it(dy, win); |
| 64 | |
| 65 | execute_window_loop(win, [&](const Coordinates & id) |
| 66 | { |
Vidhya Sudhan Loganathan | 3ac2f3a | 2019-01-17 15:16:19 +0000 | [diff] [blame] | 67 | const float in_x = (id.x() + sampling_offset) * wr - sampling_offset; |
| 68 | const float in_y = (id.y() + sampling_offset) * hr - sampling_offset; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | const int in_xi = std::floor(in_x); |
| 70 | const int in_yi = std::floor(in_y); |
| 71 | |
Georgios Pinitas | fa7ad56 | 2018-05-15 17:38:40 +0100 | [diff] [blame] | 72 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * static_cast<int>(input_element_size); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi; |
| 74 | *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi; |
| 75 | }, |
| 76 | offsets_it, dx_it, dy_it); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | // Pre-compute the offset for NEAREST interpolation |
| 81 | Iterator offsets_it(offsets, win); |
| 82 | |
| 83 | execute_window_loop(win, [&](const Coordinates & id) |
| 84 | { |
Michalis Spyrou | d473386 | 2019-07-09 14:21:06 +0100 | [diff] [blame] | 85 | const size_t in_xi = std::floor((id.x() + sampling_offset) * wr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | |
| 87 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size; |
| 88 | }, |
| 89 | offsets_it); |
| 90 | } |
| 91 | } |
| 92 | } // namespace |
| 93 | |
Georgios Pinitas | 3021edf | 2017-09-18 17:55:22 +0100 | [diff] [blame] | 94 | NEScale::NEScale() // NOLINT |
| 95 | : _offsets(), |
Moritz Pflanzer | f4af76e | 2017-09-06 07:42:43 +0100 | [diff] [blame] | 96 | _dx(), |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 97 | _dy(), |
| 98 | _scale_kernel(), |
George Wort | 05398a9 | 2019-01-25 15:38:33 +0000 | [diff] [blame] | 99 | _border_handler(), |
Sang-Hoon Park | f025a77 | 2020-05-26 11:11:32 +0100 | [diff] [blame] | 100 | _use_padding(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | { |
| 102 | } |
| 103 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 104 | void NEScale::configure(ITensor *input, ITensor *output, const ScaleKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | { |
Georgios Pinitas | 20b4313 | 2018-05-14 16:05:23 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), info)); |
George Wort | 05398a9 | 2019-01-25 15:38:33 +0000 | [diff] [blame] | 108 | |
Sang-Hoon Park | f025a77 | 2020-05-26 11:11:32 +0100 | [diff] [blame] | 109 | _use_padding = info.use_padding; |
| 110 | const bool is_align_corners_used = info.align_corners && is_align_corners_allowed(info.sampling_policy); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 112 | // Get data layout and width/height indices |
| 113 | const DataLayout data_layout = input->info()->data_layout(); |
| 114 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 115 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | // Get the tensor shape |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 118 | const TensorShape shape(output->info()->dimension(idx_width), output->info()->dimension(idx_height)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
| 120 | // Compute the ratio between source width/height and destination width/height |
Sang-Hoon Park | f025a77 | 2020-05-26 11:11:32 +0100 | [diff] [blame] | 121 | const auto wr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_width), output->info()->dimension(idx_width), is_align_corners_used); |
| 122 | const auto hr = arm_compute::calculate_resize_ratio(input->info()->dimension(idx_height), output->info()->dimension(idx_height), is_align_corners_used); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | |
| 124 | // Get the element size of the input image |
| 125 | const size_t input_element_size = input->info()->element_size(); |
| 126 | |
| 127 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 128 | const auto policy_to_use = (info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : info.interpolation_policy; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 130 | switch(policy_to_use) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | { |
| 132 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 133 | { |
| 134 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 135 | _offsets.allocator()->init(tensor_info_offsets); |
| 136 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 137 | _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
| 139 | // Allocate once the configure methods have been called |
| 140 | _offsets.allocator()->allocate(); |
| 141 | |
| 142 | // Pre-compute offsets for nearest interpolation |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 143 | precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, info.sampling_policy); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | case InterpolationPolicy::BILINEAR: |
| 147 | { |
| 148 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 149 | TensorInfo tensor_info_dxdy(shape, Format::F32); |
| 150 | |
| 151 | _offsets.allocator()->init(tensor_info_offsets); |
| 152 | _dx.allocator()->init(tensor_info_dxdy); |
| 153 | _dy.allocator()->init(tensor_info_dxdy); |
| 154 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 155 | _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | |
| 157 | // Allocate once the configure methods have been called |
| 158 | _offsets.allocator()->allocate(); |
| 159 | _dx.allocator()->allocate(); |
| 160 | _dy.allocator()->allocate(); |
| 161 | |
| 162 | // Pre-compute dx, dy and offsets for bilinear interpolation |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 163 | precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, info.sampling_policy); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | case InterpolationPolicy::AREA: |
| 167 | { |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 168 | _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | default: |
| 172 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 173 | } |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 174 | if(info.use_padding) |
George Wort | 05398a9 | 2019-01-25 15:38:33 +0000 | [diff] [blame] | 175 | { |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 176 | _border_handler.configure(input, _scale_kernel.border_size(), info.border_mode, info.constant_border_value); |
George Wort | 05398a9 | 2019-01-25 15:38:33 +0000 | [diff] [blame] | 177 | } |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 180 | void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, |
| 181 | bool align_corners) |
| 182 | { |
| 183 | configure(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners }); |
| 184 | } |
| 185 | |
| 186 | Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info) |
Georgios Pinitas | 20b4313 | 2018-05-14 16:05:23 +0100 | [diff] [blame] | 187 | { |
| 188 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT); |
Georgios Pinitas | 20b4313 | 2018-05-14 16:05:23 +0100 | [diff] [blame] | 190 | |
| 191 | ITensorInfo *offsets = nullptr; |
| 192 | ITensorInfo *dx = nullptr; |
| 193 | ITensorInfo *dy = nullptr; |
| 194 | |
| 195 | // Get data layout and width/height indices |
| 196 | const DataLayout data_layout = input->data_layout(); |
| 197 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 198 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 199 | |
| 200 | // Get the tensor shape of auxilary buffers |
| 201 | const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height)); |
| 202 | |
| 203 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 204 | TensorInfo tensor_info_dx(shape, Format::F32); |
| 205 | TensorInfo tensor_info_dy(shape, Format::F32); |
| 206 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 207 | switch(info.interpolation_policy) |
Georgios Pinitas | 20b4313 | 2018-05-14 16:05:23 +0100 | [diff] [blame] | 208 | { |
| 209 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 210 | offsets = &tensor_info_offsets; |
| 211 | break; |
| 212 | case InterpolationPolicy::BILINEAR: |
| 213 | offsets = &tensor_info_offsets; |
| 214 | dx = &tensor_info_dx; |
| 215 | dy = &tensor_info_dy; |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | |
Sang-Hoon Park | c261798 | 2020-05-20 22:13:47 +0100 | [diff] [blame] | 221 | ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(), info)); |
| 222 | return Status{}; |
| 223 | } |
| 224 | |
| 225 | Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, |
| 226 | BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy, bool use_padding, bool align_corners) |
| 227 | { |
| 228 | ARM_COMPUTE_RETURN_ON_ERROR(NEScale::validate(input, output, ScaleKernelInfo{ policy, border_mode, constant_border_value, sampling_policy, use_padding, align_corners })); |
Georgios Pinitas | 20b4313 | 2018-05-14 16:05:23 +0100 | [diff] [blame] | 229 | return Status{}; |
| 230 | } |
| 231 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 232 | void NEScale::run() |
| 233 | { |
George Wort | 05398a9 | 2019-01-25 15:38:33 +0000 | [diff] [blame] | 234 | if(_use_padding) |
| 235 | { |
| 236 | NEScheduler::get().schedule(&_border_handler, Window::DimZ); |
| 237 | } |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 238 | NEScheduler::get().schedule(&_scale_kernel, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 239 | } |
Sang-Hoon Park | ccd9496 | 2020-06-09 12:09:24 +0100 | [diff] [blame^] | 240 | } // namespace arm_compute |