From 60f0a41c45813fa9c85cd4f8fbed57c4c9284a5c Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Wed, 24 Oct 2018 17:27:02 +0100 Subject: COMPMID-1676: Change CLROIAlign interface to accept ROIs as tensors Change-Id: I69e995973597ba3927d29e4f6ed5438560e53d77 --- tests/datasets/ROIAlignLayerDataset.h | 143 +++++++++++++++++++++++ tests/validation/CL/ROIAlignLayer.cpp | 48 +++++--- tests/validation/fixtures/ROIAlignLayerFixture.h | 80 +++++++++---- tests/validation/reference/ROIAlignLayer.cpp | 35 +++--- tests/validation/reference/ROIAlignLayer.h | 2 +- 5 files changed, 257 insertions(+), 51 deletions(-) create mode 100644 tests/datasets/ROIAlignLayerDataset.h (limited to 'tests') diff --git a/tests/datasets/ROIAlignLayerDataset.h b/tests/datasets/ROIAlignLayerDataset.h new file mode 100644 index 0000000000..27c6ee4d91 --- /dev/null +++ b/tests/datasets/ROIAlignLayerDataset.h @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_ROI_ALIGN_LAYER_DATASET +#define ARM_COMPUTE_TEST_ROI_ALIGN_LAYER_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class ROIAlignLayerDataset +{ +public: + using type = std::tuple; + + struct iterator + { + iterator(std::vector::const_iterator tensor_shape_it, + std::vector::const_iterator infos_it, + std::vector::const_iterator rois_shape_it) + : _tensor_shape_it{ std::move(tensor_shape_it) }, + _infos_it{ std::move(infos_it) }, + _rois_shape_it{ std::move(rois_shape_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_tensor_shape_it << ":"; + description << "Info=" << *_infos_it << ":"; + description << "ROIS=" << *_rois_shape_it; + return description.str(); + } + + ROIAlignLayerDataset::type operator*() const + { + return std::make_tuple(*_tensor_shape_it, *_infos_it, *_rois_shape_it); + } + + iterator &operator++() + { + ++_tensor_shape_it; + ++_infos_it; + ++_rois_shape_it; + + return *this; + } + + private: + std::vector::const_iterator _tensor_shape_it; + std::vector::const_iterator _infos_it; + std::vector::const_iterator _rois_shape_it; + }; + + iterator begin() const + { + return iterator(_tensor_shapes.begin(), _infos.begin(), _rois_shape.begin()); + } + + int size() const + { + return std::min(std::min(_tensor_shapes.size(), _infos.size()), _rois_shape.size()); + } + + void add_config(TensorShape tensor_shape, ROIPoolingLayerInfo info, TensorShape rois_shape) + { + _tensor_shapes.emplace_back(std::move(tensor_shape)); + _infos.emplace_back(std::move(info)); + _rois_shape.emplace_back(std::move(rois_shape)); + } + +protected: + ROIAlignLayerDataset() = default; + ROIAlignLayerDataset(ROIAlignLayerDataset &&) = default; + +private: + std::vector _tensor_shapes{}; + std::vector _infos{}; + std::vector _rois_shape{}; +}; + +class SmallROIAlignLayerDataset final : public ROIAlignLayerDataset +{ +public: + SmallROIAlignLayerDataset() + { + add_config(TensorShape(50U, 47U, 1U, 1U), ROIPoolingLayerInfo(7U, 7U, 1.f / 4.f), TensorShape(5U, 1U)); + add_config(TensorShape(50U, 47U, 3U, 4U), ROIPoolingLayerInfo(7U, 7U, 1.f / 4.f), TensorShape(5U, 1U)); + add_config(TensorShape(50U, 47U, 3U, 1U), ROIPoolingLayerInfo(7U, 7U, 1.f / 4.f), TensorShape(5U, 10U)); + add_config(TensorShape(50U, 47U, 10U, 1U), ROIPoolingLayerInfo(7U, 7U, 1.f / 4.f), TensorShape(5U, 80U)); + + //Spatial Scale 1/4 + add_config(TensorShape(50U, 47U, 80U, 4U), ROIPoolingLayerInfo(7U, 7U, 1.f / 4.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 3U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 4.f), TensorShape(5U, 40U)); + add_config(TensorShape(50U, 47U, 10U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 4.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 80U, 8U), ROIPoolingLayerInfo(9U, 9U, 1.f / 4.f), TensorShape(5U, 80U)); + + //Spatial Scale 1/8 + add_config(TensorShape(50U, 47U, 80U, 4U), ROIPoolingLayerInfo(7U, 7U, 1.f / 8.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 3U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 8.f), TensorShape(5U, 40U)); + add_config(TensorShape(50U, 47U, 10U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 8.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 80U, 8U), ROIPoolingLayerInfo(9U, 9U, 1.f / 8.f), TensorShape(5U, 80U)); + + //Spatial Scale 1/16 + add_config(TensorShape(50U, 47U, 80U, 4U), ROIPoolingLayerInfo(7U, 7U, 1.f / 16.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 3U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 16.f), TensorShape(5U, 40U)); + add_config(TensorShape(50U, 47U, 10U, 1U), ROIPoolingLayerInfo(9U, 9U, 1.f / 16.f), TensorShape(5U, 80U)); + add_config(TensorShape(50U, 47U, 80U, 8U), ROIPoolingLayerInfo(9U, 9U, 1.f / 16.f), TensorShape(5U, 80U)); + } +}; + +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_ROI_ALIGN_LAYER_DATASET */ diff --git a/tests/validation/CL/ROIAlignLayer.cpp b/tests/validation/CL/ROIAlignLayer.cpp index acea6d447c..f3fc3818f2 100644 --- a/tests/validation/CL/ROIAlignLayer.cpp +++ b/tests/validation/CL/ROIAlignLayer.cpp @@ -24,9 +24,8 @@ #include "arm_compute/runtime/CL/CLScheduler.h" #include "arm_compute/runtime/CL/functions/CLROIAlignLayer.h" #include "tests/CL/CLAccessor.h" -#include "tests/CL/CLArrayAccessor.h" #include "tests/Globals.h" -#include "tests/datasets/ROIPoolingLayerDataset.h" +#include "tests/datasets/ROIAlignLayerDataset.h" #include "tests/datasets/ShapeDatasets.h" #include "tests/framework/Macros.h" #include "tests/framework/datasets/Datasets.h" @@ -43,7 +42,10 @@ namespace validation namespace { RelativeTolerance relative_tolerance_f32(0.01f); -RelativeTolerance absolute_tolerance_f32(0.001f); +AbsoluteTolerance absolute_tolerance_f32(0.001f); + +RelativeTolerance relative_tolerance_f16(0.01f); +AbsoluteTolerance absolute_tolerance_f16(0.001f); } // namespace TEST_SUITE(CL) @@ -53,17 +55,28 @@ TEST_SUITE(RoiAlign) // clang-format off DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( framework::dataset::make("InputInfo", { TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::F32), // Mismatching data type input/rois TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::F32), // Mismatching data type input/output TensorInfo(TensorShape(250U, 128U, 2U), 1, DataType::F32), // Mismatching depth size input/output TensorInfo(TensorShape(250U, 128U, 2U), 1, DataType::F32), // Mismatching number of rois and output batch size + TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::F32), // Invalid number of values per ROIS TensorInfo(TensorShape(250U, 128U, 2U), 1, DataType::F32), // Mismatching height and width input/output }), - framework::dataset::make("NumRois", { 3U, 3U, 4U, 10U, 4U})), + framework::dataset::make("RoisInfo", { TensorInfo(TensorShape(5, 3U), 1, DataType::F32), + TensorInfo(TensorShape(5, 3U), 1, DataType::F16), + TensorInfo(TensorShape(5, 3U), 1, DataType::F32), + TensorInfo(TensorShape(5, 4U), 1, DataType::F32), + TensorInfo(TensorShape(5, 10U), 1, DataType::F32), + TensorInfo(TensorShape(4, 3U), 1, DataType::F32), + TensorInfo(TensorShape(5, 4U), 1, DataType::F32), + })), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(7U, 7U, 3U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 3U), 1, DataType::F32), TensorInfo(TensorShape(7U, 7U, 3U, 3U), 1, DataType::F16), TensorInfo(TensorShape(7U, 7U, 4U, 3U), 1, DataType::F32), TensorInfo(TensorShape(7U, 7U, 2U, 3U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 3U), 1, DataType::F32), TensorInfo(TensorShape(5U, 5U, 2U, 4U), 1, DataType::F32), })), framework::dataset::make("PoolInfo", { ROIPoolingLayerInfo(7U, 7U, 1./8), @@ -71,30 +84,35 @@ DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( ROIPoolingLayerInfo(7U, 7U, 1./8), ROIPoolingLayerInfo(7U, 7U, 1./8), ROIPoolingLayerInfo(7U, 7U, 1./8), + ROIPoolingLayerInfo(7U, 7U, 1./8), + ROIPoolingLayerInfo(7U, 7U, 1./8), })), - framework::dataset::make("Expected", { true, false, false, false, false })), - input_info, num_rois, output_info, pool_info, expected) + framework::dataset::make("Expected", { true, false, false, false, false, false, false })), + input_info, rois_info, output_info, pool_info, expected) { - ARM_COMPUTE_EXPECT(bool(CLROIAlignLayer::validate(&input_info.clone()->set_is_resizable(true), num_rois, &output_info.clone()->set_is_resizable(true), pool_info)) == expected, framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bool(CLROIAlignLayer::validate(&input_info.clone()->set_is_resizable(true), &rois_info.clone()->set_is_resizable(true), &output_info.clone()->set_is_resizable(true), pool_info)) == expected, framework::LogLevel::ERRORS); } // clang-format on // *INDENT-ON* template -using CLROIAlignLayerFixture = ROIAlignLayerFixture, CLArrayAccessor, T>; +using CLROIAlignLayerFixture = ROIAlignLayerFixture; TEST_SUITE(Float) -TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(SmallROIAlignLayer, CLROIAlignLayerFixture, framework::DatasetMode::ALL, - framework::dataset::combine(framework::dataset::combine(datasets::SmallROIPoolingLayerDataset(), - framework::dataset::make("DataType", { DataType::F32 })), - framework::dataset::make("Batches", { 1, 4, 8 }))) +FIXTURE_DATA_TEST_CASE(SmallROIAlignLayerFloat, CLROIAlignLayerFixture, framework::DatasetMode::ALL, + framework::dataset::combine(datasets::SmallROIAlignLayerDataset(), + framework::dataset::make("DataType", { DataType::F32 }))) { // Validate output validate(CLAccessor(_target), _reference, relative_tolerance_f32, .02f, absolute_tolerance_f32); } -TEST_SUITE_END() // FP32 - +FIXTURE_DATA_TEST_CASE(SmallROIAlignLayerHalf, CLROIAlignLayerFixture, framework::DatasetMode::ALL, + framework::dataset::combine(datasets::SmallROIAlignLayerDataset(), + framework::dataset::make("DataType", { DataType::F16 }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, relative_tolerance_f16, .02f, absolute_tolerance_f16); +} TEST_SUITE_END() // Float TEST_SUITE_END() // RoiAlign diff --git a/tests/validation/fixtures/ROIAlignLayerFixture.h b/tests/validation/fixtures/ROIAlignLayerFixture.h index d327b0914e..c029fbae8a 100644 --- a/tests/validation/fixtures/ROIAlignLayerFixture.h +++ b/tests/validation/fixtures/ROIAlignLayerFixture.h @@ -41,18 +41,15 @@ namespace test { namespace validation { -template +template class ROIAlignLayerFixture : public framework::Fixture { public: template - void setup(TensorShape input_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) { - input_shape.set(2, batches); - std::vector rois = generate_random_rois(input_shape, pool_info, num_rois, 0U); - - _target = compute_target(input_shape, data_type, rois, pool_info); - _reference = compute_reference(input_shape, data_type, rois, pool_info); + _target = compute_target(input_shape, data_type, pool_info, rois_shape); + _reference = compute_reference(input_shape, data_type, pool_info, rois_shape); } protected: @@ -62,37 +59,78 @@ protected: library->fill_tensor_uniform(tensor, 0); } + 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()); + 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; + } + } + TensorType compute_target(const TensorShape &input_shape, DataType data_type, - std::vector const &rois, - const ROIPoolingLayerInfo &pool_info) + const ROIPoolingLayerInfo &pool_info, + const TensorShape rois_shape) { // Create tensors - TensorType src = create_tensor(input_shape, data_type); + TensorType src = create_tensor(input_shape, data_type); + TensorType rois_tensor = create_tensor(rois_shape, data_type); TensorType dst; - size_t num_rois = rois.size(); - - // Create roi arrays - std::unique_ptr rois_array = arm_compute::support::cpp14::make_unique(num_rois); - fill_array(ArrayAccessor(*rois_array), rois); - // Create and configure function FunctionType roi_align_layer; - roi_align_layer.configure(&src, rois_array.get(), &dst, pool_info); + roi_align_layer.configure(&src, &rois_tensor, &dst, pool_info); ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); // Allocate tensors src.allocator()->allocate(); + rois_tensor.allocator()->allocate(); dst.allocator()->allocate(); ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); // Fill tensors fill(AccessorType(src)); + generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape); // Compute function roi_align_layer.run(); @@ -102,16 +140,18 @@ protected: SimpleTensor compute_reference(const TensorShape &input_shape, DataType data_type, - std::vector const &rois, - const ROIPoolingLayerInfo &pool_info) + const ROIPoolingLayerInfo &pool_info, + const TensorShape rois_shape) { // Create reference tensor SimpleTensor src{ input_shape, data_type }; + SimpleTensor rois_tensor{ rois_shape, data_type }; // Fill reference tensor fill(src); + generate_rois(rois_tensor, input_shape, pool_info, rois_shape); - return reference::roi_align_layer(src, rois, pool_info); + return reference::roi_align_layer(src, rois_tensor, pool_info); } TensorType _target{}; diff --git a/tests/validation/reference/ROIAlignLayer.cpp b/tests/validation/reference/ROIAlignLayer.cpp index 68a465d18f..8a76983d44 100644 --- a/tests/validation/reference/ROIAlignLayer.cpp +++ b/tests/validation/reference/ROIAlignLayer.cpp @@ -114,30 +114,35 @@ T clamp(T value, T lower, T upper) } } // namespace template -SimpleTensor roi_align_layer(const SimpleTensor &src, const std::vector &rois, const ROIPoolingLayerInfo &pool_info) +SimpleTensor roi_align_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info) { - const size_t num_rois = rois.size(); - DataType dst_data_type = src.data_type(); + const size_t values_per_roi = rois.shape()[0]; + const size_t num_rois = rois.shape()[1]; + DataType dst_data_type = src.data_type(); + + const auto *rois_ptr = static_cast(rois.data()); TensorShape input_shape = src.shape(); TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois); SimpleTensor dst(output_shape, dst_data_type); // Iterate over every pixel of the input image - for(size_t px = 0; px < pool_info.pooled_width(); px++) + for(size_t px = 0; px < pool_info.pooled_width(); ++px) { - for(size_t py = 0; py < pool_info.pooled_height(); py++) + for(size_t py = 0; py < pool_info.pooled_height(); ++py) { - for(size_t pw = 0; pw < num_rois; pw++) + for(size_t pw = 0; pw < num_rois; ++pw) { - ROI roi = rois[pw]; - const int roi_batch = roi.batch_idx; + const unsigned int roi_batch = rois_ptr[values_per_roi * pw]; + const auto x1 = float(rois_ptr[values_per_roi * pw + 1]); + const auto y1 = float(rois_ptr[values_per_roi * pw + 2]); + const auto x2 = float(rois_ptr[values_per_roi * pw + 3]); + const auto y2 = float(rois_ptr[values_per_roi * pw + 4]); - const float roi_anchor_x = roi.rect.x * pool_info.spatial_scale(); - const float roi_anchor_y = roi.rect.y * pool_info.spatial_scale(); - const float roi_dims_x = std::max(roi.rect.width * pool_info.spatial_scale(), 1.0f); - const float roi_dims_y = std::max(roi.rect.height * pool_info.spatial_scale(), 1.0f); - ; + const float roi_anchor_x = x1 * pool_info.spatial_scale(); + const float roi_anchor_y = y1 * pool_info.spatial_scale(); + const float roi_dims_x = std::max((x2 - x1) * pool_info.spatial_scale(), 1.0f); + const float roi_dims_y = std::max((y2 - y1) * pool_info.spatial_scale(), 1.0f); float bin_size_x = roi_dims_x / pool_info.pooled_width(); float bin_size_y = roi_dims_y / pool_info.pooled_height(); @@ -178,8 +183,8 @@ SimpleTensor roi_align_layer(const SimpleTensor &src, const std::vector roi_align_layer(const SimpleTensor &src, const std::vector &rois, const ROIPoolingLayerInfo &pool_info); -template SimpleTensor roi_align_layer(const SimpleTensor &src, const std::vector &rois, const ROIPoolingLayerInfo &pool_info); +template SimpleTensor roi_align_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info); +template SimpleTensor roi_align_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info); } // namespace reference } // namespace validation } // namespace test diff --git a/tests/validation/reference/ROIAlignLayer.h b/tests/validation/reference/ROIAlignLayer.h index 818f9b147c..b67ff42166 100644 --- a/tests/validation/reference/ROIAlignLayer.h +++ b/tests/validation/reference/ROIAlignLayer.h @@ -37,7 +37,7 @@ namespace validation namespace reference { template -SimpleTensor roi_align_layer(const SimpleTensor &src, const std::vector &rois, const ROIPoolingLayerInfo &pool_info); +SimpleTensor roi_align_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info); } // namespace reference } // namespace validation } // namespace test -- cgit v1.2.1