From c9564cb3850b6675cef663d7cc0722567b55cc25 Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Fri, 13 Sep 2019 10:20:25 +0100 Subject: COMPMID-2257: Implement NEGenerateProposals. Change-Id: I8d751f8b09f842a214c305a4530a71d82f16db8f Signed-off-by: Pablo Tello Reviewed-on: https://review.mlplatform.org/c/1943 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Reviewed-by: Michele Di Giorgio --- src/core/NEON/kernels/NECopyKernel.cpp | 116 +++++++-- .../kernels/NEGenerateProposalsLayerKernel.cpp | 131 ++++++++++ src/runtime/NEON/functions/NEComputeAllAnchors.cpp | 42 ++++ .../NEON/functions/NEGenerateProposalsLayer.cpp | 264 +++++++++++++++++++++ 4 files changed, 533 insertions(+), 20 deletions(-) create mode 100644 src/core/NEON/kernels/NEGenerateProposalsLayerKernel.cpp create mode 100644 src/runtime/NEON/functions/NEComputeAllAnchors.cpp create mode 100644 src/runtime/NEON/functions/NEGenerateProposalsLayer.cpp (limited to 'src') diff --git a/src/core/NEON/kernels/NECopyKernel.cpp b/src/core/NEON/kernels/NECopyKernel.cpp index 4722c05507..83f3dded4f 100644 --- a/src/core/NEON/kernels/NECopyKernel.cpp +++ b/src/core/NEON/kernels/NECopyKernel.cpp @@ -29,28 +29,88 @@ #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Validate.h" #include "arm_compute/core/Window.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" -using namespace arm_compute; +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList()) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4); + + // Validate output if initialized + if(output->total_size() != 0) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding), output->tensor_shape()); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + } + + return Status{}; +} + +std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) +{ + // Output auto inizialitation if not yet initialized + auto_init_if_empty(*output, *input); + return std::make_pair(Status{}, calculate_max_window(*output)); +} + +std::pair validate_and_configure_window_with_padding(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding) +{ + const TensorShape input_shape = input->tensor_shape(); + const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input_shape, padding); + auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape)); + // Configure window + const Window win = calculate_max_window(*output, output->dimension(0)); + return std::make_pair(Status{}, win); +} + +} // namespace NECopyKernel::NECopyKernel() - : _input(nullptr), _output(nullptr) + : _input(nullptr), _output(nullptr), _padding() { } -void NECopyKernel::configure(const ITensor *input, ITensor *output) +void NECopyKernel::configure(const ITensor *input, ITensor *output, const PaddingList &padding) { ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding)); + + _input = input; + _output = output; + _padding = padding; - _input = input; - _output = output; + std::pair win_config; - INEKernel::configure(calculate_max_window(*output->info())); + if(padding.empty()) + { + win_config = validate_and_configure_window(input->info(), output->info()); + } + else + { + win_config = validate_and_configure_window_with_padding(input->info(), output->info(), padding); + } + + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + INEKernel::configure(win_config.second); } -Status NECopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output) +Status NECopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, const PaddingList &padding) { - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding)); + + if(padding.empty()) + { + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(input->clone().get(), output->clone().get(), padding).first); + } + return Status{}; } @@ -60,22 +120,38 @@ void NECopyKernel::run(const Window &window, const ThreadInfo &info) ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - Window output_window{ window }; - output_window.set(Window::DimX, Window::Dimension(output_window.x().start(), output_window.x().end(), _input->info()->dimension(0))); - - Window out_slice = output_window.first_slice_window_1D(); + if(_padding.empty()) + { + Window output_window{ window }; + output_window.set(Window::DimX, Window::Dimension(output_window.x().start(), output_window.x().end(), _input->info()->dimension(0))); + Window out_slice = output_window.first_slice_window_1D(); + do + { + Iterator input_it(_input, out_slice); + Iterator output_it(_output, out_slice); - do + execute_window_loop(out_slice, [&](const Coordinates &) + { + memcpy(output_it.ptr(), input_it.ptr(), _output->info()->dimension(0) * _output->info()->element_size()); + }, + input_it, output_it); + } + while(output_window.slide_window_slice_1D(out_slice)); + } + else { - Iterator input_it(_input, out_slice); - Iterator output_it(_output, out_slice); + Window input_window{ window }; + input_window.set(Window::DimX, Window::Dimension(0, window.x().end() - _padding[0].first, _input->info()->dimension(0))); - execute_window_loop(out_slice, [&](const Coordinates &) + Iterator input_it(_input, input_window); + Iterator output_it(_output, window); + const size_t row_size_in_bytes = _input->info()->dimension(0) * _input->info()->element_size(); + execute_window_loop(window, [&](const Coordinates &) { - memcpy(output_it.ptr(), input_it.ptr(), _output->info()->dimension(0) * _output->info()->element_size()); + auto dst_ptr = output_it.ptr() + _padding[0].first * _output->info()->element_size(); + std::memcpy(dst_ptr, input_it.ptr(), row_size_in_bytes); }, input_it, output_it); - } - while(output_window.slide_window_slice_1D(out_slice)); } +} // namespace arm_compute diff --git a/src/core/NEON/kernels/NEGenerateProposalsLayerKernel.cpp b/src/core/NEON/kernels/NEGenerateProposalsLayerKernel.cpp new file mode 100644 index 0000000000..4a585b70fd --- /dev/null +++ b/src/core/NEON/kernels/NEGenerateProposalsLayerKernel.cpp @@ -0,0 +1,131 @@ +/* + * 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/NEGenerateProposalsLayerKernel.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" + +namespace arm_compute +{ +namespace +{ +Status validate_arguments(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(anchors, all_anchors); + ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(anchors); + ARM_COMPUTE_RETURN_ERROR_ON(anchors->dimension(0) != info.values_per_roi()); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(anchors, DataType::F16, DataType::F32); + ARM_COMPUTE_RETURN_ERROR_ON(anchors->num_dimensions() > 2); + if(all_anchors->total_size() > 0) + { + const size_t feature_height = info.feat_height(); + const size_t feature_width = info.feat_width(); + const size_t num_anchors = anchors->dimension(1); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(all_anchors, anchors); + ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(0) != info.values_per_roi()); + ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(1) != feature_height * feature_width * num_anchors); + } + return Status{}; +} + +} // namespace + +NEComputeAllAnchorsKernel::NEComputeAllAnchorsKernel() + : _anchors(nullptr), _all_anchors(nullptr), _anchors_info(0.f, 0.f, 0.f) +{ +} + +void NEComputeAllAnchorsKernel::configure(const ITensor *anchors, ITensor *all_anchors, const ComputeAnchorsInfo &info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(anchors, all_anchors); + ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(anchors->info(), all_anchors->info(), info)); + + // Metadata + const size_t num_anchors = anchors->info()->dimension(1); + const DataType data_type = anchors->info()->data_type(); + const float width = info.feat_width(); + const float height = info.feat_height(); + + // Initialize the output if empty + const TensorShape output_shape(info.values_per_roi(), width * height * num_anchors); + auto_init_if_empty(*all_anchors->info(), output_shape, 1, data_type); + + // Set instance variables + _anchors = anchors; + _all_anchors = all_anchors; + _anchors_info = info; + + Window win = calculate_max_window(*all_anchors->info(), Steps(info.values_per_roi())); + + INEKernel::configure(win); +} + +Status NEComputeAllAnchorsKernel::validate(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info) +{ + ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(anchors, all_anchors, info)); + return Status{}; +} + +void NEComputeAllAnchorsKernel::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); + + Iterator all_anchors_it(_all_anchors, window); + Iterator anchors_it(_all_anchors, window); + + const size_t num_anchors = _anchors->info()->dimension(1); + const float stride = 1.f / _anchors_info.spatial_scale(); + const size_t feat_width = _anchors_info.feat_width(); + + execute_window_loop(window, [&](const Coordinates & id) + { + const size_t anchor_offset = id.y() % num_anchors; + + const auto out_anchor_ptr = reinterpret_cast(all_anchors_it.ptr()); + const auto anchor_ptr = reinterpret_cast(_anchors->ptr_to_element(Coordinates(0, anchor_offset))); + + *out_anchor_ptr = *anchor_ptr; + *(1 + out_anchor_ptr) = *(1 + anchor_ptr); + *(2 + out_anchor_ptr) = *(2 + anchor_ptr); + *(3 + out_anchor_ptr) = *(3 + anchor_ptr); + + const size_t shift_idy = id.y() / num_anchors; + const float shiftx = (shift_idy % feat_width) * stride; + const float shifty = (shift_idy / feat_width) * stride; + + *out_anchor_ptr += shiftx; + *(out_anchor_ptr + 1) += shifty; + *(out_anchor_ptr + 2) += shiftx; + *(out_anchor_ptr + 3) += shifty; + }, + all_anchors_it); +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEComputeAllAnchors.cpp b/src/runtime/NEON/functions/NEComputeAllAnchors.cpp new file mode 100644 index 0000000000..7702fb026d --- /dev/null +++ b/src/runtime/NEON/functions/NEComputeAllAnchors.cpp @@ -0,0 +1,42 @@ +/* + * 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/NEComputeAllAnchors.h" + +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +void NEComputeAllAnchors::configure(const ITensor *anchors, ITensor *all_anchors, const ComputeAnchorsInfo &info) +{ + // Configure ComputeAllAnchors kernel + auto k = arm_compute::support::cpp14::make_unique(); + k->configure(anchors, all_anchors, info); + _kernel = std::move(k); +} + +Status NEComputeAllAnchors::validate(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info) +{ + return NEComputeAllAnchorsKernel::validate(anchors, all_anchors, info); +} +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEGenerateProposalsLayer.cpp b/src/runtime/NEON/functions/NEGenerateProposalsLayer.cpp new file mode 100644 index 0000000000..6e5da43a94 --- /dev/null +++ b/src/runtime/NEON/functions/NEGenerateProposalsLayer.cpp @@ -0,0 +1,264 @@ +/* + * 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/NEGenerateProposalsLayer.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/NEON/NEScheduler.h" +#include "support/ToolchainSupport.h" + +namespace arm_compute +{ +NEGenerateProposalsLayer::NEGenerateProposalsLayer(std::shared_ptr memory_manager) + : _memory_group(std::move(memory_manager)), + _permute_deltas_kernel(), + _flatten_deltas_kernel(), + _permute_scores_kernel(), + _flatten_scores_kernel(), + _compute_anchors_kernel(), + _bounding_box_kernel(), + _memset_kernel(), + _padded_copy_kernel(), + _cpp_nms_kernel(), + _is_nhwc(false), + _deltas_permuted(), + _deltas_flattened(), + _scores_permuted(), + _scores_flattened(), + _all_anchors(), + _all_proposals(), + _keeps_nms_unused(), + _classes_nms_unused(), + _proposals_4_roi_values(), + _num_valid_proposals(nullptr), + _scores_out(nullptr) +{ +} + +void NEGenerateProposalsLayer::configure(const ITensor *scores, const ITensor *deltas, const ITensor *anchors, ITensor *proposals, ITensor *scores_out, ITensor *num_valid_proposals, + const GenerateProposalsInfo &info) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals); + ARM_COMPUTE_ERROR_THROW_ON(NEGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info)); + + _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC; + const DataType data_type = deltas->info()->data_type(); + const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL)); + const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH)); + const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT)); + const int total_num_anchors = num_anchors * feat_width * feat_height; + const int pre_nms_topN = info.pre_nms_topN(); + const int post_nms_topN = info.post_nms_topN(); + const size_t values_per_roi = info.values_per_roi(); + + // Compute all the anchors + _memory_group.manage(&_all_anchors); + _compute_anchors_kernel.configure(anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())); + + const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors); + _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, data_type)); + _memory_group.manage(&_deltas_flattened); + + // Permute and reshape deltas + if(!_is_nhwc) + { + _memory_group.manage(&_deltas_permuted); + _permute_deltas_kernel.configure(deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 }); + _flatten_deltas_kernel.configure(&_deltas_permuted, &_deltas_flattened); + _deltas_permuted.allocator()->allocate(); + } + else + { + _flatten_deltas_kernel.configure(deltas, &_deltas_flattened); + } + + const TensorShape flatten_shape_scores(1, total_num_anchors); + _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, data_type)); + _memory_group.manage(&_scores_flattened); + // Permute and reshape scores + if(!_is_nhwc) + { + _memory_group.manage(&_scores_permuted); + _permute_scores_kernel.configure(scores, &_scores_permuted, PermutationVector{ 2, 0, 1 }); + _flatten_scores_kernel.configure(&_scores_permuted, &_scores_flattened); + _scores_permuted.allocator()->allocate(); + } + else + { + _flatten_scores_kernel.configure(scores, &_scores_flattened); + } + + // Bounding box transform + _memory_group.manage(&_all_proposals); + BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f); + _bounding_box_kernel.configure(&_all_anchors, &_all_proposals, &_deltas_flattened, bbox_info); + _deltas_flattened.allocator()->allocate(); + _all_anchors.allocator()->allocate(); + + // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort) + // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation. + // Since we are reusing the NMS layer and we don't implement any CL/sort, we let NMS do the sorting (of all the input) + // and the filtering + const int scores_nms_size = std::min(std::min(post_nms_topN, pre_nms_topN), total_num_anchors); + const float min_size_scaled = info.min_size() * info.im_scale(); + _memory_group.manage(&_classes_nms_unused); + _memory_group.manage(&_keeps_nms_unused); + + // Note that NMS needs outputs preinitialized. + auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, data_type); + auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, data_type); + auto_init_if_empty(*num_valid_proposals->info(), TensorShape(scores_nms_size), 1, DataType::U32); + + // Initialize temporaries (unused) outputs + _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(8, 1), 1, data_type)); + _keeps_nms_unused.allocator()->init(*scores_out->info()); + + // Save the output (to map and unmap them at run) + _scores_out = scores_out; + _num_valid_proposals = num_valid_proposals; + + _memory_group.manage(&_proposals_4_roi_values); + + const BoxNMSLimitInfo box_nms_info(0.0f, info.nms_thres(), scores_nms_size, false, NMSType::LINEAR, 0.5f, 0.001f, true, min_size_scaled, info.im_width(), info.im_height()); + _cpp_nms_kernel.configure(&_scores_flattened /*scores_in*/, + &_all_proposals /*boxes_in,*/, + nullptr /* batch_splits_in*/, + scores_out /* scores_out*/, + &_proposals_4_roi_values /*boxes_out*/, + &_classes_nms_unused /*classes*/, + nullptr /*batch_splits_out*/, + &_keeps_nms_unused /*keeps*/, + num_valid_proposals /* keeps_size*/, + box_nms_info); + + _keeps_nms_unused.allocator()->allocate(); + _classes_nms_unused.allocator()->allocate(); + _all_proposals.allocator()->allocate(); + _scores_flattened.allocator()->allocate(); + + // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images + _padded_copy_kernel.configure(&_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } }); + _proposals_4_roi_values.allocator()->allocate(); + + _memset_kernel.configure(proposals, PixelValue()); +} + +Status NEGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out, + const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info) +{ + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas); + + const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL)); + const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH)); + const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT)); + const int num_images = scores->dimension(3); + const int total_num_anchors = num_anchors * feat_width * feat_height; + const int values_per_roi = info.values_per_roi(); + + ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1); + + TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); + ARM_COMPUTE_RETURN_ON_ERROR(NEComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()))); + + TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true); + TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true); + if(scores->data_layout() == DataLayout::NHWC) + { + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info); + } + else + { + ARM_COMPUTE_RETURN_ON_ERROR(NEPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 })); + ARM_COMPUTE_RETURN_ON_ERROR(NEPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 })); + } + + TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); + ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(&deltas_permuted_info, &deltas_flattened_info)); + + TensorInfo scores_flattened_info(scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true)); + TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); + + ARM_COMPUTE_RETURN_ON_ERROR(NEReshapeLayerKernel::validate(&scores_permuted_info, &scores_flattened_info)); + ARM_COMPUTE_RETURN_ON_ERROR(NEBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info, BoundingBoxTransformInfo(info.im_width(), info.im_height(), + 1.f))); + + ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(&proposals_4_roi_values, proposals, PaddingList{ { 0, 1 } })); + + if(num_valid_proposals->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32); + } + + if(proposals->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2); + ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1); + ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors)); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, deltas); + } + + if(scores_out->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1); + ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors)); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores); + } + + return Status{}; +} + +void NEGenerateProposalsLayer::run() +{ + // Acquire all the temporaries + MemoryGroupResourceScope scope_mg(_memory_group); + + // Compute all the anchors + NEScheduler::get().schedule(&_compute_anchors_kernel, Window::DimY); + + // Transpose and reshape the inputs + if(!_is_nhwc) + { + NEScheduler::get().schedule(&_permute_deltas_kernel, Window::DimY); + NEScheduler::get().schedule(&_permute_scores_kernel, Window::DimY); + } + + NEScheduler::get().schedule(&_flatten_deltas_kernel, Window::DimY); + NEScheduler::get().schedule(&_flatten_scores_kernel, Window::DimY); + + // Build the boxes + NEScheduler::get().schedule(&_bounding_box_kernel, Window::DimY); + + // Non maxima suppression + CPPScheduler::get().schedule(&_cpp_nms_kernel, Window::DimX); + + // Add dummy batch indexes + + NEScheduler::get().schedule(&_memset_kernel, Window::DimY); + NEScheduler::get().schedule(&_padded_copy_kernel, Window::DimY); +} +} // namespace arm_compute -- cgit v1.2.1