blob: b21affd9d3282aa46c9ba519e60a57eab7f12dbe [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 Pinitas393fa4c2018-05-08 15:54:53 +010077DATA_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 +070078 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
79 datasets::BorderModes()),
80 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010081 shape, data_type, data_layout, policy, border_mode, sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010082{
83 std::mt19937 generator(library->seed());
84 std::uniform_real_distribution<float> distribution_float(0.25, 2);
85 const float scale_x = distribution_float(generator);
86 const float scale_y = distribution_float(generator);
87 uint8_t constant_border_value = 0;
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010088 TensorShape src_shape = shape;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010089 if(border_mode == BorderMode::CONSTANT)
90 {
91 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
92 constant_border_value = distribution_u8(generator);
93 }
94
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010095 // Get width/height indices depending on layout
96 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
97 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
98
99 // Change shape in case of NHWC.
100 if(data_layout == DataLayout::NHWC)
101 {
102 permute(src_shape, PermutationVector(2U, 0U, 1U));
103 }
104
105 // Calculate scaled shape
106 TensorShape shape_scaled(src_shape);
107 shape_scaled.set(idx_width, src_shape[idx_width] * scale_x);
108 shape_scaled.set(idx_height, src_shape[idx_height] * scale_y);
109
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100110 // Create tensors
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100111 Tensor src = create_tensor<Tensor>(src_shape, data_type, 1, 0, QuantizationInfo(), data_layout);
112 Tensor dst = create_tensor<Tensor>(shape_scaled, data_type, 1, 0, QuantizationInfo(), data_layout);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100113
114 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
116
117 // Create and configure function
118 NEScale nescale;
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700119 nescale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100120
121 // Validate valid region
Diego Lopez Recas00854292018-02-22 13:08:01 +0000122 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 +0100123 validate(dst.info()->valid_region(), dst_valid_region);
124
125 // Validate padding
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100126 int num_elements_processed_x = 16;
127 if(data_layout == DataLayout::NHWC)
128 {
129 num_elements_processed_x = (policy == InterpolationPolicy::BILINEAR) ? 1 : 16 / src.info()->element_size();
130 }
131 PaddingCalculator calculator(shape_scaled.x(), num_elements_processed_x);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100132 calculator.set_border_mode(border_mode);
133
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100134 PaddingSize read_padding(1);
135 if(data_layout == DataLayout::NHWC)
136 {
137 read_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
138 if(border_mode == BorderMode::CONSTANT && policy == InterpolationPolicy::BILINEAR)
139 {
140 read_padding.top = 1;
141 }
142 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100143 const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
144 validate(src.info()->padding(), read_padding);
145 validate(dst.info()->padding(), write_padding);
146}
147
148template <typename T>
149using NEScaleFixture = ScaleValidationFixture<Tensor, Accessor, NEScale, T>;
150
Georgios Pinitas583137c2017-08-31 18:12:42 +0100151TEST_SUITE(Float)
152TEST_SUITE(FP32)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100153FIXTURE_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 +0700154 DataType::F32)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100155 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700156 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
157 datasets::BorderModes()),
158 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100159{
160 //Create valid region
161 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000162 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 +0100163
164 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100165 validate(Accessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100166}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100167FIXTURE_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 +0100168 DataType::F32)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100169 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700170 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
171 datasets::BorderModes()),
172 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100173{
174 //Create valid region
175 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000176 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 +0100177
178 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100179 validate(Accessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100180}
181TEST_SUITE_END()
182TEST_SUITE_END()
183
184TEST_SUITE(Integer)
185TEST_SUITE(U8)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100186FIXTURE_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 +0700187 DataType::U8)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100188 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700189 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
190 datasets::BorderModes()),
191 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100192{
193 //Create valid region
194 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000195 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 +0100196
197 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100198 validate(Accessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100199}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100200FIXTURE_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 +0100201 DataType::U8)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100202 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700203 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
204 datasets::BorderModes()),
205 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100206{
207 //Create valid region
208 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000209 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 +0100210
211 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100212 validate(Accessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100213}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100214TEST_SUITE_END()
215TEST_SUITE(S16)
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100216FIXTURE_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 +0700217 DataType::S16)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100218 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700219 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
220 datasets::BorderModes()),
221 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100222{
223 //Create valid region
224 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000225 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 +0100226
227 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100228 validate(Accessor(_target), _reference, valid_region, tolerance_s16, tolerance_num_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100229}
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100230FIXTURE_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 +0100231 DataType::S16)),
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100232 framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC })),
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700233 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
234 datasets::BorderModes()),
235 framework::dataset::make("SamplingPolicy", { SamplingPolicy::CENTER })))
Georgios Pinitas583137c2017-08-31 18:12:42 +0100236{
237 //Create valid region
238 TensorInfo src_info(_shape, 1, _data_type);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000239 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 +0100240
241 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100242 validate(Accessor(_target), _reference, valid_region, tolerance_s16, tolerance_num_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100243}
244TEST_SUITE_END()
245TEST_SUITE_END()
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100246
247TEST_SUITE_END()
248TEST_SUITE_END()
249} // namespace validation
250} // namespace test
251} // namespace arm_compute