SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +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_ROIPOOLINGLAYERFIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ROIPOOLINGLAYERFIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 29 | #include "tests/Globals.h" |
| 30 | #include "tests/Utils.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 31 | #include "tests/framework/Fixture.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 32 | |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace test |
| 38 | { |
Michalis Spyrou | 724079d | 2017-12-15 11:37:37 +0000 | [diff] [blame] | 39 | namespace benchmark |
| 40 | { |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 41 | /** Fixture that can be used for NEON and CL */ |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 42 | template <typename TensorType, typename Function, typename AccessorType, typename T> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 43 | class ROIPoolingLayerFixture : public framework::Fixture |
| 44 | { |
| 45 | public: |
| 46 | template <typename...> |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 47 | void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, int batches) |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 48 | { |
| 49 | // Set batched in source and destination shapes |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 50 | |
| 51 | TensorShape shape_dst; |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 52 | rois_tensor = create_tensor<TensorType>(rois_shape, DataType::U16); |
| 53 | |
| 54 | input_shape.set(input_shape.num_dimensions(), batches); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 55 | shape_dst.set(0, pool_info.pooled_width()); |
| 56 | shape_dst.set(1, pool_info.pooled_height()); |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 57 | shape_dst.set(2, input_shape.z()); |
| 58 | shape_dst.set(3, rois_shape[1]); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 59 | |
| 60 | // Create tensors |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 61 | src = create_tensor<TensorType>(input_shape, data_type, 1); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 62 | dst = create_tensor<TensorType>(shape_dst, data_type, 1); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 63 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 64 | // Create and configure function |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 65 | roi_pool.configure(&src, &rois_tensor, &dst, pool_info); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 66 | |
| 67 | // Allocate tensors |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 68 | rois_tensor.allocator()->allocate(); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 69 | src.allocator()->allocate(); |
| 70 | dst.allocator()->allocate(); |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 71 | |
| 72 | // Create random ROIs |
| 73 | generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void run() |
| 77 | { |
| 78 | roi_pool.run(); |
| 79 | } |
| 80 | |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 81 | void sync() |
| 82 | { |
| 83 | sync_if_necessary<TensorType>(); |
| 84 | sync_tensor_if_necessary<TensorType>(dst); |
| 85 | } |
| 86 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 87 | void teardown() |
| 88 | { |
| 89 | src.allocator()->free(); |
| 90 | dst.allocator()->free(); |
| 91 | } |
| 92 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 93 | protected: |
| 94 | template <typename U> |
| 95 | void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape) |
| 96 | { |
| 97 | const size_t values_per_roi = rois_shape.x(); |
| 98 | const size_t num_rois = rois_shape.y(); |
| 99 | |
| 100 | std::mt19937 gen(library->seed()); |
| 101 | uint16_t *rois_ptr = static_cast<uint16_t *>(rois.data()); |
| 102 | |
| 103 | const float pool_width = pool_info.pooled_width(); |
| 104 | const float pool_height = pool_info.pooled_height(); |
| 105 | const float roi_scale = pool_info.spatial_scale(); |
| 106 | |
| 107 | // Calculate distribution bounds |
| 108 | const auto scaled_width = static_cast<uint16_t>((shape.x() / roi_scale) / pool_width); |
| 109 | const auto scaled_height = static_cast<uint16_t>((shape.y() / roi_scale) / pool_height); |
| 110 | const auto min_width = static_cast<uint16_t>(pool_width / roi_scale); |
| 111 | const auto min_height = static_cast<uint16_t>(pool_height / roi_scale); |
| 112 | |
| 113 | // Create distributions |
| 114 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 115 | std::uniform_int_distribution<uint16_t> dist_x1(0, scaled_width); |
| 116 | std::uniform_int_distribution<uint16_t> dist_y1(0, scaled_height); |
| 117 | std::uniform_int_distribution<uint16_t> dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width)); |
| 118 | std::uniform_int_distribution<uint16_t> dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height)); |
| 119 | |
| 120 | for(unsigned int pw = 0; pw < num_rois; ++pw) |
| 121 | { |
| 122 | const auto batch_idx = dist_batch(gen); |
| 123 | const auto x1 = dist_x1(gen); |
| 124 | const auto y1 = dist_y1(gen); |
| 125 | const auto x2 = x1 + dist_w(gen); |
| 126 | const auto y2 = y1 + dist_h(gen); |
| 127 | |
| 128 | rois_ptr[values_per_roi * pw] = batch_idx; |
| 129 | rois_ptr[values_per_roi * pw + 1] = x1; |
| 130 | rois_ptr[values_per_roi * pw + 2] = y1; |
| 131 | rois_ptr[values_per_roi * pw + 3] = x2; |
| 132 | rois_ptr[values_per_roi * pw + 4] = y2; |
| 133 | } |
| 134 | } |
| 135 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 136 | private: |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 137 | TensorType src{}; |
| 138 | TensorType dst{}; |
| 139 | TensorType rois_tensor{}; |
| 140 | Function roi_pool{}; |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 141 | }; |
Michalis Spyrou | 724079d | 2017-12-15 11:37:37 +0000 | [diff] [blame] | 142 | } // namespace benchmark |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 143 | } // namespace test |
| 144 | } // namespace arm_compute |
| 145 | #endif /* ARM_COMPUTE_TEST_ROIPOOLINGLAYERFIXTURE */ |