Compute Library
 21.11
Scale.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
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 
25 #include "Scale.h"
26 
27 #include "Utils.h"
30 #include "support/Rounding.h"
31 
32 namespace arm_compute
33 {
34 namespace test
35 {
36 namespace validation
37 {
38 namespace reference
39 {
40 template <typename T>
41 SimpleTensor<T> scale_core(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
42  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
43 {
44  // Add 1 if ceil_policy_scale is true
45  const size_t round_value = ceil_policy_scale ? 1U : 0U;
47  shape_scaled.set(0, (in.shape()[0] + round_value) * scale_x, /* apply_dim_correction = */ false);
48  shape_scaled.set(1, (in.shape()[1] + round_value) * scale_y, /* apply_dim_correction = */ false);
50 
51  // Compute the ratio between source width/height and destination width/height
52  const auto wr = arm_compute::scale_utils::calculate_resize_ratio(in.shape()[0], out.shape()[0], align_corners);
53  const auto hr = arm_compute::scale_utils::calculate_resize_ratio(in.shape()[1], out.shape()[1], align_corners);
54 
55  const auto width = static_cast<int>(in.shape().x());
56  const auto height = static_cast<int>(in.shape().y());
57 
58  // Determine border size
59  const int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
60 
61  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
62  if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
63  {
65  }
66 
67  const uint32_t num_elements = out.num_elements();
68  for(uint32_t element_idx = 0, count = 0; element_idx < num_elements; ++element_idx, ++count)
69  {
70  Coordinates id = index2coord(out.shape(), element_idx);
71  int idx = id.x();
72  int idy = id.y();
73  float x_src = 0;
74  float y_src = 0;
75 
76  switch(policy)
77  {
79  {
80  switch(sampling_policy)
81  {
83  x_src = align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(idx * wr) : std::floor(idx * wr);
84  y_src = align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(idy * hr) : std::floor(idy * hr);
85  break;
87  //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords
88  x_src = (idx + 0.5f) * wr;
89  y_src = (idy + 0.5f) * hr;
90  break;
91  default:
92  ARM_COMPUTE_ERROR("Unsupported sampling policy.");
93  }
94 
95  id.set(0, x_src);
96  id.set(1, y_src);
97 
98  // If coordinates in range of tensor's width or height
99  if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
100  {
101  out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value);
102  }
103  break;
104  }
106  {
107  switch(sampling_policy)
108  {
110  x_src = idx * wr;
111  y_src = idy * hr;
112  break;
114  x_src = (idx + 0.5f) * wr - 0.5f;
115  y_src = (idy + 0.5f) * hr - 0.5f;
116  break;
117  default:
118  ARM_COMPUTE_ERROR("Unsupported sampling policy.");
119  }
120 
121  id.set(0, std::floor(x_src));
122  id.set(1, std::floor(y_src));
123  if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
124  {
125  out[element_idx] = bilinear_policy(in, id, x_src, y_src, border_mode, constant_border_value);
126  }
127  else
128  {
129  if(border_mode == BorderMode::CONSTANT)
130  {
131  out[element_idx] = constant_border_value;
132  }
133  else if(border_mode == BorderMode::REPLICATE)
134  {
135  id.set(0, utility::clamp<int>(x_src, 0, width - 1));
136  id.set(1, utility::clamp<int>(y_src, 0, height - 1));
137  out[element_idx] = in[coord2index(in.shape(), id)];
138  }
139  }
140  break;
141  }
143  {
144  int x_from = std::floor(idx * wr - 0.5f - x_src);
145  int y_from = std::floor(idy * hr - 0.5f - y_src);
146  int x_to = std::ceil((idx + 1) * wr - 0.5f - x_src);
147  int y_to = std::ceil((idy + 1) * hr - 0.5f - y_src);
148  const int xi = std::floor(x_src);
149  const int yi = std::floor(y_src);
150 
151  // Clamp position to borders
152  x_src = std::max(-static_cast<float>(border_size), std::min(x_src, static_cast<float>(width - 1 + border_size)));
153  y_src = std::max(-static_cast<float>(border_size), std::min(y_src, static_cast<float>(height - 1 + border_size)));
154 
155  // Clamp bounding box offsets to borders
156  x_from = ((x_src + x_from) < -border_size) ? -border_size : x_from;
157  y_from = ((y_src + y_from) < -border_size) ? -border_size : y_from;
158  x_to = ((x_src + x_to) >= (width + border_size)) ? (width - 1 + border_size) : x_to;
159  y_to = ((y_src + y_to) >= (height + border_size)) ? (height - 1 + border_size) : y_to;
160  ARM_COMPUTE_ERROR_ON((x_to - x_from + 1) == 0 || (y_to - y_from + 1) == 0);
161 
162  float sum = 0;
163  for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
164  {
165  for(int i = xi + x_from, ie = xi + x_to; i <= ie; ++i)
166  {
167  id.set(0, static_cast<int>(i));
168  id.set(1, static_cast<int>(j));
169  sum += tensor_elem_at(in, id, border_mode, constant_border_value);
170  }
171  }
172  out[element_idx] = sum / ((x_to - x_from + 1) * (y_to - y_from + 1));
173 
174  break;
175  }
176  default:
177  ARM_COMPUTE_ERROR("Unsupported interpolation mode");
178  }
179  }
180 
181  return out;
182 }
183 
184 template <typename T>
185 SimpleTensor<T> scale(const SimpleTensor<T> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
186  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
187 {
188  return scale_core<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
189 }
190 
191 template <>
192 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,
193  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
194 {
196  if(src.quantization_info().uniform().scale != 0.f)
197  {
199  float constant_border_value_f = dequantize_qasymm8(constant_border_value, src.quantization_info());
200  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, align_corners);
201  dst = convert_to_asymmetric<uint8_t>(dst_tmp, src.quantization_info());
202  }
203  else
204  {
205  dst = scale_core<uint8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
206  }
207  return dst;
208 }
209 
210 template <>
211 SimpleTensor<int8_t> scale(const SimpleTensor<int8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int8_t constant_border_value,
212  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
213 {
215  if(src.quantization_info().uniform().scale != 0.f)
216  {
218  float constant_border_value_f = dequantize_qasymm8_signed(constant_border_value, src.quantization_info());
219  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, align_corners);
220  dst = convert_to_asymmetric<int8_t>(dst_tmp, src.quantization_info());
221  }
222  else
223  {
224  dst = scale_core<int8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
225  }
226  return dst;
227 }
228 
229 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,
230  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
231 template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value,
232  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
233 template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value,
234  SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
235 } // namespace reference
236 } // namespace validation
237 } // namespace test
238 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:261
InterpolationPolicy
Interpolation method.
Definition: Types.h:398
T tensor_elem_at(const SimpleTensor< T > &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
Definition: Utils.h:63
Shape of a tensor.
Definition: TensorShape.h:39
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
TensorShape shape_scaled(shape)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
half_float::half half
16-bit floating point type
Definition: Types.h:48
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
Output values are defined by bilinear interpolation between the pixels.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:36
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
T round_half_away_from_zero(T value)
Round floating-point value with half value rounding away from zero.
Definition: Rounding.h:106
Output values are defined to match the source pixel whose center is nearest to the sample position...
SimpleTensor< T > scale_core(const SimpleTensor< T > &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Definition: Scale.cpp:41
bool is_valid_pixel_index(int x, int y, int width, int height, int border_size)
Checks if a pixel has valid coordinates.
Definition: Utils.h:53
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:387
Samples are taken at pixel center.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
Coordinates of an item.
Definition: Coordinates.h:37
T bilinear_policy(const SimpleTensor< T > &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value)
Definition: Utils.cpp:36
UniformQuantizationInfo uniform() const
Return per layer quantization info.
Samples are taken at pixel top left corner.
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:358
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
Borders are left undefined.
Pixels outside the image are assumed to have the same value as the closest image pixel.
float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo)
Dequantize a value given a signed 8-bit asymmetric quantization scheme.
float calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners=false)
Returns resize ratio between input and output with consideration of aligned corners.
Definition: ScaleUtils.cpp:27
SimpleTensor< T > scale(const SimpleTensor< T > &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Definition: Scale.cpp:185
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
SamplingPolicy
Available Sampling Policies.
Definition: Types.h:104