From cc5171b85654b9f19a5f52bbe8abea0572ee0163 Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Wed, 9 Jan 2019 17:04:39 +0000 Subject: COMPMID-1677: Change ROIPooling layer interface to accept ROIs as tensors Change-Id: If16b572a4d906187b77f32133a72a44316fa74e4 Reviewed-on: https://review.mlplatform.org/490 Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- tests/benchmark/fixtures/ROIPoolingLayerFixture.h | 78 ++++++++++++++++++----- 1 file changed, 61 insertions(+), 17 deletions(-) (limited to 'tests/benchmark/fixtures') diff --git a/tests/benchmark/fixtures/ROIPoolingLayerFixture.h b/tests/benchmark/fixtures/ROIPoolingLayerFixture.h index fa4a5b7044..2c828272de 100644 --- a/tests/benchmark/fixtures/ROIPoolingLayerFixture.h +++ b/tests/benchmark/fixtures/ROIPoolingLayerFixture.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 ARM Limited. + * Copyright (c) 2017-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -39,37 +39,38 @@ namespace test namespace benchmark { /** Fixture that can be used for NEON and CL */ -template +template class ROIPoolingLayerFixture : public framework::Fixture { public: template - void setup(TensorShape shape, const ROIPoolingLayerInfo pool_info, unsigned int num_rois, DataType data_type, int batches) + void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, int batches) { // Set batched in source and destination shapes TensorShape shape_dst; - shape.set(shape.num_dimensions(), batches); + rois_tensor = create_tensor(rois_shape, DataType::U16); + + input_shape.set(input_shape.num_dimensions(), batches); shape_dst.set(0, pool_info.pooled_width()); shape_dst.set(1, pool_info.pooled_height()); - shape_dst.set(2, shape.z()); - shape_dst.set(3, num_rois); + shape_dst.set(2, input_shape.z()); + shape_dst.set(3, rois_shape[1]); // Create tensors - src = create_tensor(shape, data_type, 1); + src = create_tensor(input_shape, data_type, 1); dst = create_tensor(shape_dst, data_type, 1); - // Create random ROIs - std::vector rois = generate_random_rois(shape, pool_info, num_rois, 0U); - rois_array = arm_compute::support::cpp14::make_unique(num_rois); - fill_array(ArrayAccessor(*rois_array), rois); - // Create and configure function - roi_pool.configure(&src, rois_array.get(), &dst, pool_info); + roi_pool.configure(&src, &rois_tensor, &dst, pool_info); // Allocate tensors + rois_tensor.allocator()->allocate(); src.allocator()->allocate(); dst.allocator()->allocate(); + + // Create random ROIs + generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape); } void run() @@ -89,11 +90,54 @@ public: dst.allocator()->free(); } +protected: + template + void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape) + { + const size_t values_per_roi = rois_shape.x(); + const size_t num_rois = rois_shape.y(); + + std::mt19937 gen(library->seed()); + uint16_t *rois_ptr = static_cast(rois.data()); + + const float pool_width = pool_info.pooled_width(); + const float pool_height = pool_info.pooled_height(); + const float roi_scale = pool_info.spatial_scale(); + + // Calculate distribution bounds + const auto scaled_width = static_cast((shape.x() / roi_scale) / pool_width); + const auto scaled_height = static_cast((shape.y() / roi_scale) / pool_height); + const auto min_width = static_cast(pool_width / roi_scale); + const auto min_height = static_cast(pool_height / roi_scale); + + // Create distributions + std::uniform_int_distribution dist_batch(0, shape[3] - 1); + std::uniform_int_distribution dist_x1(0, scaled_width); + std::uniform_int_distribution dist_y1(0, scaled_height); + std::uniform_int_distribution dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width)); + std::uniform_int_distribution dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height)); + + for(unsigned int pw = 0; pw < num_rois; ++pw) + { + const auto batch_idx = dist_batch(gen); + const auto x1 = dist_x1(gen); + const auto y1 = dist_y1(gen); + const auto x2 = x1 + dist_w(gen); + const auto y2 = y1 + dist_h(gen); + + rois_ptr[values_per_roi * pw] = batch_idx; + rois_ptr[values_per_roi * pw + 1] = x1; + rois_ptr[values_per_roi * pw + 2] = y1; + rois_ptr[values_per_roi * pw + 3] = x2; + rois_ptr[values_per_roi * pw + 4] = y2; + } + } + private: - TensorType src{}; - TensorType dst{}; - std::unique_ptr rois_array{}; - Function roi_pool{}; + TensorType src{}; + TensorType dst{}; + TensorType rois_tensor{}; + Function roi_pool{}; }; } // namespace benchmark } // namespace test -- cgit v1.2.1