From d75f9e9a7bd192a88631972ccd82254059ce4a9d Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Fri, 23 Aug 2019 16:26:26 +0100 Subject: COMPMID-2318: Implement NEROIAlignLayer Change-Id: I2665c98b5d6523a9ddaef0b7c220aecd22559ce6 Signed-off-by: Pablo Tello Reviewed-on: https://review.mlplatform.org/c/1826 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Michele Di Giorgio --- arm_compute/core/NEON/NEKernels.h | 1 + .../core/NEON/kernels/NEROIAlignLayerKernel.h | 99 +++++++ arm_compute/runtime/NEON/NEFunctions.h | 1 + .../runtime/NEON/functions/NEROIAlignLayer.h | 74 +++++ src/core/NEON/kernels/NEROIAlignLayerKernel.cpp | 326 +++++++++++++++++++++ src/runtime/NEON/functions/NEROIAlignLayer.cpp | 46 +++ tests/validation/NEON/ROIAlignLayer.cpp | 134 +++++++++ 7 files changed, 681 insertions(+) create mode 100644 arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h create mode 100644 arm_compute/runtime/NEON/functions/NEROIAlignLayer.h create mode 100644 src/core/NEON/kernels/NEROIAlignLayerKernel.cpp create mode 100644 src/runtime/NEON/functions/NEROIAlignLayer.cpp create mode 100644 tests/validation/NEON/ROIAlignLayer.cpp diff --git a/arm_compute/core/NEON/NEKernels.h b/arm_compute/core/NEON/NEKernels.h index bf77e576ad..5e3389744f 100644 --- a/arm_compute/core/NEON/NEKernels.h +++ b/arm_compute/core/NEON/NEKernels.h @@ -118,6 +118,7 @@ #include "arm_compute/core/NEON/kernels/NEPoolingLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEQuantizationLayerKernel.h" +#include "arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h" #include "arm_compute/core/NEON/kernels/NEROIPoolingLayerKernel.h" #include "arm_compute/core/NEON/kernels/NERangeKernel.h" #include "arm_compute/core/NEON/kernels/NEReductionOperationKernel.h" diff --git a/arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h b/arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h new file mode 100644 index 0000000000..00c6f07cb5 --- /dev/null +++ b/arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2019 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_NEROIALIGNLAYERKERNEL_H__ +#define __ARM_COMPUTE_NEROIALIGNLAYERKERNEL_H__ + +#include "arm_compute/core/NEON/INEKernel.h" + +namespace arm_compute +{ +class ITensor; + +/** Interface for the RoIAlign kernel. + */ +class NEROIAlignLayerKernel : public INEKernel +{ +public: + const char *name() const override + { + return "NEROIAlignLayerKernel"; + } + + /** Constructor */ + NEROIAlignLayerKernel(); + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEROIAlignLayerKernel(const NEROIAlignLayerKernel &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + NEROIAlignLayerKernel &operator=(const NEROIAlignLayerKernel &) = delete; + /** Default Move Constructor. */ + NEROIAlignLayerKernel(NEROIAlignLayerKernel &&) = default; + /** Default move assignment operator. */ + NEROIAlignLayerKernel &operator=(NEROIAlignLayerKernel &&) = default; + /** Default destructor */ + ~NEROIAlignLayerKernel() = default; + + /** Set the input and output tensors. + * + * @param[in] input Source tensor. Data types supported: F16/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: same as @p input + * @param[out] output Destination tensor. 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 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); + /** Static function to check if given info will lead to a valid configuration of @ref NEROIAlignLayerKernel + * + * @param[in] input Source tensor info. Data types supported: F16/F32. + * @param[in] rois ROIs tensor info. Data types supported: same as @p input + * @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 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, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info); + + // Inherited methods overridden: + void run(const Window &window, const ThreadInfo &info) override; + +private: + template + void internal_run(const Window &window, const ThreadInfo &info); + + const ITensor *_input; + ITensor *_output; + const ITensor *_rois; + ROIPoolingLayerInfo _pool_info; +}; +} // namespace arm_compute +#endif /*__ARM_COMPUTE_NEROIALIGNLAYERKERNEL_H__*/ diff --git a/arm_compute/runtime/NEON/NEFunctions.h b/arm_compute/runtime/NEON/NEFunctions.h index 716d28cff5..515e4070f4 100644 --- a/arm_compute/runtime/NEON/NEFunctions.h +++ b/arm_compute/runtime/NEON/NEFunctions.h @@ -116,6 +116,7 @@ #include "arm_compute/runtime/NEON/functions/NEPriorBoxLayer.h" #include "arm_compute/runtime/NEON/functions/NEQuantizationLayer.h" #include "arm_compute/runtime/NEON/functions/NERNNLayer.h" +#include "arm_compute/runtime/NEON/functions/NEROIAlignLayer.h" #include "arm_compute/runtime/NEON/functions/NEROIPoolingLayer.h" #include "arm_compute/runtime/NEON/functions/NERange.h" #include "arm_compute/runtime/NEON/functions/NEReduceMean.h" diff --git a/arm_compute/runtime/NEON/functions/NEROIAlignLayer.h b/arm_compute/runtime/NEON/functions/NEROIAlignLayer.h new file mode 100644 index 0000000000..f28fb6b2be --- /dev/null +++ b/arm_compute/runtime/NEON/functions/NEROIAlignLayer.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019 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_NEROIALIGNLAYER_H__ +#define __ARM_COMPUTE_NEROIALIGNLAYER_H__ + +#include "arm_compute/core/NEON/kernels/NEROIPoolingLayerKernel.h" +#include "arm_compute/runtime/NEON/INESimpleFunction.h" + +namespace arm_compute +{ +class ITensor; + +/** Basic function to run @ref NEROIAlignLayerKernel. + * + * This function calls the following NEON kernels: + * -# @ref NEROIAlignLayerKernel + * + */ +class NEROIAlignLayer : public INESimpleFunction +{ +public: + /** Set the input and output tensors. + * + * @param[in] input Source tensor. Data types supported: F16/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: same as @p input + * @param[out] output Destination tensor. 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 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); + /** Static function to check if given info will lead to a valid configuration of @ref NEROIAlignLayer + * + * @param[in] input Source tensor info. Data types supported: F16/F32. + * @param[in] rois ROIs tensor info. Data types supported: same as @p input + * @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 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, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info); +}; +} // namespace arm_compute +#endif /* __ARM_COMPUTE_NEROIALIGNLAYER_H__ */ diff --git a/src/core/NEON/kernels/NEROIAlignLayerKernel.cpp b/src/core/NEON/kernels/NEROIAlignLayerKernel.cpp new file mode 100644 index 0000000000..dd21094832 --- /dev/null +++ b/src/core/NEON/kernels/NEROIAlignLayerKernel.cpp @@ -0,0 +1,326 @@ +/* + * Copyright (c) 2019 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/NEON/kernels/NEROIAlignLayerKernel.h" + +#include "arm_compute/core/AccessWindowStatic.h" +#include "arm_compute/core/CPP/Validate.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Utils.h" +#include "arm_compute/core/Window.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/core/utils/misc/Utility.h" + +#include + +using namespace arm_compute::misc::shape_calculator; + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois); + 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_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW); + ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); + ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); + + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape()); + } + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + + // Output auto inizialitation if not yet initialized + const TensorShape output_shape = compute_roi_align_shape(*input, *rois, pool_info); + auto_init_if_empty((*output), output_shape, 1, input->data_type()); + output->set_data_layout(input->data_layout()); + + const unsigned int num_rois = rois->dimension(1); + Window window; + window.set(Window::DimX, Window::Dimension(0, num_rois)); + window.set(Window::DimY, Window::Dimension(0, 1)); + + AccessWindowStatic input_access(input, + input->valid_region().start(0), + input->valid_region().start(1), + input->valid_region().end(0), + input->valid_region().end(1)); + AccessWindowStatic output_access(output, 0, 0, pool_info.pooled_width(), pool_info.pooled_height()); + + const bool window_changed = update_window_and_padding(window, input_access, output_access); + output_access.set_valid_region(window, ValidRegion(Coordinates(), output->tensor_shape())); + + Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; + return std::make_pair(err, window); +} +} // namespace + +NEROIAlignLayerKernel::NEROIAlignLayerKernel() + : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f) +{ +} + +void NEROIAlignLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info)); + // Configure kernel window + auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + + // Set instance variables + _input = input; + _rois = rois; + _output = output; + _pool_info = pool_info; + + INEKernel::configure(win_config.second); +} + +Status NEROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info)); + return Status{}; +} + +/** Average pooling over an aligned window */ +template +inline T roi_align_1x1(const ITensor *input, unsigned int roi_batch, + float region_start_x, + float bin_size_x, + int grid_size_x, + float region_end_x, + float region_start_y, + float bin_size_y, + int grid_size_y, + float region_end_y, + int pz) +{ + if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) + { + return T(0); + } + else + { + float avg = 0; + // Iterate through the aligned pooling region + for(int iy = 0; iy < grid_size_y; ++iy) + { + for(int ix = 0; ix < grid_size_x; ++ix) + { + // Align the window in the middle of every bin + float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y); + float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x); + + // Interpolation in the [0,0] [0,1] [1,0] [1,1] square + const int y_low = y; + const int x_low = x; + const int y_high = y_low + 1; + const int x_high = x_low + 1; + + const float ly = y - y_low; + const float lx = x - x_low; + const float hy = 1. - ly; + const float hx = 1. - lx; + + const float w1 = hy * hx; + const float w2 = hy * lx; + const float w3 = ly * hx; + const float w4 = ly * lx; + if(data_layout == DataLayout::NCHW) + { + const auto data1 = *reinterpret_cast(input->ptr_to_element(Coordinates(x_low, y_low, pz, roi_batch))); + const auto data2 = *reinterpret_cast(input->ptr_to_element(Coordinates(x_high, y_low, pz, roi_batch))); + const auto data3 = *reinterpret_cast(input->ptr_to_element(Coordinates(x_low, y_high, pz, roi_batch))); + const auto data4 = *reinterpret_cast(input->ptr_to_element(Coordinates(x_high, y_high, pz, roi_batch))); + avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4; + } + else + { + const auto data1 = *reinterpret_cast(input->ptr_to_element(Coordinates(pz, x_low, y_low, roi_batch))); + const auto data2 = *reinterpret_cast(input->ptr_to_element(Coordinates(pz, x_high, y_low, roi_batch))); + const auto data3 = *reinterpret_cast(input->ptr_to_element(Coordinates(pz, x_low, y_high, roi_batch))); + const auto data4 = *reinterpret_cast(input->ptr_to_element(Coordinates(pz, x_high, y_high, roi_batch))); + avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4; + } + } + } + + avg /= grid_size_x * grid_size_y; + + return T(avg); + } +} + +inline float compute_region_coordinate(int p, float bin_size, float roi_anchor, float max_value) +{ + const float region_start = p * bin_size + roi_anchor; + return utility::clamp(region_start, 0.0f, max_value); +} + +void NEROIAlignLayerKernel::run(const Window &window, const ThreadInfo &info) +{ + if(_input->info()->data_layout() == DataLayout::NCHW) + { + switch(_input->info()->data_type()) + { + case DataType::F32: + { + NEROIAlignLayerKernel::internal_run(window, info); + break; + } +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + { + NEROIAlignLayerKernel::internal_run(window, info); + break; + } +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + default: + { + ARM_COMPUTE_ERROR("DataType not supported"); + break; + } + } + } + else if(_input->info()->data_layout() == DataLayout::NHWC) + { + switch(_input->info()->data_type()) + { + case DataType::F32: + { + NEROIAlignLayerKernel::internal_run(window, info); + break; + } +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + { + NEROIAlignLayerKernel::internal_run(window, info); + break; + } +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + default: + { + ARM_COMPUTE_ERROR("DataType not supported"); + break; + } + } + } + else + { + ARM_COMPUTE_ERROR("Invalid layout"); + } +} + +template +void NEROIAlignLayerKernel::internal_run(const Window &window, const ThreadInfo &info) +{ + ARM_COMPUTE_UNUSED(info); + ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); + ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); + + const size_t values_per_roi = _rois->info()->dimension(0); + + const int roi_list_start = window.x().start(); + const int roi_list_end = window.x().end(); + + const unsigned int idx_width = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::WIDTH); + const unsigned int idx_height = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::HEIGHT); + const unsigned int idx_depth = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL); + + const int input_width = _input->info()->dimension(idx_width); + const int input_height = _input->info()->dimension(idx_height); + const int input_chanels = _input->info()->dimension(idx_depth); + const int pooled_w = _pool_info.pooled_width(); + const int pooled_h = _pool_info.pooled_height(); + + const auto *rois_ptr = reinterpret_cast(_rois->buffer()); + + for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx) + { + const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx]; + const auto x1 = rois_ptr[values_per_roi * roi_indx + 1]; + const auto y1 = rois_ptr[values_per_roi * roi_indx + 2]; + const auto x2 = rois_ptr[values_per_roi * roi_indx + 3]; + const auto y2 = rois_ptr[values_per_roi * roi_indx + 4]; + + 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(); + + // Iterate through all feature maps + for(int ch = 0; ch < input_chanels; ++ch) + { + // Iterate through all output pixels + for(int py = 0; py < pooled_h; ++py) + { + for(int px = 0; px < pooled_w; ++px) + { + const float region_start_x = compute_region_coordinate(px, bin_size_x, roi_anchor_x, input_width); + const float region_start_y = compute_region_coordinate(py, bin_size_y, roi_anchor_y, input_height); + const float region_end_x = compute_region_coordinate(px + 1, bin_size_x, roi_anchor_x, input_width); + const float region_end_y = compute_region_coordinate(py + 1, bin_size_y, roi_anchor_y, input_height); + const int roi_bin_grid_x = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_x)); + const int roi_bin_grid_y = (_pool_info.sampling_ratio() > 0) ? _pool_info.sampling_ratio() : int(ceil(bin_size_y)); + + const float out_val = roi_align_1x1(_input, roi_batch, region_start_x, bin_size_x, + roi_bin_grid_x, + region_end_x, + region_start_y, + bin_size_y, + roi_bin_grid_y, + region_end_y, ch); + + if(data_layout == DataLayout::NCHW) + { + auto out_ptr = reinterpret_cast(_output->ptr_to_element(Coordinates(px, py, ch, roi_indx))); + *out_ptr = out_val; + } + else + { + auto out_ptr = reinterpret_cast(_output->ptr_to_element(Coordinates(ch, px, py, roi_indx))); + *out_ptr = out_val; + } + } + } + } + } +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEROIAlignLayer.cpp b/src/runtime/NEON/functions/NEROIAlignLayer.cpp new file mode 100644 index 0000000000..b4e0a2f9fa --- /dev/null +++ b/src/runtime/NEON/functions/NEROIAlignLayer.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 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/runtime/NEON/functions/NEROIAlignLayer.h" + +#include "arm_compute/core/NEON/kernels/NEROIAlignLayerKernel.h" +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +Status NEROIAlignLayer::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(NEROIAlignLayerKernel::validate(input, rois, output, pool_info)); + + return Status{}; +} + +void NEROIAlignLayer::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) +{ + // Configure ROI pooling kernel + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(input, rois, output, pool_info); + _kernel = std::move(k); +} + +} // namespace arm_compute diff --git a/tests/validation/NEON/ROIAlignLayer.cpp b/tests/validation/NEON/ROIAlignLayer.cpp new file mode 100644 index 0000000000..853ef6558d --- /dev/null +++ b/tests/validation/NEON/ROIAlignLayer.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2019 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/NEROIAlignLayer.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/ROIAlignLayerFixture.h" +#include "utils/TypePrinter.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +RelativeTolerance relative_tolerance_f32(0.01f); +AbsoluteTolerance absolute_tolerance_f32(0.001f); + +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +RelativeTolerance relative_tolerance_f16(0.01f); +AbsoluteTolerance absolute_tolerance_f16(0.001f); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(RoiAlign) + +// *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), + 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, 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::F32), + TensorInfo(TensorShape(5, 4U), 1, DataType::F16), + TensorInfo(TensorShape(5, 4U), 1, DataType::F32), + TensorInfo(TensorShape(5, 4U), 1, DataType::F32), + TensorInfo(TensorShape(5, 10U), 1, DataType::F32), + TensorInfo(TensorShape(4, 4U), 1, DataType::F32), + TensorInfo(TensorShape(5, 4U), 1, DataType::F32), + })), + framework::dataset::make("OutputInfo",{ 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::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), + })), + framework::dataset::make("Expected", { true, false, false, false, false, false, false })), + input_info, rois_info, output_info, pool_info, expected) +{ + ARM_COMPUTE_EXPECT(bool(NEROIAlignLayer::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 NEROIAlignLayerFixture = ROIAlignLayerFixture; + +TEST_SUITE(Float) +FIXTURE_DATA_TEST_CASE(SmallROIAlignLayerFloat, NEROIAlignLayerFixture, framework::DatasetMode::ALL, + framework::dataset::combine(framework::dataset::combine(datasets::SmallROIDataset(), + framework::dataset::make("DataType", { DataType::F32 })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference, relative_tolerance_f32, .02f, absolute_tolerance_f32); +} +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +FIXTURE_DATA_TEST_CASE(SmallROIAlignLayerHalf, NEROIAlignLayerFixture, framework::DatasetMode::ALL, + framework::dataset::combine(framework::dataset::combine(datasets::SmallROIDataset(), + framework::dataset::make("DataType", { DataType::F16 })), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(Accessor(_target), _reference, relative_tolerance_f16, .02f, absolute_tolerance_f16); +} +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + +TEST_SUITE_END() // Float + +TEST_SUITE_END() // RoiAlign +TEST_SUITE_END() // NEON +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1