blob: 71e98fd7768b48e6210c0806b97ddf1102d5a286 [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Isabella Gottardi1fab09f2017-07-13 15:55:57 +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
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010025#include "Scale.h"
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010026
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010027#include "Utils.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000028#include "arm_compute/core/utils/misc/Utility.h"
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010029#include "src/core/utils/ScaleUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "support/Rounding.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010031
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40template <typename T>
Michalis Spyrou17220e22018-09-12 13:35:38 +010041SimpleTensor<T> scale_core(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000042 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010043{
Gian Marco37908d92017-11-07 14:38:22 +000044 // Add 1 if ceil_policy_scale is true
45 const size_t round_value = ceil_policy_scale ? 1U : 0U;
46 TensorShape shape_scaled(in.shape());
Sang-Hoon Park9e4b8992020-06-22 13:48:56 +010047 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);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010049 SimpleTensor<T> out(shape_scaled, in.data_type());
50
51 // Compute the ratio between source width/height and destination width/height
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010052 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);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010054
55 const auto width = static_cast<int>(in.shape().x());
56 const auto height = static_cast<int>(in.shape().y());
57
Daniil Efremov7a49c792017-11-14 21:25:34 +070058 // Determine border size
59 const int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
60
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010061 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
62 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
63 {
64 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
65 }
66
Michalis Spyroufae513c2019-10-16 17:41:33 +010067 const uint32_t num_elements = out.num_elements();
68 for(uint32_t element_idx = 0, count = 0; element_idx < num_elements; ++element_idx, ++count)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010069 {
70 Coordinates id = index2coord(out.shape(), element_idx);
71 int idx = id.x();
72 int idy = id.y();
Daniil Efremov02bf80d2017-11-22 00:26:51 +070073 float x_src = 0;
74 float y_src = 0;
75
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010076 switch(policy)
77 {
78 case InterpolationPolicy::NEAREST_NEIGHBOR:
79 {
Michalis Spyroud4733862019-07-09 14:21:06 +010080 switch(sampling_policy)
81 {
82 case SamplingPolicy::TOP_LEFT:
Sang-Hoon Park94d50512020-07-02 10:49:39 +010083 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);
Michalis Spyroud4733862019-07-09 14:21:06 +010085 break;
86 case SamplingPolicy::CENTER:
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
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010095 id.set(0, x_src);
96 id.set(1, y_src);
97
98 // If coordinates in range of tensor's width or height
Daniil Efremov7a49c792017-11-14 21:25:34 +070099 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100100 {
101 out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value);
102 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100103 break;
104 }
105 case InterpolationPolicy::BILINEAR:
106 {
Michalis Spyroud4733862019-07-09 14:21:06 +0100107 switch(sampling_policy)
108 {
109 case SamplingPolicy::TOP_LEFT:
110 x_src = idx * wr;
111 y_src = idy * hr;
112 break;
113 case SamplingPolicy::CENTER:
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
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100121 id.set(0, std::floor(x_src));
122 id.set(1, std::floor(y_src));
Daniil Efremov7a49c792017-11-14 21:25:34 +0700123 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100124 {
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 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000135 id.set(0, utility::clamp<int>(x_src, 0, width - 1));
136 id.set(1, utility::clamp<int>(y_src, 0, height - 1));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100137 out[element_idx] = in[coord2index(in.shape(), id)];
138 }
139 }
140 break;
141 }
142 case InterpolationPolicy::AREA:
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
Daniil Efremov7a49c792017-11-14 21:25:34 +0700152 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)));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100154
155 // Clamp bounding box offsets to borders
Daniil Efremov7a49c792017-11-14 21:25:34 +0700156 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;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100160 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
Michalis Spyrou17220e22018-09-12 13:35:38 +0100184template <typename T>
185SimpleTensor<T> scale(const SimpleTensor<T> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000186 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100187{
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000188 return scale_core<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100189}
190
191template <>
192SimpleTensor<uint8_t> scale(const SimpleTensor<uint8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000193 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100194{
195 SimpleTensor<uint8_t> dst;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100196 if(src.quantization_info().uniform().scale != 0.f)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100197 {
198 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100199 float constant_border_value_f = dequantize_qasymm8(constant_border_value, src.quantization_info());
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000200 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);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100201 dst = convert_to_asymmetric<uint8_t>(dst_tmp, src.quantization_info());
Michalis Spyrou17220e22018-09-12 13:35:38 +0100202 }
203 else
204 {
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000205 dst = scale_core<uint8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100206 }
207 return dst;
208}
209
Manuel Bottini8481d832019-12-10 15:28:40 +0000210template <>
211SimpleTensor<int8_t> scale(const SimpleTensor<int8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int8_t constant_border_value,
morgolockdceacdc2020-01-09 15:16:05 +0000212 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Manuel Bottini8481d832019-12-10 15:28:40 +0000213{
214 SimpleTensor<int8_t> dst;
215 if(src.quantization_info().uniform().scale != 0.f)
216 {
217 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
218 float constant_border_value_f = dequantize_qasymm8_signed(constant_border_value, src.quantization_info());
morgolockdceacdc2020-01-09 15:16:05 +0000219 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);
Manuel Bottini8481d832019-12-10 15:28:40 +0000220 dst = convert_to_asymmetric<int8_t>(dst_tmp, src.quantization_info());
221 }
222 else
223 {
morgolockdceacdc2020-01-09 15:16:05 +0000224 dst = scale_core<int8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
Manuel Bottini8481d832019-12-10 15:28:40 +0000225 }
226 return dst;
227}
228
Gian Marco37908d92017-11-07 14:38:22 +0000229template 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,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000230 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700231template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000232 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
Gian Marco37908d92017-11-07 14:38:22 +0000233template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000234 SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100235} // namespace reference
236} // namespace validation
237} // namespace test
steniu01f81652d2017-09-11 15:29:12 +0100238} // namespace arm_compute