From ab8408872f49c9429c84d83de665c55e31a500b2 Mon Sep 17 00:00:00 2001 From: Suhail Munshi Date: Tue, 9 Feb 2021 16:31:00 +0000 Subject: Added Qasymm8 datatype support to NEROIPoolingLayer with Tests Tests added to check ROIPooling Layer against reference with both Float32 and Qasymm8 input. Resolves : COMPMID-2319 Change-Id: I867bc4dde1e3e91f9f42f4a7ce8debfe83b8db50 Signed-off-by: Mohammed Suhail Munshi Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/296640 Tested-by: bsgcomp Reviewed-by: Pablo Tello Comments-Addressed: Pablo Tello Signed-off-by: Suhail Munshi Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/5060 Tested-by: Arm Jenkins Reviewed-by: Pablo Marquez Tello Comments-Addressed: Arm Jenkins --- .../runtime/NEON/functions/NEROIPoolingLayer.h | 23 ++- src/core/NEON/kernels/NEROIPoolingLayerKernel.cpp | 130 +++++++++---- src/core/NEON/kernels/NEROIPoolingLayerKernel.h | 24 ++- src/runtime/NEON/functions/NEROIPoolingLayer.cpp | 10 +- tests/validation/NEON/ROIPoolingLayer.cpp | 142 +++++++++++++++ tests/validation/fixtures/ROIPoolingLayerFixture.h | 202 +++++++++++++++++++++ tests/validation/reference/ROIPoolingLayer.cpp | 147 +++++++++++++++ tests/validation/reference/ROIPoolingLayer.h | 46 +++++ 8 files changed, 678 insertions(+), 46 deletions(-) create mode 100644 tests/validation/NEON/ROIPoolingLayer.cpp create mode 100644 tests/validation/fixtures/ROIPoolingLayerFixture.h create mode 100644 tests/validation/reference/ROIPoolingLayer.cpp create mode 100644 tests/validation/reference/ROIPoolingLayer.h diff --git a/arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h b/arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h index a912669f57..510c89caf2 100644 --- a/arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h +++ b/arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h @@ -24,14 +24,14 @@ #ifndef ARM_COMPUTE_NEROIPOOLINGLAYER_H #define ARM_COMPUTE_NEROIPOOLINGLAYER_H -#include "arm_compute/runtime/IFunction.h" - #include "arm_compute/core/IArray.h" +#include "arm_compute/runtime/IFunction.h" #include namespace arm_compute { class ITensor; +class ITensorInfo; class NEROIPoolingLayerKernel; class ROIPoolingLayerInfo; @@ -58,7 +58,7 @@ public: ~NEROIPoolingLayer(); /** Set the input and output tensors. * - * @param[in] input Source tensor. Data types supported: F32. + * @param[in] input Source tensor. Data types supported: QASYMM8/F32 * @param[in] rois ROIs tensor, it is a 2D tensor of size [5, N] (where N is the number of ROIs) containing top left and bottom right corner * as coordinate of an image and batch_id of ROI [ batch_id, x1, y1, x2, y2 ]. Data types supported: U16 * @param[out] output Destination tensor. Data types supported: Same as @p input. @@ -69,11 +69,26 @@ public: * @note The z dimensions of @p output tensor and @p input tensor must be the same. * @note The fourth dimension of @p output tensor must be the same as the number of elements in @p rois array. */ - void configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info); + void configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info); // Inherited methods overridden: void run() override; + /** Static function to check if given info will lead to a valid configuration of @ref NEROIPoolingLayerKernel + * + * @param[in] input Source tensor info. Data types supported: QASYMM8/F32. + * @param[in] rois TensorInfo for rois tensor which is a 2D tensor of size [5,N] (where 5 is the number ROIs). Data types supported: U16 + * @param[in] output Destination tensor info. Data types supported: Same as @p input. + * @param[in] pool_info Contains pooling operation information described in @ref ROIPoolingLayerInfo. + * + * @note The x and y dimensions of @p output tensor must be the same as that specified by @p pool_info 's pooled + * width and pooled height. + * @note The z dimensions of @p output tensor and @p input tensor must be the same. + * @note The fourth dimension of @p output tensor must be the same as the number of elements in @p rois array. + * @return a Status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info); + private: std::unique_ptr _roi_kernel; }; diff --git a/src/core/NEON/kernels/NEROIPoolingLayerKernel.cpp b/src/core/NEON/kernels/NEROIPoolingLayerKernel.cpp index 9a3a757f1c..400e8291d6 100644 --- a/src/core/NEON/kernels/NEROIPoolingLayerKernel.cpp +++ b/src/core/NEON/kernels/NEROIPoolingLayerKernel.cpp @@ -22,7 +22,6 @@ * SOFTWARE. */ #include "src/core/NEON/kernels/NEROIPoolingLayerKernel.h" - #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Validate.h" #include "arm_compute/core/Window.h" @@ -35,35 +34,101 @@ namespace arm_compute { +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, rois); + + //Validate arguments + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(rois, DataType::U16); + ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5); + ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32, DataType::QASYMM8); + ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); + + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height())); + ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2)); + ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3)); + } + + return Status{}; +} + +/** Evaluate number needing to be stored in output tensor as quantized format. + * + * @param[in] input Source tensor. Data types supported: QASYMM8 + * @param[out] output Destination tensor. Where output value will be stored, same datatype as input + * @param[in] region_start_x Beginning region of x coordinate of pooling region + * @param[in] region_start_y Beginning region of y coordinate of pooling region + * @param[in] region_end_x End of pooling region, x coordinate + * @param[in] region_end_y End of pooling region, y coordinate + * @param[in] fm Channel index of coordinate in output Tensor to store value + * @param[in] px Width index of coodinate in output Tensor to store value + * @param[in] py Height index of coordinate in output Tensor to store value + * @param[in] roi_batch Index of image to perform Pooling on in input Tensor + * @param[in] roi_indx Index of image of coordinate in output Tensor to store value + */ +template +void template_eval(const ITensor *input, const ITensor *output, int region_start_x, int region_start_y, + int region_end_x, int region_end_y, int fm, int px, int py, int roi_batch, int roi_indx) +{ + if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) + { + *reinterpret_cast(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0; + } + else + { + T curr_max = std::numeric_limits::lowest(); // Min value of typename T + for(int j = region_start_y; j < region_end_y; ++j) + { + for(int i = region_start_x; i < region_end_x; ++i) + { + const auto val = *reinterpret_cast(input->ptr_to_element(Coordinates(i, j, fm, roi_batch))); + curr_max = std::max(val, curr_max); + } + } + + // if quantized datatype, requantize then store in output tensor + if(is_data_type_quantized(input->info()->data_type())) + { + // covert qasymm to new output quantization scale and offset + UniformQuantizationInfo uqinfo = compute_requantization_scale_offset(input->info()->quantization_info().uniform(), output->info()->quantization_info().uniform()); + *reinterpret_cast(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = quantize_qasymm8(curr_max, uqinfo); + } + else + { + *reinterpret_cast(output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max; + } + } +} +} // namespace + NEROIPoolingLayerKernel::NEROIPoolingLayerKernel() : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) { } -void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) +Status NEROIPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info)); + return Status{}; +} + +void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info) { ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); //Validate arguments - ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info()); - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16); - ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5); - ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2); - ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(input); - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); - - if(output->info()->total_size() != 0) - { - ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); - ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2)); - ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3)); - } + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info)); // Output auto initialization if not yet initialized TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1)); - auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); + + auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), output->info()->quantization_info()); ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); @@ -99,7 +164,8 @@ void NEROIPoolingLayerKernel::run(const Window &window, const ThreadInfo &info) const int pooled_h = _pool_info.pooled_height(); const float spatial_scale = _pool_info.spatial_scale(); - const auto *rois_ptr = reinterpret_cast(_rois->buffer()); + const auto *rois_ptr = reinterpret_cast(_rois->buffer()); + const auto data_type = _input->info()->data_type(); for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx) { @@ -133,23 +199,17 @@ void NEROIPoolingLayerKernel::run(const Window &window, const ThreadInfo &info) region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height); region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height); - // Iterate through the pooling region - if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) - { - *reinterpret_cast(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0; - } - else + switch(data_type) { - float curr_max = -FLT_MAX; - for(int j = region_start_y; j < region_end_y; ++j) - { - for(int i = region_start_x; i < region_end_x; ++i) - { - const auto val = *reinterpret_cast(_input->ptr_to_element(Coordinates(i, j, fm, roi_batch))); - curr_max = std::max(val, curr_max); - } - } - *reinterpret_cast(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max; + case DataType::F32: + template_eval(_input, _output, region_start_x, region_start_y, region_end_x, region_end_y, fm, px, py, roi_batch, roi_indx); + break; + case DataType::QASYMM8: + template_eval(_input, _output, region_start_x, region_start_y, region_end_x, region_end_y, fm, px, py, roi_batch, roi_indx); + break; + default: + ARM_COMPUTE_ERROR("DataType not Supported"); + break; } } } diff --git a/src/core/NEON/kernels/NEROIPoolingLayerKernel.h b/src/core/NEON/kernels/NEROIPoolingLayerKernel.h index 36424172a6..2fcdb81eb6 100644 --- a/src/core/NEON/kernels/NEROIPoolingLayerKernel.h +++ b/src/core/NEON/kernels/NEROIPoolingLayerKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Arm Limited. + * Copyright (c) 2017-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -55,7 +55,7 @@ public: /** Set the input and output tensors. * - * @param[in] input Source tensor. Data types supported: F32. + * @param[in] input Source tensor. Data types supported: QASYMM8/F32 * @param[in] rois ROIs tensor, it is a 2D tensor of size [5, N] (where N is the number of ROIs) containing top left and bottom right corner * as coordinate of an image and batch_id of ROI [ batch_id, x1, y1, x2, y2 ]. Data types supported: U16 * @param[out] output Destination tensor. Data types supported: Same as @p input. @@ -66,15 +66,31 @@ public: * @note The z dimensions of @p output tensor and @p input tensor must be the same. * @note The fourth dimension of @p output tensor must be the same as the number of elements in @p rois tensor. */ - void configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info); + void configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info); // Inherited methods overridden: void run(const Window &window, const ThreadInfo &info) override; + /** Static function to check if given info will lead to a valid configuration of @ref NEROIPoolingLayerKernel + * + * @param[in] input Source tensor info. Data types supported: QASYMM8/F32. + * @param[in] rois ROIs tensor info. Data types supported: U16 + * @param[in] output Destination tensor info. Data types supported: Same as @p input. + * @param[in] pool_info Contains pooling operation information described in @ref ROIPoolingLayerInfo. + * + * @note The x and y dimensions of @p output tensor must be the same as @p pool_info 's pooled + * width and pooled height. + * @note The datatype of @p output should be the same as the datatype of @p input + * @note The fourth dimension of @p output tensor must be the same as the number of elements in @p rois array. + * + * @return a Status + */ + static Status validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info); + private: const ITensor *_input; const ITensor *_rois; - ITensor *_output; + const ITensor *_output; ROIPoolingLayerInfo _pool_info; }; } // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEROIPoolingLayer.cpp b/src/runtime/NEON/functions/NEROIPoolingLayer.cpp index 7ca6ecc737..f9434059ea 100644 --- a/src/runtime/NEON/functions/NEROIPoolingLayer.cpp +++ b/src/runtime/NEON/functions/NEROIPoolingLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Arm Limited. + * Copyright (c) 2017-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -22,7 +22,6 @@ * SOFTWARE. */ #include "arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h" - #include "arm_compute/core/Helpers.h" #include "arm_compute/runtime/NEON/NEScheduler.h" #include "src/core/NEON/kernels/NEROIPoolingLayerKernel.h" @@ -36,7 +35,12 @@ NEROIPoolingLayer::NEROIPoolingLayer() { } -void NEROIPoolingLayer::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) +Status NEROIPoolingLayer::validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + return NEROIPoolingLayerKernel::validate(input, rois, output, pool_info); +} + +void NEROIPoolingLayer::configure(const ITensor *input, const ITensor *rois, const ITensor *output, const ROIPoolingLayerInfo &pool_info) { _roi_kernel = std::make_unique(); _roi_kernel->configure(input, rois, output, pool_info); diff --git a/tests/validation/NEON/ROIPoolingLayer.cpp b/tests/validation/NEON/ROIPoolingLayer.cpp new file mode 100644 index 0000000000..8b5147e57f --- /dev/null +++ b/tests/validation/NEON/ROIPoolingLayer.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2021 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. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/Globals.h" +#include "tests/NEON/Accessor.h" +#include "tests/datasets/ROIDataset.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/ROIPoolingLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +RelativeTolerance relative_tolerance_f32(0.01f); +AbsoluteTolerance absolute_tolerance_f32(0.001f); + +constexpr AbsoluteTolerance tolerance_qasymm8(1); +} // end namespace + +TEST_SUITE(NEON) +TEST_SUITE(RoiPooling) + +// *INDENT-OFF* +// 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), // Successful test + TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::QASYMM8), // Successful test (quantized) + TensorInfo(TensorShape(250U, 128U, 3U), 1, DataType::F32), // Incorrect rois type + 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, 3U), 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, 3U), 1, DataType::F32), // Mismatching height and width input/output + + }), + framework::dataset::make("RoisInfo", { TensorInfo(TensorShape(5, 4U), 1, DataType::U16), + TensorInfo(TensorShape(5, 4U), 1, DataType::U16), + TensorInfo(TensorShape(5, 4U), 1, DataType::F16), + TensorInfo(TensorShape(5, 4U), 1, DataType::U16), + TensorInfo(TensorShape(5, 4U), 1, DataType::U16), + TensorInfo(TensorShape(5, 10U), 1, DataType::U16), + TensorInfo(TensorShape(4, 4U), 1, DataType::U16), + TensorInfo(TensorShape(5, 4U), 1, DataType::U16), + })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::QASYMM8), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F16), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(7U, 7U, 3U, 4U), 1, DataType::F32), + TensorInfo(TensorShape(5U, 5U, 3U, 4U), 1, DataType::F32), + })), + framework::dataset::make("PoolInfo", { 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), + ROIPoolingLayerInfo(7U, 7U, 1./8), + ROIPoolingLayerInfo(7U, 7U, 1./8), + ROIPoolingLayerInfo(7U, 7U, 1./8), + })), + framework::dataset::make("Expected", { true, true, false, false, false, false, false })), + input_info, rois_info, output_info, pool_info, expected) +{ + ARM_COMPUTE_EXPECT(bool(NEROIPoolingLayer::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* + +using NEROIPoolingLayerFloatFixture = ROIPoolingLayerFixture; + +TEST_SUITE(Float) +FIXTURE_DATA_TEST_CASE(SmallROIPoolingLayerFloat, NEROIPoolingLayerFloatFixture, framework::DatasetMode::ALL, + framework::dataset::combine(framework::dataset::combine(datasets::SmallROIDataset(), + framework::dataset::make("DataType", { DataType::F32 })), + framework::dataset::make("DataLayout", { DataLayout::NCHW }))) +{ + // Validate output + validate(Accessor(_target), _reference, relative_tolerance_f32, .02f, absolute_tolerance_f32); +} + +TEST_SUITE_END() // Float test suite end + +// Begin quantized tests +TEST_SUITE(Quantized) +template +using NEROIPoolingLayerQuantizedFixture = ROIPoolingLayerQuantizedFixture; + +TEST_SUITE(QASYMM8) + +FIXTURE_DATA_TEST_CASE(Small, NEROIPoolingLayerQuantizedFixture, framework::DatasetMode::ALL, + combine(combine(combine(combine(datasets::SmallROIDataset(), + framework::dataset::make("DataType", { DataType::QASYMM8 })), + framework::dataset::make("DataLayout", { DataLayout::NCHW })), + framework::dataset::make("InputQuantizationInfo", { QuantizationInfo(1.f / 255.f, 127) })), + framework::dataset::make("OutputQuantizationInfo", { QuantizationInfo(2.f / 255.f, 120) }))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance_qasymm8); +} + +TEST_SUITE_END() // end qasymm8 tests +TEST_SUITE_END() // end quantized tests + +TEST_SUITE_END() // RoiPooling +TEST_SUITE_END() // NEON + +} // validation end +} // test namespace end +} // arm_compute namespace end diff --git a/tests/validation/fixtures/ROIPoolingLayerFixture.h b/tests/validation/fixtures/ROIPoolingLayerFixture.h new file mode 100644 index 0000000000..c32e7af180 --- /dev/null +++ b/tests/validation/fixtures/ROIPoolingLayerFixture.h @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2021 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_ROIPOOLINGLAYER_FIXTURE +#define ARM_COMPUTE_TEST_ROIPOOLINGLAYER_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/ROIPoolingLayer.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class ROIPoolingLayerGenericFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo) + { + _target = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape, qinfo, output_qinfo); + _reference = compute_reference(input_shape, data_type, pool_info, rois_shape, qinfo, output_qinfo); + } + +protected: + template + void fill(U &&tensor) + { + library->fill_tensor_uniform(tensor, 0); + } + + template + void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape, DataLayout data_layout = DataLayout::NCHW) + { + 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[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width); + const auto scaled_height = static_cast((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / 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] = static_cast(x1); + rois_ptr[values_per_roi * pw + 2] = static_cast(y1); + rois_ptr[values_per_roi * pw + 3] = static_cast(x2); + rois_ptr[values_per_roi * pw + 4] = static_cast(y2); + } + } + + TensorType compute_target(TensorShape input_shape, + DataType data_type, + DataLayout data_layout, + const ROIPoolingLayerInfo &pool_info, + const TensorShape rois_shape, + const QuantizationInfo &qinfo, + const QuantizationInfo &output_qinfo) + { + const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo(); + + // Create tensors + TensorType src = create_tensor(input_shape, data_type, 1, qinfo, data_layout); + TensorType rois_tensor = create_tensor(rois_shape, _rois_data_type, 1, rois_qinfo); + + // Initialise shape and declare output tensor dst + const TensorShape dst_shape; + TensorType dst = create_tensor(dst_shape, data_type, 1, output_qinfo, data_layout); + + // Create and configure function + FunctionType roi_pool_layer; + roi_pool_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, data_layout); + + // Compute function + roi_pool_layer.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &input_shape, + DataType data_type, + const ROIPoolingLayerInfo &pool_info, + const TensorShape rois_shape, + const QuantizationInfo &qinfo, + const QuantizationInfo &output_qinfo) + { + // Create reference tensor + SimpleTensor src{ input_shape, data_type, 1, qinfo }; + const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo(); + SimpleTensor rois_tensor{ rois_shape, _rois_data_type, 1, rois_qinfo }; + + // Fill reference tensor + fill(src); + generate_rois(rois_tensor, input_shape, pool_info, rois_shape); + + return reference::roi_pool_layer(src, rois_tensor, pool_info, output_qinfo); + } + + TensorType _target{}; + SimpleTensor _reference{}; + const DataType _rois_data_type{ DataType::U16 }; +}; + +template +class ROIPoolingLayerQuantizedFixture : public ROIPoolingLayerGenericFixture +{ +public: + template + void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, + DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo) + { + ROIPoolingLayerGenericFixture::setup(input_shape, pool_info, rois_shape, + data_type, data_layout, qinfo, output_qinfo); + } +}; + +template +class ROIPoolingLayerFixture : public ROIPoolingLayerGenericFixture +{ +public: + template + void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout) + { + ROIPoolingLayerGenericFixture::setup(input_shape, pool_info, rois_shape, data_type, data_layout, + QuantizationInfo(), QuantizationInfo()); + } +}; + +} // namespace validation +} // namespace test +} // namespace arm_compute + +#endif /* ARM_COMPUTE_TEST_ROIPOOLINGLAYER_FIXTURE */ \ No newline at end of file diff --git a/tests/validation/reference/ROIPoolingLayer.cpp b/tests/validation/reference/ROIPoolingLayer.cpp new file mode 100644 index 0000000000..8dc3014763 --- /dev/null +++ b/tests/validation/reference/ROIPoolingLayer.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "ROIPoolingLayer.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/validation/Helpers.h" +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <> +SimpleTensor roi_pool_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) +{ + ARM_COMPUTE_UNUSED(output_qinfo); + + const size_t num_rois = rois.shape()[1]; + const size_t values_per_roi = rois.shape()[0]; + DataType output_data_type = src.data_type(); + + TensorShape input_shape = src.shape(); + TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois); + SimpleTensor output(output_shape, output_data_type); + + const int pooled_w = pool_info.pooled_width(); + const int pooled_h = pool_info.pooled_height(); + const float spatial_scale = pool_info.spatial_scale(); + + // get sizes of x and y dimensions in src tensor + const int width = src.shape()[0]; + const int height = src.shape()[1]; + + // Move pointer across the fourth dimension + const size_t input_stride_w = input_shape[0] * input_shape[1] * input_shape[2]; + const size_t output_stride_w = output_shape[0] * output_shape[1] * output_shape[2]; + + const auto *rois_ptr = reinterpret_cast(rois.data()); + + // Iterate through pixel width (X-Axis) + for(size_t pw = 0; pw < num_rois; ++pw) + { + const unsigned int roi_batch = rois_ptr[values_per_roi * pw]; + const auto x1 = rois_ptr[values_per_roi * pw + 1]; + const auto y1 = rois_ptr[values_per_roi * pw + 2]; + const auto x2 = rois_ptr[values_per_roi * pw + 3]; + const auto y2 = rois_ptr[values_per_roi * pw + 4]; + + //Iterate through pixel height (Y-Axis) + for(size_t fm = 0; fm < input_shape[2]; ++fm) + { + // Iterate through regions of interest index + for(size_t py = 0; py < pool_info.pooled_height(); ++py) + { + // Scale ROI + const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale); + const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale); + const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f); + const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f); + + // Iterate over feature map (Z axis) + for(size_t px = 0; px < pool_info.pooled_width(); ++px) + { + auto region_start_x = static_cast(std::floor((static_cast(px) / pooled_w) * roi_width)); + auto region_end_x = static_cast(std::floor((static_cast(px + 1) / pooled_w) * roi_width)); + auto region_start_y = static_cast(std::floor((static_cast(py) / pooled_h) * roi_height)); + auto region_end_y = static_cast(std::floor((static_cast(py + 1) / pooled_h) * roi_height)); + + region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width); + region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width); + region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height); + region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height); + + // Iterate through the pooling region + if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) + { + /* Assign element in tensor 'output' at coordinates px, py, fm, roi_indx, to 0 */ + auto out_ptr = output.data() + px + py * output_shape[0] + fm * output_shape[0] * output_shape[1] + pw * output_stride_w; + *out_ptr = 0; + } + else + { + float curr_max = -std::numeric_limits::max(); + for(int j = region_start_y; j < region_end_y; ++j) + { + for(int i = region_start_x; i < region_end_x; ++i) + { + /* Retrieve element from input tensor at coordinates(i, j, fm, roi_batch) */ + float in_element = *(src.data() + i + j * input_shape[0] + fm * input_shape[0] * input_shape[1] + roi_batch * input_stride_w); + curr_max = std::max(in_element, curr_max); + } + } + + /* Assign element in tensor 'output' at coordinates px, py, fm, roi_indx, to curr_max */ + auto out_ptr = output.data() + px + py * output_shape[0] + fm * output_shape[0] * output_shape[1] + pw * output_stride_w; + *out_ptr = curr_max; + } + } + } + } + } + + return output; +} + +/* + Template genericised method to allow calling of roi_pooling_layer with quantized 8 bit datatype +*/ +template <> +SimpleTensor roi_pool_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) +{ + const SimpleTensor src_tmp = convert_from_asymmetric(src); + SimpleTensor dst_tmp = roi_pool_layer(src_tmp, rois, pool_info, output_qinfo); + SimpleTensor dst = convert_to_asymmetric(dst_tmp, output_qinfo); + return dst; +} + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute \ No newline at end of file diff --git a/tests/validation/reference/ROIPoolingLayer.h b/tests/validation/reference/ROIPoolingLayer.h new file mode 100644 index 0000000000..ddbaee2d5e --- /dev/null +++ b/tests/validation/reference/ROIPoolingLayer.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 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_ROIPOOLLAYER_H +#define ARM_COMPUTE_TEST_ROIPOOLLAYER_H + +#include "arm_compute/core/Types.h" +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor roi_pool_layer(const SimpleTensor &src, const SimpleTensor &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute + +#endif /* ARM_COMPUTE_TEST_ROIPOOLLAYER_H */ \ No newline at end of file -- cgit v1.2.1