blob: 5f2c8659505c62ddd444e2d45388a53faf7022ff [file] [log] [blame]
zhenglin0fb6cf52017-12-12 15:56:09 +08001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
zhenglin0fb6cf52017-12-12 15:56:09 +08003 *
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_NORMALIZE_PLANAR_YUV_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_NORMALIZE_PLANAR_YUV_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/Helpers.h"
35#include "tests/validation/reference/NormalizePlanarYUVLayer.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010044class NormalizePlanarYUVLayerValidationGenericFixture : public framework::Fixture
zhenglin0fb6cf52017-12-12 15:56:09 +080045{
46public:
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010047 void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info)
zhenglin0fb6cf52017-12-12 15:56:09 +080048 {
49 _data_type = dt;
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010050 _target = compute_target(shape0, shape1, dt, data_layout, quantization_info);
51 _reference = compute_reference(shape0, shape1, dt, quantization_info);
zhenglin0fb6cf52017-12-12 15:56:09 +080052 }
53
54protected:
55 template <typename U>
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010056 void fill(U &&src_tensor, U &&mean_tensor, U &&std_tensor)
zhenglin0fb6cf52017-12-12 15:56:09 +080057 {
Giorgio Arena33b103b2021-01-08 10:37:15 +000058 using FloatDistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<float>>::type;
Giorgio Arena4bdd1772020-12-17 16:47:07 +000059
zhenglin0fb6cf52017-12-12 15:56:09 +080060 if(is_data_type_float(_data_type))
61 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +000062 const T min_bound = T(-1.f);
63 const T max_bound = T(1.f);
64 FloatDistributionType distribution(min_bound, max_bound);
65 FloatDistributionType distribution_std(T(0.1f), max_bound);
zhenglin0fb6cf52017-12-12 15:56:09 +080066 library->fill(src_tensor, distribution, 0);
67 library->fill(mean_tensor, distribution, 1);
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010068 library->fill(std_tensor, distribution_std, 2);
zhenglin0fb6cf52017-12-12 15:56:09 +080069 }
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010070 else if(is_data_type_quantized_asymmetric(_data_type))
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010071 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010072 const QuantizationInfo quant_info = src_tensor.quantization_info();
73 std::pair<int, int> bounds = get_quantized_bounds(quant_info, -1.f, 1.0f);
74 std::uniform_int_distribution<> distribution(bounds.first, bounds.second);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010075 std::uniform_int_distribution<> distribution_std(quantize_qasymm8(0.1f, quant_info.uniform()), bounds.second);
Michele Di Giorgio3f6a57a2018-09-26 14:39:39 +010076 library->fill(src_tensor, distribution, 0);
77 library->fill(mean_tensor, distribution, 1);
78 library->fill(std_tensor, distribution_std, 2);
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010079 }
zhenglin0fb6cf52017-12-12 15:56:09 +080080 }
81
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010082 TensorType compute_target(TensorShape shape0, const TensorShape &shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info)
zhenglin0fb6cf52017-12-12 15:56:09 +080083 {
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010084 if(data_layout == DataLayout::NHWC)
85 {
86 permute(shape0, PermutationVector(2U, 0U, 1U));
87 }
88
zhenglin0fb6cf52017-12-12 15:56:09 +080089 // Create tensors
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +010090 TensorType src = create_tensor<TensorType>(shape0, dt, 1, quantization_info, data_layout);
91 TensorType mean = create_tensor<TensorType>(shape1, dt, 1, quantization_info);
92 TensorType std = create_tensor<TensorType>(shape1, dt, 1, quantization_info);
93 TensorType dst;
zhenglin0fb6cf52017-12-12 15:56:09 +080094
95 // Create and configure function
96 FunctionType norm;
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +010097 norm.configure(&src, &dst, &mean, &std);
zhenglin0fb6cf52017-12-12 15:56:09 +080098
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010099 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
100 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
101 ARM_COMPUTE_ASSERT(mean.info()->is_resizable());
102 ARM_COMPUTE_ASSERT(std.info()->is_resizable());
zhenglin0fb6cf52017-12-12 15:56:09 +0800103
104 // Allocate tensors
105 src.allocator()->allocate();
106 dst.allocator()->allocate();
107 mean.allocator()->allocate();
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100108 std.allocator()->allocate();
zhenglin0fb6cf52017-12-12 15:56:09 +0800109
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100110 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
111 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
112 ARM_COMPUTE_ASSERT(!mean.info()->is_resizable());
113 ARM_COMPUTE_ASSERT(!std.info()->is_resizable());
zhenglin0fb6cf52017-12-12 15:56:09 +0800114
115 // Fill tensors
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100116 fill(AccessorType(src), AccessorType(mean), AccessorType(std));
zhenglin0fb6cf52017-12-12 15:56:09 +0800117
118 // Compute function
119 norm.run();
120
121 return dst;
122 }
123
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100124 SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, DataType dt, QuantizationInfo quantization_info)
zhenglin0fb6cf52017-12-12 15:56:09 +0800125 {
126 // Create reference
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100127 SimpleTensor<T> ref_src{ shape0, dt, 1, quantization_info };
128 SimpleTensor<T> ref_mean{ shape1, dt, 1, quantization_info };
129 SimpleTensor<T> ref_std{ shape1, dt, 1, quantization_info };
zhenglin0fb6cf52017-12-12 15:56:09 +0800130
131 // Fill reference
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100132 fill(ref_src, ref_mean, ref_std);
zhenglin0fb6cf52017-12-12 15:56:09 +0800133
Michele Di Giorgiob57be0d2018-08-31 16:26:25 +0100134 return reference::normalize_planar_yuv_layer(ref_src, ref_mean, ref_std);
zhenglin0fb6cf52017-12-12 15:56:09 +0800135 }
136
137 TensorType _target{};
138 SimpleTensor<T> _reference{};
139 DataType _data_type{};
140};
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100141
142template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
143class NormalizePlanarYUVLayerValidationFixture : public NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
144{
145public:
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100146 void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout)
147 {
148 NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, dt, data_layout, QuantizationInfo());
149 }
150};
151
152template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
153class NormalizePlanarYUVLayerValidationQuantizedFixture : public NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
154{
155public:
Michele Di Giorgiod63dfa22018-09-12 10:18:54 +0100156 void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info)
157 {
158 NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, dt, data_layout, quantization_info);
159 }
160};
zhenglin0fb6cf52017-12-12 15:56:09 +0800161} // namespace validation
162} // namespace test
163} // namespace arm_compute
164#endif /* ARM_COMPUTE_TEST_NORMALIZE_PLANAR_YUV_LAYER_FIXTURE */