giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [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_ROIALIGNLAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ROIALIGNLAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/CL/functions/CLROIAlignLayer.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/Helpers.h" |
| 36 | #include "tests/validation/reference/ROIAlignLayer.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 45 | class ROIAlignLayerFixture : public framework::Fixture |
| 46 | { |
| 47 | public: |
| 48 | template <typename...> |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 49 | void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 50 | { |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 51 | _target = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 52 | _reference = compute_reference(input_shape, data_type, pool_info, rois_shape); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | template <typename U> |
| 57 | void fill(U &&tensor) |
| 58 | { |
| 59 | library->fill_tensor_uniform(tensor, 0); |
| 60 | } |
| 61 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 62 | template <typename U> |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 63 | void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape, DataLayout data_layout = DataLayout::NCHW) |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 64 | { |
| 65 | const size_t values_per_roi = rois_shape.x(); |
| 66 | const size_t num_rois = rois_shape.y(); |
| 67 | |
| 68 | std::mt19937 gen(library->seed()); |
| 69 | T *rois_ptr = static_cast<T *>(rois.data()); |
| 70 | |
| 71 | const float pool_width = pool_info.pooled_width(); |
| 72 | const float pool_height = pool_info.pooled_height(); |
| 73 | const float roi_scale = pool_info.spatial_scale(); |
| 74 | |
| 75 | // Calculate distribution bounds |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 76 | const auto scaled_width = static_cast<T>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width); |
| 77 | const auto scaled_height = static_cast<T>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 78 | const auto min_width = static_cast<T>(pool_width / roi_scale); |
| 79 | const auto min_height = static_cast<T>(pool_height / roi_scale); |
| 80 | |
| 81 | // Create distributions |
| 82 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 83 | std::uniform_int_distribution<> dist_x1(0, scaled_width); |
| 84 | std::uniform_int_distribution<> dist_y1(0, scaled_height); |
| 85 | std::uniform_int_distribution<> dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width)); |
| 86 | std::uniform_int_distribution<> dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height)); |
| 87 | |
| 88 | for(unsigned int pw = 0; pw < num_rois; ++pw) |
| 89 | { |
| 90 | const auto batch_idx = dist_batch(gen); |
| 91 | const auto x1 = dist_x1(gen); |
| 92 | const auto y1 = dist_y1(gen); |
| 93 | const auto x2 = x1 + dist_w(gen); |
| 94 | const auto y2 = y1 + dist_h(gen); |
| 95 | |
| 96 | rois_ptr[values_per_roi * pw] = batch_idx; |
| 97 | rois_ptr[values_per_roi * pw + 1] = x1; |
| 98 | rois_ptr[values_per_roi * pw + 2] = y1; |
| 99 | rois_ptr[values_per_roi * pw + 3] = x2; |
| 100 | rois_ptr[values_per_roi * pw + 4] = y2; |
| 101 | } |
| 102 | } |
| 103 | |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 104 | TensorType compute_target(TensorShape input_shape, |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 105 | DataType data_type, |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 106 | DataLayout data_layout, |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 107 | const ROIPoolingLayerInfo &pool_info, |
| 108 | const TensorShape rois_shape) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 109 | { |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 110 | if(data_layout == DataLayout::NHWC) |
| 111 | { |
| 112 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 113 | } |
| 114 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 115 | // Create tensors |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 116 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 117 | TensorType rois_tensor = create_tensor<TensorType>(rois_shape, data_type); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 118 | TensorType dst; |
| 119 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 120 | // Create and configure function |
| 121 | FunctionType roi_align_layer; |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 122 | roi_align_layer.configure(&src, &rois_tensor, &dst, pool_info); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 123 | |
| 124 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_EXPECT(rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 127 | |
| 128 | // Allocate tensors |
| 129 | src.allocator()->allocate(); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 130 | rois_tensor.allocator()->allocate(); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 131 | dst.allocator()->allocate(); |
| 132 | |
| 133 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 134 | ARM_COMPUTE_EXPECT(!rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 136 | |
| 137 | // Fill tensors |
| 138 | fill(AccessorType(src)); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 139 | generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape, data_layout); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 140 | |
| 141 | // Compute function |
| 142 | roi_align_layer.run(); |
| 143 | |
| 144 | return dst; |
| 145 | } |
| 146 | |
| 147 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, |
| 148 | DataType data_type, |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 149 | const ROIPoolingLayerInfo &pool_info, |
| 150 | const TensorShape rois_shape) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 151 | { |
| 152 | // Create reference tensor |
| 153 | SimpleTensor<T> src{ input_shape, data_type }; |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 154 | SimpleTensor<T> rois_tensor{ rois_shape, data_type }; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 155 | |
| 156 | // Fill reference tensor |
| 157 | fill(src); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 158 | generate_rois(rois_tensor, input_shape, pool_info, rois_shape); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 159 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 160 | return reference::roi_align_layer(src, rois_tensor, pool_info); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | TensorType _target{}; |
| 164 | SimpleTensor<T> _reference{}; |
| 165 | }; |
| 166 | |
| 167 | } // namespace validation |
| 168 | } // namespace test |
| 169 | } // namespace arm_compute |
| 170 | #endif /* ARM_COMPUTE_TEST_ROIALIGNLAYER_FIXTURE */ |