blob: 2c828272de610549e4d69294e9e2fd97d7274d31 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Manuel Bottinicc5171b2019-01-09 17:04:39 +00002 * Copyright (c) 2017-2019 ARM Limited.
SiCong Li3e363692017-07-04 15:02:10 +01003 *
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 Li3e363692017-07-04 15:02:10 +010029#include "tests/Globals.h"
30#include "tests/Utils.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/framework/Fixture.h"
SiCong Li3e363692017-07-04 15:02:10 +010032
33#include <vector>
34
35namespace arm_compute
36{
37namespace test
38{
Michalis Spyrou724079d2017-12-15 11:37:37 +000039namespace benchmark
40{
SiCong Li3e363692017-07-04 15:02:10 +010041/** Fixture that can be used for NEON and CL */
Manuel Bottinicc5171b2019-01-09 17:04:39 +000042template <typename TensorType, typename Function, typename AccessorType, typename T>
SiCong Li3e363692017-07-04 15:02:10 +010043class ROIPoolingLayerFixture : public framework::Fixture
44{
45public:
46 template <typename...>
Manuel Bottinicc5171b2019-01-09 17:04:39 +000047 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, int batches)
SiCong Li3e363692017-07-04 15:02:10 +010048 {
49 // Set batched in source and destination shapes
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050
51 TensorShape shape_dst;
Manuel Bottinicc5171b2019-01-09 17:04:39 +000052 rois_tensor = create_tensor<TensorType>(rois_shape, DataType::U16);
53
54 input_shape.set(input_shape.num_dimensions(), batches);
SiCong Li3e363692017-07-04 15:02:10 +010055 shape_dst.set(0, pool_info.pooled_width());
56 shape_dst.set(1, pool_info.pooled_height());
Manuel Bottinicc5171b2019-01-09 17:04:39 +000057 shape_dst.set(2, input_shape.z());
58 shape_dst.set(3, rois_shape[1]);
SiCong Li3e363692017-07-04 15:02:10 +010059
60 // Create tensors
Manuel Bottinicc5171b2019-01-09 17:04:39 +000061 src = create_tensor<TensorType>(input_shape, data_type, 1);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010062 dst = create_tensor<TensorType>(shape_dst, data_type, 1);
SiCong Li3e363692017-07-04 15:02:10 +010063
SiCong Li3e363692017-07-04 15:02:10 +010064 // Create and configure function
Manuel Bottinicc5171b2019-01-09 17:04:39 +000065 roi_pool.configure(&src, &rois_tensor, &dst, pool_info);
SiCong Li3e363692017-07-04 15:02:10 +010066
67 // Allocate tensors
Manuel Bottinicc5171b2019-01-09 17:04:39 +000068 rois_tensor.allocator()->allocate();
SiCong Li3e363692017-07-04 15:02:10 +010069 src.allocator()->allocate();
70 dst.allocator()->allocate();
Manuel Bottinicc5171b2019-01-09 17:04:39 +000071
72 // Create random ROIs
73 generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape);
SiCong Li3e363692017-07-04 15:02:10 +010074 }
75
76 void run()
77 {
78 roi_pool.run();
79 }
80
Joel Liang1c5ffd62017-12-28 10:09:51 +080081 void sync()
82 {
83 sync_if_necessary<TensorType>();
84 sync_tensor_if_necessary<TensorType>(dst);
85 }
86
SiCong Li3e363692017-07-04 15:02:10 +010087 void teardown()
88 {
89 src.allocator()->free();
90 dst.allocator()->free();
91 }
92
Manuel Bottinicc5171b2019-01-09 17:04:39 +000093protected:
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 Li3e363692017-07-04 15:02:10 +0100136private:
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000137 TensorType src{};
138 TensorType dst{};
139 TensorType rois_tensor{};
140 Function roi_pool{};
SiCong Li3e363692017-07-04 15:02:10 +0100141};
Michalis Spyrou724079d2017-12-15 11:37:37 +0000142} // namespace benchmark
SiCong Li3e363692017-07-04 15:02:10 +0100143} // namespace test
144} // namespace arm_compute
145#endif /* ARM_COMPUTE_TEST_ROIPOOLINGLAYERFIXTURE */