blob: fe24f5bac5f2e95091acec3a83a4b68510434bd1 [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
2 * Copyright (c) 2017 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#ifndef ARM_COMPUTE_TEST_SCALE_FIXTURE
25#define ARM_COMPUTE_TEST_SCALE_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000034#include "tests/validation/reference/Scale.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010035
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
43class ScaleValidationFixture : public framework::Fixture
44{
45public:
46 template <typename...>
Daniil Efremov02bf80d2017-11-22 00:26:51 +070047 void setup(TensorShape shape, DataType data_type, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010048 {
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010049 constexpr float max_width = 8192.0f;
50 constexpr float max_height = 6384.0f;
51
Daniil Efremov02bf80d2017-11-22 00:26:51 +070052 _shape = shape;
53 _policy = policy;
54 _border_mode = border_mode;
55 _sampling_policy = sampling_policy;
56 _data_type = data_type;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010057
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010058 std::mt19937 generator(library->seed());
59 std::uniform_real_distribution<float> distribution_float(0.25, 3);
60 float scale_x = distribution_float(generator);
61 float scale_y = distribution_float(generator);
62
63 scale_x = ((shape.x() * scale_x) > max_width) ? (max_width / shape.x()) : scale_x;
64 scale_y = ((shape.y() * scale_y) > max_height) ? (max_height / shape.y()) : scale_y;
65
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010066 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
Georgios Pinitas583137c2017-08-31 18:12:42 +010067 T constant_border_value = static_cast<T>(distribution_u8(generator));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010068
Daniil Efremov02bf80d2017-11-22 00:26:51 +070069 _target = compute_target(shape, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy);
70 _reference = compute_reference(shape, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010071 }
72
73protected:
74 template <typename U>
75 void fill(U &&tensor)
76 {
77 library->fill_tensor_uniform(tensor, 0);
78 }
79
80 TensorType compute_target(const TensorShape &shape, const float scale_x, const float scale_y,
Daniil Efremov02bf80d2017-11-22 00:26:51 +070081 InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010082 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(shape, _data_type);
85 TensorShape shape_scaled(shape);
86 shape_scaled.set(0, shape[0] * scale_x);
87 shape_scaled.set(1, shape[1] * scale_y);
88 TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type);
89
90 // Create and configure function
91 FunctionType scale;
92
Daniil Efremov02bf80d2017-11-22 00:26:51 +070093 scale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010094
95 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
98 // Allocate tensors
99 src.allocator()->allocate();
100 dst.allocator()->allocate();
101 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
103
104 // Fill tensors
105 fill(AccessorType(src));
106
107 // Compute function
108 scale.run();
109
110 return dst;
111 }
112
113 SimpleTensor<T> compute_reference(const TensorShape &shape, const float scale_x, const float scale_y,
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700114 InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100115 {
116 // Create reference
117 SimpleTensor<T> src{ shape, _data_type };
118
119 // Fill reference
120 fill(src);
121
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700122 return reference::scale<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100123 }
124
125 TensorType _target{};
126 SimpleTensor<T> _reference{};
127 TensorShape _shape{};
128 InterpolationPolicy _policy{};
129 BorderMode _border_mode{};
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700130 SamplingPolicy _sampling_policy{};
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100131 DataType _data_type{};
132};
133} // namespace validation
134} // namespace test
135} // namespace arm_compute
steniu01f81652d2017-09-11 15:29:12 +0100136#endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */