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