Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 1 | /* |
| 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 Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/CPP/Scale.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 43 | class ScaleValidationFixture : public framework::Fixture |
| 44 | { |
| 45 | public: |
| 46 | template <typename...> |
| 47 | void setup(TensorShape shape, DataType data_type, InterpolationPolicy policy, BorderMode border_mode) |
| 48 | { |
| 49 | _shape = shape; |
| 50 | _policy = policy; |
| 51 | _border_mode = border_mode; |
| 52 | _data_type = data_type; |
| 53 | |
| 54 | std::mt19937 generator(library->seed()); |
| 55 | std::uniform_real_distribution<float> distribution_float(0.25, 4); |
| 56 | const float scale_x = distribution_float(generator); |
| 57 | const float scale_y = distribution_float(generator); |
| 58 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 59 | uint8_t constant_border_value = distribution_u8(generator); |
| 60 | |
| 61 | _target = compute_target(shape, scale_x, scale_y, policy, border_mode, constant_border_value); |
| 62 | _reference = compute_reference(shape, scale_x, scale_y, policy, border_mode, constant_border_value); |
| 63 | } |
| 64 | |
| 65 | protected: |
| 66 | template <typename U> |
| 67 | void fill(U &&tensor) |
| 68 | { |
| 69 | library->fill_tensor_uniform(tensor, 0); |
| 70 | } |
| 71 | |
| 72 | TensorType compute_target(const TensorShape &shape, const float scale_x, const float scale_y, |
| 73 | InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value) |
| 74 | { |
| 75 | // Create tensors |
| 76 | TensorType src = create_tensor<TensorType>(shape, _data_type); |
| 77 | TensorShape shape_scaled(shape); |
| 78 | shape_scaled.set(0, shape[0] * scale_x); |
| 79 | shape_scaled.set(1, shape[1] * scale_y); |
| 80 | TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type); |
| 81 | |
| 82 | // Create and configure function |
| 83 | FunctionType scale; |
| 84 | |
| 85 | scale.configure(&src, &dst, policy, border_mode, constant_border_value); |
| 86 | |
| 87 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | |
| 90 | // Allocate tensors |
| 91 | src.allocator()->allocate(); |
| 92 | dst.allocator()->allocate(); |
| 93 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 95 | |
| 96 | // Fill tensors |
| 97 | fill(AccessorType(src)); |
| 98 | |
| 99 | // Compute function |
| 100 | scale.run(); |
| 101 | |
| 102 | return dst; |
| 103 | } |
| 104 | |
| 105 | SimpleTensor<T> compute_reference(const TensorShape &shape, const float scale_x, const float scale_y, |
| 106 | InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value) |
| 107 | { |
| 108 | // Create reference |
| 109 | SimpleTensor<T> src{ shape, _data_type }; |
| 110 | |
| 111 | // Fill reference |
| 112 | fill(src); |
| 113 | |
| 114 | return reference::scale<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value); |
| 115 | } |
| 116 | |
| 117 | TensorType _target{}; |
| 118 | SimpleTensor<T> _reference{}; |
| 119 | TensorShape _shape{}; |
| 120 | InterpolationPolicy _policy{}; |
| 121 | BorderMode _border_mode{}; |
| 122 | DataType _data_type{}; |
| 123 | }; |
| 124 | } // namespace validation |
| 125 | } // namespace test |
| 126 | } // namespace arm_compute |
| 127 | #endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */ |