1 /* 2 * Copyright (c) 2017-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_HOG_DESCRIPTOR_FIXTURE 25 #define ARM_COMPUTE_TEST_HOG_DESCRIPTOR_FIXTURE 26 27 #include "arm_compute/core/HOGInfo.h" 28 #include "arm_compute/core/TensorShape.h" 29 #include "arm_compute/core/Types.h" 30 #include "tests/AssetsLibrary.h" 31 #include "tests/Globals.h" 32 #include "tests/IAccessor.h" 33 #include "tests/framework/Asserts.h" 34 #include "tests/framework/Fixture.h" 35 #include "tests/validation/reference/HOGDescriptor.h" 36 37 namespace arm_compute 38 { 39 namespace test 40 { 41 namespace validation 42 { 43 template <typename TensorType, typename HOGType, typename AccessorType, typename FunctionType, typename T, typename U> 44 class HOGDescriptorValidationFixture : public framework::Fixture 45 { 46 public: 47 template <typename...> setup(std::string image,HOGInfo hog_info,Format format,BorderMode border_mode)48 void setup(std::string image, HOGInfo hog_info, Format format, BorderMode border_mode) 49 { 50 // Only defined borders supported 51 ARM_COMPUTE_ERROR_ON(border_mode == BorderMode::UNDEFINED); 52 53 // Generate a random constant value 54 std::mt19937 gen(library->seed()); 55 std::uniform_int_distribution<T> int_dist(0, 255); 56 const T constant_border_value = int_dist(gen); 57 58 _target = compute_target(image, format, border_mode, constant_border_value, hog_info); 59 _reference = compute_reference(image, format, border_mode, constant_border_value, hog_info); 60 } 61 62 protected: 63 template <typename V> fill(V && tensor,const std::string image,Format format)64 void fill(V &&tensor, const std::string image, Format format) 65 { 66 library->fill(tensor, image, format); 67 } 68 69 template <typename V, typename D> fill(V && tensor,int i,D max)70 void fill(V &&tensor, int i, D max) 71 { 72 library->fill_tensor_uniform(tensor, i, static_cast<D>(0), max); 73 } 74 compute_target(const std::string image,Format & format,BorderMode & border_mode,T constant_border_value,const HOGInfo & hog_info)75 TensorType compute_target(const std::string image, Format &format, BorderMode &border_mode, T constant_border_value, const HOGInfo &hog_info) 76 { 77 // Get image shape for src tensor 78 TensorShape shape = library->get_image_shape(image); 79 80 // Create tensor info for HOG descriptor 81 TensorInfo tensor_info_hog_descriptor(hog_info, shape.x(), shape.y()); 82 83 // Create HOG 84 HOGType hog = create_HOG<HOGType>(hog_info); 85 86 // Create tensors 87 TensorType src = create_tensor<TensorType>(shape, data_type_from_format(format)); 88 TensorType dst = create_tensor<TensorType>(tensor_info_hog_descriptor.tensor_shape(), DataType::F32, tensor_info_hog_descriptor.num_channels()); 89 90 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); 91 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); 92 93 // Create and configure function 94 FunctionType hog_descriptor; 95 hog_descriptor.configure(&src, &dst, &hog, border_mode, constant_border_value); 96 97 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); 98 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); 99 100 // Allocate tensors 101 src.allocator()->allocate(); 102 dst.allocator()->allocate(); 103 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); 104 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); 105 106 const T max = std::numeric_limits<T>::max(); 107 108 // Fill tensors 109 fill(AccessorType(src), image, format); 110 fill(AccessorType(dst), 1, static_cast<U>(max)); 111 112 // Compute function 113 hog_descriptor.run(); 114 115 return dst; 116 } 117 compute_reference(const std::string image,Format format,BorderMode border_mode,T constant_border_value,const HOGInfo & hog_info)118 SimpleTensor<U> compute_reference(const std::string image, Format format, BorderMode border_mode, T constant_border_value, const HOGInfo &hog_info) 119 { 120 // Create reference 121 SimpleTensor<T> src{ library->get_image_shape(image), data_type_from_format(format) }; 122 123 // Fill reference 124 fill(src, image, format); 125 126 return reference::hog_descriptor<U>(src, border_mode, constant_border_value, hog_info); 127 } 128 129 TensorType _target{}; 130 SimpleTensor<U> _reference{}; 131 }; 132 } // namespace validation 133 } // namespace test 134 } // namespace arm_compute 135 #endif /* ARM_COMPUTE_TEST_HOG_DESCRIPTOR_FIXTURE */ 136