Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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_YOLO_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_YOLO_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/YOLOLayer.h" |
| 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 46 | class YOLOValidationGenericFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, int32_t num_classes, DataLayout data_layout, DataType data_type, |
| 51 | QuantizationInfo quantization_info) |
| 52 | { |
| 53 | _data_type = data_type; |
| 54 | _function = function; |
| 55 | |
| 56 | ActivationLayerInfo info(function, alpha_beta, alpha_beta); |
| 57 | |
| 58 | _target = compute_target(shape, in_place, info, num_classes, data_layout, data_type, quantization_info); |
| 59 | _reference = compute_reference(shape, info, num_classes, data_type, quantization_info); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | template <typename U> |
| 64 | void fill(U &&tensor) |
| 65 | { |
| 66 | float min_bound = 0; |
| 67 | float max_bound = 0; |
| 68 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type); |
| 69 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 70 | library->fill(tensor, distribution, 0); |
| 71 | } |
| 72 | |
| 73 | TensorType compute_target(TensorShape shape, bool in_place, const ActivationLayerInfo &info, int32_t num_classes, DataLayout data_layout, DataType data_type, QuantizationInfo quantization_info) |
| 74 | { |
| 75 | if(data_layout == DataLayout::NHWC) |
| 76 | { |
| 77 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 78 | } |
| 79 | |
| 80 | // Create tensors |
| 81 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, quantization_info, data_layout); |
| 82 | TensorType dst = create_tensor<TensorType>(shape, data_type, 1, quantization_info, data_layout); |
| 83 | |
| 84 | // Create and configure function |
| 85 | FunctionType yolo_layer; |
| 86 | |
| 87 | TensorType *dst_ptr = in_place ? &src : &dst; |
| 88 | |
| 89 | yolo_layer.configure(&src, dst_ptr, info, num_classes); |
| 90 | |
| 91 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | |
| 94 | // Allocate tensors |
| 95 | src.allocator()->allocate(); |
| 96 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 97 | |
| 98 | if(!in_place) |
| 99 | { |
| 100 | dst.allocator()->allocate(); |
| 101 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | } |
| 103 | |
| 104 | // Fill tensors |
| 105 | fill(AccessorType(src)); |
| 106 | |
| 107 | // Compute function |
| 108 | yolo_layer.run(); |
| 109 | |
| 110 | if(in_place) |
| 111 | { |
| 112 | return src; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | return dst; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | SimpleTensor<T> compute_reference(const TensorShape &shape, const ActivationLayerInfo &info, int32_t num_classes, DataType data_type, QuantizationInfo quantization_info) |
| 121 | { |
| 122 | // Create reference |
| 123 | SimpleTensor<T> src{ shape, data_type, 1, quantization_info }; |
| 124 | |
| 125 | // Fill reference |
| 126 | fill(src); |
| 127 | |
| 128 | return reference::yolo_layer<T>(src, info, num_classes); |
| 129 | } |
| 130 | |
| 131 | TensorType _target{}; |
| 132 | SimpleTensor<T> _reference{}; |
| 133 | DataType _data_type{}; |
| 134 | ActivationLayerInfo::ActivationFunction _function{}; |
| 135 | }; |
| 136 | |
| 137 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 138 | class YOLOValidationFixture : public YOLOValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 139 | { |
| 140 | public: |
| 141 | template <typename...> |
| 142 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, int32_t num_classes, DataLayout data_layout, DataType data_type) |
| 143 | { |
| 144 | YOLOValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, num_classes, data_layout, data_type, QuantizationInfo()); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 149 | class YOLOValidationQuantizedFixture : public YOLOValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 150 | { |
| 151 | public: |
| 152 | template <typename...> |
| 153 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, int32_t num_classes, DataLayout data_layout, DataType data_type, |
| 154 | QuantizationInfo quantization_info) |
| 155 | { |
| 156 | YOLOValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, num_classes, data_layout, data_type, quantization_info); |
| 157 | } |
| 158 | }; |
| 159 | } // namespace validation |
| 160 | } // namespace test |
| 161 | } // namespace arm_compute |
| 162 | #endif // ARM_COMPUTE_TEST_YOLO_LAYER_FIXTURE |