Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +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 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 25 | #include "Scale.h" |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 26 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 27 | #include "Utils.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/Utility.h" |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | template <typename T> |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 40 | SimpleTensor<T> scale_core(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, |
| 41 | SamplingPolicy sampling_policy, bool ceil_policy_scale) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 42 | { |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 43 | // Add 1 if ceil_policy_scale is true |
| 44 | const size_t round_value = ceil_policy_scale ? 1U : 0U; |
| 45 | TensorShape shape_scaled(in.shape()); |
| 46 | shape_scaled.set(0, (in.shape()[0] + round_value) * scale_x); |
| 47 | shape_scaled.set(1, (in.shape()[1] + round_value) * scale_y); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 48 | SimpleTensor<T> out(shape_scaled, in.data_type()); |
| 49 | |
| 50 | // Compute the ratio between source width/height and destination width/height |
| 51 | const auto wr = static_cast<float>(in.shape()[0]) / static_cast<float>(out.shape()[0]); |
| 52 | const auto hr = static_cast<float>(in.shape()[1]) / static_cast<float>(out.shape()[1]); |
| 53 | |
| 54 | const auto width = static_cast<int>(in.shape().x()); |
| 55 | const auto height = static_cast<int>(in.shape().y()); |
| 56 | |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 57 | // Determine border size |
| 58 | const int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1; |
| 59 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 60 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
| 61 | if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) |
| 62 | { |
| 63 | policy = InterpolationPolicy::NEAREST_NEIGHBOR; |
| 64 | } |
| 65 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 66 | const uint32_t num_elements = out.num_elements(); |
| 67 | for(uint32_t element_idx = 0, count = 0; element_idx < num_elements; ++element_idx, ++count) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 68 | { |
| 69 | Coordinates id = index2coord(out.shape(), element_idx); |
| 70 | int idx = id.x(); |
| 71 | int idy = id.y(); |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 72 | float x_src = 0; |
| 73 | float y_src = 0; |
| 74 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 75 | switch(policy) |
| 76 | { |
| 77 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 78 | { |
Michalis Spyrou | d473386 | 2019-07-09 14:21:06 +0100 | [diff] [blame] | 79 | switch(sampling_policy) |
| 80 | { |
| 81 | case SamplingPolicy::TOP_LEFT: |
| 82 | x_src = std::floor(idx * wr); |
| 83 | y_src = std::floor(idy * hr); |
| 84 | break; |
| 85 | case SamplingPolicy::CENTER: |
| 86 | //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords |
| 87 | x_src = (idx + 0.5f) * wr; |
| 88 | y_src = (idy + 0.5f) * hr; |
| 89 | break; |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Unsupported sampling policy."); |
| 92 | } |
| 93 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 94 | id.set(0, x_src); |
| 95 | id.set(1, y_src); |
| 96 | |
| 97 | // If coordinates in range of tensor's width or height |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 98 | if(is_valid_pixel_index(x_src, y_src, width, height, border_size)) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 99 | { |
| 100 | out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 101 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | case InterpolationPolicy::BILINEAR: |
| 105 | { |
Michalis Spyrou | d473386 | 2019-07-09 14:21:06 +0100 | [diff] [blame] | 106 | switch(sampling_policy) |
| 107 | { |
| 108 | case SamplingPolicy::TOP_LEFT: |
| 109 | x_src = idx * wr; |
| 110 | y_src = idy * hr; |
| 111 | break; |
| 112 | case SamplingPolicy::CENTER: |
| 113 | x_src = (idx + 0.5f) * wr - 0.5f; |
| 114 | y_src = (idy + 0.5f) * hr - 0.5f; |
| 115 | break; |
| 116 | default: |
| 117 | ARM_COMPUTE_ERROR("Unsupported sampling policy."); |
| 118 | } |
| 119 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 120 | id.set(0, std::floor(x_src)); |
| 121 | id.set(1, std::floor(y_src)); |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 122 | if(is_valid_pixel_index(x_src, y_src, width, height, border_size)) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 123 | { |
| 124 | out[element_idx] = bilinear_policy(in, id, x_src, y_src, border_mode, constant_border_value); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | if(border_mode == BorderMode::CONSTANT) |
| 129 | { |
| 130 | out[element_idx] = constant_border_value; |
| 131 | } |
| 132 | else if(border_mode == BorderMode::REPLICATE) |
| 133 | { |
Diego Lopez Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 134 | id.set(0, utility::clamp<int>(x_src, 0, width - 1)); |
| 135 | id.set(1, utility::clamp<int>(y_src, 0, height - 1)); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 136 | out[element_idx] = in[coord2index(in.shape(), id)]; |
| 137 | } |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | case InterpolationPolicy::AREA: |
| 142 | { |
| 143 | int x_from = std::floor(idx * wr - 0.5f - x_src); |
| 144 | int y_from = std::floor(idy * hr - 0.5f - y_src); |
| 145 | int x_to = std::ceil((idx + 1) * wr - 0.5f - x_src); |
| 146 | int y_to = std::ceil((idy + 1) * hr - 0.5f - y_src); |
| 147 | const int xi = std::floor(x_src); |
| 148 | const int yi = std::floor(y_src); |
| 149 | |
| 150 | // Clamp position to borders |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 151 | x_src = std::max(-static_cast<float>(border_size), std::min(x_src, static_cast<float>(width - 1 + border_size))); |
| 152 | y_src = std::max(-static_cast<float>(border_size), std::min(y_src, static_cast<float>(height - 1 + border_size))); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 153 | |
| 154 | // Clamp bounding box offsets to borders |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 155 | x_from = ((x_src + x_from) < -border_size) ? -border_size : x_from; |
| 156 | y_from = ((y_src + y_from) < -border_size) ? -border_size : y_from; |
| 157 | x_to = ((x_src + x_to) >= (width + border_size)) ? (width - 1 + border_size) : x_to; |
| 158 | y_to = ((y_src + y_to) >= (height + border_size)) ? (height - 1 + border_size) : y_to; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 159 | ARM_COMPUTE_ERROR_ON((x_to - x_from + 1) == 0 || (y_to - y_from + 1) == 0); |
| 160 | |
| 161 | float sum = 0; |
| 162 | for(int j = yi + y_from, je = yi + y_to; j <= je; ++j) |
| 163 | { |
| 164 | for(int i = xi + x_from, ie = xi + x_to; i <= ie; ++i) |
| 165 | { |
| 166 | id.set(0, static_cast<int>(i)); |
| 167 | id.set(1, static_cast<int>(j)); |
| 168 | sum += tensor_elem_at(in, id, border_mode, constant_border_value); |
| 169 | } |
| 170 | } |
| 171 | out[element_idx] = sum / ((x_to - x_from + 1) * (y_to - y_from + 1)); |
| 172 | |
| 173 | break; |
| 174 | } |
| 175 | default: |
| 176 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return out; |
| 181 | } |
| 182 | |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 183 | template <typename T> |
| 184 | SimpleTensor<T> scale(const SimpleTensor<T> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, |
| 185 | SamplingPolicy sampling_policy, bool ceil_policy_scale) |
| 186 | { |
| 187 | return scale_core<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale); |
| 188 | } |
| 189 | |
| 190 | template <> |
| 191 | SimpleTensor<uint8_t> scale(const SimpleTensor<uint8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value, |
| 192 | SamplingPolicy sampling_policy, bool ceil_policy_scale) |
| 193 | { |
| 194 | SimpleTensor<uint8_t> dst; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 195 | if(src.quantization_info().uniform().scale != 0.f) |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 196 | { |
| 197 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 198 | float constant_border_value_f = dequantize_qasymm8(constant_border_value, src.quantization_info()); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 199 | SimpleTensor<float> dst_tmp = scale_core<float>(src_tmp, scale_x, scale_y, policy, border_mode, constant_border_value_f, sampling_policy, ceil_policy_scale); |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 200 | dst = convert_to_asymmetric<uint8_t>(dst_tmp, src.quantization_info()); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
| 204 | dst = scale_core<uint8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale); |
| 205 | } |
| 206 | return dst; |
| 207 | } |
| 208 | |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 209 | template SimpleTensor<int16_t> scale(const SimpleTensor<int16_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int16_t constant_border_value, |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 210 | SamplingPolicy sampling_policy, bool ceil_policy_scale); |
| 211 | template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value, |
| 212 | SamplingPolicy sampling_policy, bool ceil_policy_scale); |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 213 | template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value, |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 214 | SamplingPolicy sampling_policy, bool ceil_policy_scale); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 215 | } // namespace reference |
| 216 | } // namespace validation |
| 217 | } // namespace test |
steniu01 | f81652d | 2017-09-11 15:29:12 +0100 | [diff] [blame] | 218 | } // namespace arm_compute |