blob: 0d4a86e372faa9890c77f51f28ad2f9eff461d77 [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
Diego Lopez Recasff860cf2018-02-22 13:08:01 +00002 * Copyright (c) 2017-2018 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#include "arm_compute/core/Helpers.h"
25#include "arm_compute/core/Types.h"
26#include "arm_compute/runtime/NEON/functions/NEScale.h"
27#include "arm_compute/runtime/Tensor.h"
28#include "arm_compute/runtime/TensorAllocator.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010029#include "tests/NEON/Accessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/datasets/BorderModeDataset.h"
32#include "tests/datasets/InterpolationPolicyDataset.h"
Daniil Efremov02bf80d2017-11-22 00:26:51 +070033#include "tests/datasets/SamplingPolicyDataset.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/datasets/ShapeDatasets.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Macros.h"
37#include "tests/framework/datasets/Datasets.h"
38#include "tests/validation/Helpers.h"
39#include "tests/validation/Validation.h"
40#include "tests/validation/fixtures/ScaleFixture.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010041
42namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
Isabella Gottardi732f3682017-09-05 14:10:12 +010048namespace
49{
Georgios Pinitas583137c2017-08-31 18:12:42 +010050/** Scale data types */
51const auto ScaleDataTypes = framework::dataset::make("DataType",
52{
53 DataType::U8,
54 DataType::S16,
55 DataType::F32,
56});
57
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010058/** Scale data types */
59const auto ScaleDataLayouts = framework::dataset::make("DataLayout",
60{
61 DataLayout::NCHW,
62 DataLayout::NHWC,
63});
64
Georgios Pinitas583137c2017-08-31 18:12:42 +010065/** Tolerance */
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +010066constexpr AbsoluteTolerance<uint8_t> tolerance_u8(1);
67constexpr AbsoluteTolerance<int16_t> tolerance_s16(1);
Georgios Pinitas583137c2017-08-31 18:12:42 +010068RelativeTolerance<float> tolerance_f32(0.01);
Moritz Pflanzerff1c3602017-09-22 12:41:25 +010069
70constexpr float tolerance_num_s16 = 0.01f;
71constexpr float tolerance_num_f32 = 0.01f;
Isabella Gottardi732f3682017-09-05 14:10:12 +010072} // namespace
73
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010074TEST_SUITE(NEON)
75TEST_SUITE(Scale)
76
Georgios Pinitas20b43132018-05-14 16:05:23 +010077// *INDENT-OFF*
78// clang-format off
79DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010080 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8), // Mismatching data type
81 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Unsupported sampling point
82 TensorInfo(TensorShape(4U, 27U, 13U), 1, DataType::F32), // Invalid policy
83 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Insufficient padding
84 TensorInfo(TensorShape(4U, 27U, 13U), 1, DataType::F32),
Georgios Pinitas20b43132018-05-14 16:05:23 +010085 }),
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010086 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(132U, 25U, 2U), 1, DataType::F32),
87 TensorInfo(TensorShape(132U, 25U, 2U), 1, DataType::F32),
88 TensorInfo(TensorShape(4U, 132U, 25U), 1, DataType::F32),
89 TensorInfo(TensorShape(132U, 25U, 2U), 1, DataType::F32),
90 TensorInfo(TensorShape(4U, 132U, 25U), 1, DataType::F32),
Georgios Pinitas20b43132018-05-14 16:05:23 +010091 })),
92 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR,
93 InterpolationPolicy::NEAREST_NEIGHBOR,
94 InterpolationPolicy::AREA,
95 InterpolationPolicy::AREA,
96 InterpolationPolicy::NEAREST_NEIGHBOR,
97 })),
98 framework::dataset::make("BorderMode", { BorderMode::UNDEFINED,
99 BorderMode::UNDEFINED,
100 BorderMode::UNDEFINED,
101 BorderMode::UNDEFINED,
102 BorderMode::REPLICATE,
103 })),
104 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER,
105 SamplingPolicy::TOP_LEFT,
106 SamplingPolicy::CENTER,
107 SamplingPolicy::CENTER,
108 SamplingPolicy::CENTER,
109 })),
110 framework::dataset::make("DataLayout", { DataLayout::NCHW,
111 DataLayout::NCHW,
112 DataLayout::NHWC,
113 DataLayout::NCHW,
114 DataLayout::NHWC,
115 })),
116 framework::dataset::make("Expected", { false, false, false, false ,true })),
117 input_info, output_info, policy,border_mode, sampling_policy, data_layout, expected)
118{
119 const PixelValue constant_border(5);
120 Status status = NEScale::validate(&input_info.clone()->set_is_resizable(false).set_data_layout(data_layout),
121 &output_info.clone()->set_is_resizable(false).set_data_layout(data_layout),
122 policy, border_mode, constant_border, sampling_policy);
123 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
124}
125// clang-format on
126// *INDENT-ON*
127
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100128DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), ScaleDataTypes), ScaleDataLayouts),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700129 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
130 datasets::BorderModes()),
131 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100132 shape, data_type, data_layout, policy, border_mode, sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100133{
134 std::mt19937 generator(library->seed());
135 std::uniform_real_distribution<float> distribution_float(0.25, 2);
136 const float scale_x = distribution_float(generator);
137 const float scale_y = distribution_float(generator);
138 uint8_t constant_border_value = 0;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100139 TensorShape src_shape = shape;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100140 if(border_mode == BorderMode::CONSTANT)
141 {
142 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
143 constant_border_value = distribution_u8(generator);
144 }
145
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100146 // Get width/height indices depending on layout
147 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
148 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
149
150 // Change shape in case of NHWC.
151 if(data_layout == DataLayout::NHWC)
152 {
153 permute(src_shape, PermutationVector(2U, 0U, 1U));
154 }
155
156 // Calculate scaled shape
157 TensorShape shape_scaled(src_shape);
158 shape_scaled.set(idx_width, src_shape[idx_width] * scale_x);
159 shape_scaled.set(idx_height, src_shape[idx_height] * scale_y);
160
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100161 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100162 Tensor src = create_tensor<Tensor>(src_shape, data_type, 1, QuantizationInfo(), data_layout);
163 Tensor dst = create_tensor<Tensor>(shape_scaled, data_type, 1, QuantizationInfo(), data_layout);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100164
165 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
166 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
167
168 // Create and configure function
169 NEScale nescale;
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700170 nescale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100171
172 // Validate valid region
Diego Lopez Recas00854292018-02-22 13:08:01 +0000173 const ValidRegion dst_valid_region = calculate_valid_region_scale(*(src.info()), shape_scaled, policy, sampling_policy, (border_mode == BorderMode::UNDEFINED));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100174 validate(dst.info()->valid_region(), dst_valid_region);
175
176 // Validate padding
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100177 int num_elements_processed_x = 16;
178 if(data_layout == DataLayout::NHWC)
179 {
180 num_elements_processed_x = (policy == InterpolationPolicy::BILINEAR) ? 1 : 16 / src.info()->element_size();
181 }
182 PaddingCalculator calculator(shape_scaled.x(), num_elements_processed_x);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100183 calculator.set_border_mode(border_mode);
184
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100185 PaddingSize read_padding(1);
186 if(data_layout == DataLayout::NHWC)
187 {
188 read_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
189 if(border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR)
190 {
191 read_padding.top = 1;
192 }
193 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100194 const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
195 validate(src.info()->padding(), read_padding);
196 validate(dst.info()->padding(), write_padding);
197}
198
199template <typename T>
200using NEScaleFixture = ScaleValidationFixture<Tensor, Accessor, NEScale, T>;
201
Georgios Pinitas583137c2017-08-31 18:12:42 +0100202TEST_SUITE(Float)
203TEST_SUITE(FP32)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100204FIXTURE_DATA_TEST_CASE(RunSmall, NEScaleFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700205 DataType::F32)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100206 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700207 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
208 datasets::BorderModes()),
209 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100210{
211 //Create valid region
212 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000213 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Georgios Pinitas583137c2017-08-31 18:12:42 +0100214
215 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100216 validate(Accessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100217}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100218FIXTURE_DATA_TEST_CASE(RunLarge, NEScaleFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType",
Georgios Pinitas583137c2017-08-31 18:12:42 +0100219 DataType::F32)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100220 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700221 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
222 datasets::BorderModes()),
223 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100224{
225 //Create valid region
226 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000227 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Georgios Pinitas583137c2017-08-31 18:12:42 +0100228
229 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100230 validate(Accessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100231}
232TEST_SUITE_END()
233TEST_SUITE_END()
234
235TEST_SUITE(Integer)
236TEST_SUITE(U8)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100237FIXTURE_DATA_TEST_CASE(RunSmall, NEScaleFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700238 DataType::U8)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100239 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700240 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
241 datasets::BorderModes()),
242 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100243{
244 //Create valid region
245 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000246 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100247
248 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100249 validate(Accessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100250}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100251FIXTURE_DATA_TEST_CASE(RunLarge, NEScaleFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType",
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100252 DataType::U8)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100253 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700254 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
255 datasets::BorderModes()),
256 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100257{
258 //Create valid region
259 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000260 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100261
262 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100263 validate(Accessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100264}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100265TEST_SUITE_END()
266TEST_SUITE(S16)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100267FIXTURE_DATA_TEST_CASE(RunSmall, NEScaleFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700268 DataType::S16)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100269 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700270 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
271 datasets::BorderModes()),
272 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100273{
274 //Create valid region
275 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000276 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Georgios Pinitas583137c2017-08-31 18:12:42 +0100277
278 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100279 validate(Accessor(_target), _reference, valid_region, tolerance_s16, tolerance_num_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100280}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100281FIXTURE_DATA_TEST_CASE(RunLarge, NEScaleFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType",
Georgios Pinitas583137c2017-08-31 18:12:42 +0100282 DataType::S16)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100283 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700284 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
285 datasets::BorderModes()),
286 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100287{
288 //Create valid region
289 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000290 ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, _sampling_policy, (_border_mode == BorderMode::UNDEFINED));
Georgios Pinitas583137c2017-08-31 18:12:42 +0100291
292 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100293 validate(Accessor(_target), _reference, valid_region, tolerance_s16, tolerance_num_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100294}
295TEST_SUITE_END()
296TEST_SUITE_END()
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100297
298TEST_SUITE_END()
299TEST_SUITE_END()
300} // namespace validation
301} // namespace test
302} // namespace arm_compute