zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 1 | /* |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 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_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 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 44 | class NormalizePlanarYUVLayerValidationGenericFixture : public framework::Fixture |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | template <typename...> |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 48 | void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info) |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 49 | { |
| 50 | _data_type = dt; |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 51 | _target = compute_target(shape0, shape1, dt, data_layout, quantization_info); |
| 52 | _reference = compute_reference(shape0, shape1, dt, quantization_info); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | template <typename U> |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 57 | void fill(U &&src_tensor, U &&mean_tensor, U &&std_tensor) |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 58 | { |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 59 | 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 Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 60 | |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 61 | if(is_data_type_float(_data_type)) |
| 62 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 63 | const T min_bound = T(-1.f); |
| 64 | const T max_bound = T(1.f); |
| 65 | FloatDistributionType distribution(min_bound, max_bound); |
| 66 | FloatDistributionType distribution_std(T(0.1f), max_bound); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 67 | library->fill(src_tensor, distribution, 0); |
| 68 | library->fill(mean_tensor, distribution, 1); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 69 | library->fill(std_tensor, distribution_std, 2); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 70 | } |
Michele Di Giorgio | 3f6a57a | 2018-09-26 14:39:39 +0100 | [diff] [blame] | 71 | else if(is_data_type_quantized_asymmetric(_data_type)) |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 72 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 73 | const QuantizationInfo quant_info = src_tensor.quantization_info(); |
| 74 | std::pair<int, int> bounds = get_quantized_bounds(quant_info, -1.f, 1.0f); |
| 75 | std::uniform_int_distribution<> distribution(bounds.first, bounds.second); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 76 | std::uniform_int_distribution<> distribution_std(quantize_qasymm8(0.1f, quant_info.uniform()), bounds.second); |
Michele Di Giorgio | 3f6a57a | 2018-09-26 14:39:39 +0100 | [diff] [blame] | 77 | library->fill(src_tensor, distribution, 0); |
| 78 | library->fill(mean_tensor, distribution, 1); |
| 79 | library->fill(std_tensor, distribution_std, 2); |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 80 | } |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 81 | } |
| 82 | |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 83 | TensorType compute_target(TensorShape shape0, const TensorShape &shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info) |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 84 | { |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 85 | if(data_layout == DataLayout::NHWC) |
| 86 | { |
| 87 | permute(shape0, PermutationVector(2U, 0U, 1U)); |
| 88 | } |
| 89 | |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 90 | // Create tensors |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 91 | TensorType src = create_tensor<TensorType>(shape0, dt, 1, quantization_info, data_layout); |
| 92 | TensorType mean = create_tensor<TensorType>(shape1, dt, 1, quantization_info); |
| 93 | TensorType std = create_tensor<TensorType>(shape1, dt, 1, quantization_info); |
| 94 | TensorType dst; |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 95 | |
| 96 | // Create and configure function |
| 97 | FunctionType norm; |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 98 | norm.configure(&src, &dst, &mean, &std); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 99 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame^] | 100 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 101 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
| 102 | ARM_COMPUTE_ASSERT(mean.info()->is_resizable()); |
| 103 | ARM_COMPUTE_ASSERT(std.info()->is_resizable()); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 104 | |
| 105 | // Allocate tensors |
| 106 | src.allocator()->allocate(); |
| 107 | dst.allocator()->allocate(); |
| 108 | mean.allocator()->allocate(); |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 109 | std.allocator()->allocate(); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 110 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame^] | 111 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 112 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
| 113 | ARM_COMPUTE_ASSERT(!mean.info()->is_resizable()); |
| 114 | ARM_COMPUTE_ASSERT(!std.info()->is_resizable()); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 115 | |
| 116 | // Fill tensors |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 117 | fill(AccessorType(src), AccessorType(mean), AccessorType(std)); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 118 | |
| 119 | // Compute function |
| 120 | norm.run(); |
| 121 | |
| 122 | return dst; |
| 123 | } |
| 124 | |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 125 | SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, DataType dt, QuantizationInfo quantization_info) |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 126 | { |
| 127 | // Create reference |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 128 | SimpleTensor<T> ref_src{ shape0, dt, 1, quantization_info }; |
| 129 | SimpleTensor<T> ref_mean{ shape1, dt, 1, quantization_info }; |
| 130 | SimpleTensor<T> ref_std{ shape1, dt, 1, quantization_info }; |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 131 | |
| 132 | // Fill reference |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 133 | fill(ref_src, ref_mean, ref_std); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 134 | |
Michele Di Giorgio | b57be0d | 2018-08-31 16:26:25 +0100 | [diff] [blame] | 135 | return reference::normalize_planar_yuv_layer(ref_src, ref_mean, ref_std); |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | TensorType _target{}; |
| 139 | SimpleTensor<T> _reference{}; |
| 140 | DataType _data_type{}; |
| 141 | }; |
Michele Di Giorgio | d63dfa2 | 2018-09-12 10:18:54 +0100 | [diff] [blame] | 142 | |
| 143 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 144 | class NormalizePlanarYUVLayerValidationFixture : public NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 145 | { |
| 146 | public: |
| 147 | template <typename...> |
| 148 | void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout) |
| 149 | { |
| 150 | NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, dt, data_layout, QuantizationInfo()); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 155 | class NormalizePlanarYUVLayerValidationQuantizedFixture : public NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 156 | { |
| 157 | public: |
| 158 | template <typename...> |
| 159 | void setup(TensorShape shape0, TensorShape shape1, DataType dt, DataLayout data_layout, QuantizationInfo quantization_info) |
| 160 | { |
| 161 | NormalizePlanarYUVLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape0, shape1, dt, data_layout, quantization_info); |
| 162 | } |
| 163 | }; |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 164 | } // namespace validation |
| 165 | } // namespace test |
| 166 | } // namespace arm_compute |
| 167 | #endif /* ARM_COMPUTE_TEST_NORMALIZE_PLANAR_YUV_LAYER_FIXTURE */ |