From 3f9d4d7c7242f50330dca4ff104a76bc88804903 Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Fri, 19 Oct 2018 14:04:42 +0100 Subject: COMPMID-1637 - Add ROIAlign operator to the graph API Change-Id: I8a9b1e16d90b9d99a6ff2a442347748432723b14 --- arm_compute/graph/GraphBuilder.h | 11 ++++ arm_compute/graph/TypePrinter.h | 3 + arm_compute/graph/Types.h | 1 + arm_compute/graph/backends/FunctionHelpers.h | 43 +++++++++++++ arm_compute/graph/backends/ValidateHelpers.h | 25 ++++++++ arm_compute/graph/frontend/Layers.h | 34 ++++++++++ arm_compute/graph/nodes/Nodes.h | 1 + arm_compute/graph/nodes/NodesFwd.h | 1 + arm_compute/graph/nodes/ROIAlignLayerNode.h | 66 +++++++++++++++++++ src/graph/GraphBuilder.cpp | 14 ++++ src/graph/backends/CL/CLFunctionsFactory.cpp | 2 + src/graph/backends/CL/CLNodeValidator.cpp | 2 + src/graph/backends/GLES/GCNodeValidator.cpp | 2 + src/graph/backends/NEON/NENodeValidator.cpp | 2 + src/graph/nodes/ROIAlignLayerNode.cpp | 95 ++++++++++++++++++++++++++++ 15 files changed, 302 insertions(+) create mode 100644 arm_compute/graph/nodes/ROIAlignLayerNode.h create mode 100644 src/graph/nodes/ROIAlignLayerNode.cpp diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h index c501006ec6..22fc041684 100644 --- a/arm_compute/graph/GraphBuilder.h +++ b/arm_compute/graph/GraphBuilder.h @@ -340,6 +340,17 @@ public: * @return Node ID of the created node, EmptyNodeID in case of error */ static NodeID add_resize_node(Graph &g, NodeParams params, NodeIdxPair input, InterpolationPolicy policy, float width_scale, float height_scale); + /** Adds a ROI align layer node to the graph + * + * @param[in] g Graph to add the node to + * @param[in] params Common node parameters + * @param[in] input Input to the reshape layer node as a NodeID-Index pair + * @param[in] rois Input containing @ref ROI. + * @param[in] pool_info Contains pooling operation information described in @ref ROIPoolingLayerInfo. + * + * @return Node ID of the created node, EmptyNodeID in case of error + */ + static NodeID add_roi_align_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info); /** Adds a scale layer node to the graph * This layer computes a product of the input with a scale (read from mul_accessor) and it applies an offset (read from add_accessor). * output = input * mul_w + add_w diff --git a/arm_compute/graph/TypePrinter.h b/arm_compute/graph/TypePrinter.h index b7dc2bb284..c66f9cb374 100644 --- a/arm_compute/graph/TypePrinter.h +++ b/arm_compute/graph/TypePrinter.h @@ -122,6 +122,9 @@ inline ::std::ostream &operator<<(::std::ostream &os, const NodeType &node_type) case NodeType::ResizeLayer: os << "ResizeLayer"; break; + case NodeType::ROIAlignLayer: + os << "ROIAlignLayer"; + break; case NodeType::SoftmaxLayer: os << "SoftmaxLayer"; break; diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index ceee776aaa..24c24d328f 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -144,6 +144,7 @@ enum class NodeType ReorgLayer, ReshapeLayer, ResizeLayer, + ROIAlignLayer, SoftmaxLayer, SliceLayer, SplitLayer, diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h index 082d43afdb..e556e2f284 100644 --- a/arm_compute/graph/backends/FunctionHelpers.h +++ b/arm_compute/graph/backends/FunctionHelpers.h @@ -966,6 +966,49 @@ std::unique_ptr create_resize_layer(ResizeLayerNode &node) return std::move(func); } +/** Create a backend ROI align layer function + * + * @tparam ROIAlignLayerFunction ROI Align function + * @tparam TargetInfo Target-specific information + * + * @param[in] node Node to create the backend function for + * + * @return ROI Align layer function + */ +template +std::unique_ptr create_roi_align_layer(ROIAlignLayerNode &node) +{ + validate_node(node, 2 /* expected inputs */, 1 /* expected outputs */); + + // Extract IO and info + typename TargetInfo::TensorType *input = get_backing_tensor(node.input(0)); + typename TargetInfo::TensorType *rois = get_backing_tensor(node.input(1)); + typename TargetInfo::TensorType *output = get_backing_tensor(node.output(0)); + ARM_COMPUTE_ERROR_ON(input == nullptr); + ARM_COMPUTE_ERROR_ON(output == nullptr); + ARM_COMPUTE_ERROR_ON(rois == nullptr); + + const ROIPoolingLayerInfo pool_info = node.pooling_info(); + + // Create and configure function + auto func = support::cpp14::make_unique(); + + func->configure(input, rois, output, pool_info); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() + << " Target " << TargetInfo::TargetType + << " Data Type: " << input->info()->data_type() + << " Input shape: " << input->info()->tensor_shape() + << " Output shape: " << output->info()->tensor_shape() + << " ROIs shape: " << rois->info()->tensor_shape() + << " ROIPooling width: " << pool_info.pooled_width() + << " ROIPooling height: " << pool_info.pooled_height() + << std::endl); + + return std::move(func); +} + /** Create a backend slice layer function * * @tparam SliceLayerFunction Backend slice function diff --git a/arm_compute/graph/backends/ValidateHelpers.h b/arm_compute/graph/backends/ValidateHelpers.h index 169c795fb4..7c31a80967 100644 --- a/arm_compute/graph/backends/ValidateHelpers.h +++ b/arm_compute/graph/backends/ValidateHelpers.h @@ -324,6 +324,31 @@ Status validate_reorg_layer(ReorgLayerNode &node) return ReorgLayer::validate(input, output, node.stride()); } +/** Validates a ROI Align layer node + * + * @tparam ROIAlignLayer ROIAlign layer type + * + * @param[in] node Node to validate + * + * @return Status + */ +template +Status validate_roi_align_layer(ROIAlignLayerNode &node) +{ + ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ROIAlignLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 2); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1); + + // Extract input and output + arm_compute::ITensorInfo *input = detail::get_backing_tensor_info(node.input(0)); + arm_compute::ITensorInfo *rois = detail::get_backing_tensor_info(node.input(1)); + arm_compute::ITensorInfo *output = detail::get_backing_tensor_info(node.output(0)); + const ROIPoolingLayerInfo &pool_info = node.pooling_info(); + + // Validate function + return ROIAlignLayer::validate(input, rois, output, pool_info); +} + /** Validates a Slice layer node * * @tparam SliceLayer Slice layer function type diff --git a/arm_compute/graph/frontend/Layers.h b/arm_compute/graph/frontend/Layers.h index 56dcd88077..7ed448e3f2 100644 --- a/arm_compute/graph/frontend/Layers.h +++ b/arm_compute/graph/frontend/Layers.h @@ -795,6 +795,40 @@ private: float _height_scale; }; +/** ROIAlign Layer */ +class ROIAlignLayer final : public ILayer +{ +public: + /** Construct a RoiAlign layer. + * + * @param[in] sub_stream_input Graph sub-stream for the input + * @param[in] sub_stream_rois Graph sub-stream for the rois + * @param[in] pool_info Pooling information. + */ + ROIAlignLayer(SubStream &&sub_stream_input, SubStream &&sub_stream_rois, ROIPoolingLayerInfo pool_info) + : _ss_input(sub_stream_input), _ss_rois(sub_stream_rois), _pool_info(pool_info) + { + } + + /** Prevent instances of this class from being copy constructed */ + ROIAlignLayer(const ROIAlignLayer &) = delete; + /** Prevent instances of this class from being copied */ + ROIAlignLayer &operator=(const ROIAlignLayer &) = delete; + + NodeID create_layer(IStream &s) override + { + NodeParams common_params = { name(), s.hints().target_hint }; + NodeIdxPair input = { _ss_input.tail_node(), 0 }; + NodeIdxPair rois = { _ss_rois.tail_node(), 0 }; + return GraphBuilder::add_roi_align_node(s.graph(), common_params, input, rois, _pool_info); + } + +private: + SubStream _ss_input; + SubStream _ss_rois; + ROIPoolingLayerInfo _pool_info; +}; + /** Scale Layer */ class ScaleLayer final : public ILayer { diff --git a/arm_compute/graph/nodes/Nodes.h b/arm_compute/graph/nodes/Nodes.h index afd4e2cf67..342ecbfd3b 100644 --- a/arm_compute/graph/nodes/Nodes.h +++ b/arm_compute/graph/nodes/Nodes.h @@ -45,6 +45,7 @@ #include "arm_compute/graph/nodes/PadLayerNode.h" #include "arm_compute/graph/nodes/PermuteLayerNode.h" #include "arm_compute/graph/nodes/PoolingLayerNode.h" +#include "arm_compute/graph/nodes/ROIAlignLayerNode.h" #include "arm_compute/graph/nodes/ReorgLayerNode.h" #include "arm_compute/graph/nodes/ReshapeLayerNode.h" #include "arm_compute/graph/nodes/ResizeLayerNode.h" diff --git a/arm_compute/graph/nodes/NodesFwd.h b/arm_compute/graph/nodes/NodesFwd.h index 929a4021ef..8d9bad3771 100644 --- a/arm_compute/graph/nodes/NodesFwd.h +++ b/arm_compute/graph/nodes/NodesFwd.h @@ -54,6 +54,7 @@ class PoolingLayerNode; class ReorgLayerNode; class ReshapeLayerNode; class ResizeLayerNode; +class ROIAlignLayerNode; class SoftmaxLayerNode; class SliceLayerNode; class SplitLayerNode; diff --git a/arm_compute/graph/nodes/ROIAlignLayerNode.h b/arm_compute/graph/nodes/ROIAlignLayerNode.h new file mode 100644 index 0000000000..5c2af7d38e --- /dev/null +++ b/arm_compute/graph/nodes/ROIAlignLayerNode.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __ARM_COMPUTE_GRAPH_ROI_ALIGN_NODE_H__ +#define __ARM_COMPUTE_GRAPH_ROI_ALIGN_NODE_H__ + +#include "arm_compute/core/Types.h" +#include "arm_compute/graph/INode.h" + +namespace arm_compute +{ +namespace graph +{ +/** ROI Align node */ +class ROIAlignLayerNode final : public INode +{ +public: + /** Constructor + * + * @param[in] pool_info Contains pooling operation information described in @ref ROIPoolingLayerInfo. + */ + ROIAlignLayerNode(ROIPoolingLayerInfo &pool_info); + /** Prevent instances of this class from being copy constructed */ + ROIAlignLayerNode(const ROIAlignLayerNode &) = delete; + /** Prevent instances of this class from being copied */ + ROIAlignLayerNode &operator=(const ROIAlignLayerNode &) = delete; + + /** ROIPoolingLayerInfo accessor + * + * @return ROIPoolingLayerInfo + */ + const ROIPoolingLayerInfo &pooling_info() const; + + // Inherited overridden methods: + NodeType type() const override; + bool forward_descriptors() override; + TensorDescriptor configure_output(size_t idx) const override; + void accept(INodeVisitor &v) override; + +private: + ROIPoolingLayerInfo _pool_info; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_ROI_ALIGN_NODE_H__ */ diff --git a/src/graph/GraphBuilder.cpp b/src/graph/GraphBuilder.cpp index 7870fb10ea..b4c58780bd 100644 --- a/src/graph/GraphBuilder.cpp +++ b/src/graph/GraphBuilder.cpp @@ -510,6 +510,20 @@ NodeID GraphBuilder::add_resize_node(Graph &g, NodeParams params, NodeIdxPair in return create_simple_single_input_output_node(g, params, input, policy, width_scale, height_scale); } +NodeID GraphBuilder::add_roi_align_node(Graph &g, NodeParams params, NodeIdxPair input, NodeIdxPair rois, ROIPoolingLayerInfo pool_info) +{ + CHECK_NODEIDX_PAIR(input, g); + CHECK_NODEIDX_PAIR(rois, g); + + NodeID nid = g.add_node(pool_info); + + g.add_connection(input.node_id, input.index, nid, 0); + g.add_connection(rois.node_id, rois.index, nid, 1); + + set_node_params(g, nid, params); + return nid; +} + NodeID GraphBuilder::add_scale_layer(Graph &g, const NodeParams ¶ms, NodeIdxPair input, ITensorAccessorUPtr mul_accessor, ITensorAccessorUPtr add_accessor) { CHECK_NODEIDX_PAIR(input, g); diff --git a/src/graph/backends/CL/CLFunctionsFactory.cpp b/src/graph/backends/CL/CLFunctionsFactory.cpp index d627a39557..f63aba9ec5 100644 --- a/src/graph/backends/CL/CLFunctionsFactory.cpp +++ b/src/graph/backends/CL/CLFunctionsFactory.cpp @@ -119,6 +119,8 @@ std::unique_ptr CLFunctionFactory::create(INode *node, GraphContext & return detail::create_reshape_layer(*polymorphic_downcast(node)); case NodeType::ResizeLayer: return detail::create_resize_layer(*polymorphic_downcast(node)); + case NodeType::ROIAlignLayer: + return detail::create_roi_align_layer(*polymorphic_downcast(node)); case NodeType::SliceLayer: return detail::create_slice_layer(*polymorphic_downcast(node)); case NodeType::SoftmaxLayer: diff --git a/src/graph/backends/CL/CLNodeValidator.cpp b/src/graph/backends/CL/CLNodeValidator.cpp index 9cbf4bb3eb..1ea3517467 100644 --- a/src/graph/backends/CL/CLNodeValidator.cpp +++ b/src/graph/backends/CL/CLNodeValidator.cpp @@ -69,6 +69,8 @@ Status CLNodeValidator::validate(INode *node) return detail::validate_permute_layer(*polymorphic_downcast(node)); case NodeType::ReorgLayer: return detail::validate_reorg_layer(*polymorphic_downcast(node)); + case NodeType::ROIAlignLayer: + return detail::validate_roi_align_layer(*polymorphic_downcast(node)); case NodeType::SliceLayer: return detail::validate_slice_layer(*polymorphic_downcast(node)); case NodeType::UpsampleLayer: diff --git a/src/graph/backends/GLES/GCNodeValidator.cpp b/src/graph/backends/GLES/GCNodeValidator.cpp index e5ba66205f..9cf39c6675 100644 --- a/src/graph/backends/GLES/GCNodeValidator.cpp +++ b/src/graph/backends/GLES/GCNodeValidator.cpp @@ -125,6 +125,8 @@ Status GCNodeValidator::validate(INode *node) return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : ReorgLayer"); case NodeType::ReshapeLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : ReshapeLayer"); + case NodeType::ROIAlignLayer: + return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : ROIAlignLayer"); case NodeType::SliceLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : SliceLayer"); case NodeType::UpsampleLayer: diff --git a/src/graph/backends/NEON/NENodeValidator.cpp b/src/graph/backends/NEON/NENodeValidator.cpp index 606cdf8291..f2131586b2 100644 --- a/src/graph/backends/NEON/NENodeValidator.cpp +++ b/src/graph/backends/NEON/NENodeValidator.cpp @@ -69,6 +69,8 @@ Status NENodeValidator::validate(INode *node) return detail::validate_permute_layer(*polymorphic_downcast(node)); case NodeType::ReorgLayer: return detail::validate_reorg_layer(*polymorphic_downcast(node)); + case NodeType::ROIAlignLayer: + return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : ROIAlignLayer"); case NodeType::SliceLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : SliceLayer"); case NodeType::UpsampleLayer: diff --git a/src/graph/nodes/ROIAlignLayerNode.cpp b/src/graph/nodes/ROIAlignLayerNode.cpp new file mode 100644 index 0000000000..5e89ef2263 --- /dev/null +++ b/src/graph/nodes/ROIAlignLayerNode.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "arm_compute/graph/nodes/ROIAlignLayerNode.h" + +#include "arm_compute/graph/Graph.h" +#include "arm_compute/graph/INodeVisitor.h" + +#include "arm_compute/core/Helpers.h" + +namespace arm_compute +{ +namespace graph +{ +ROIAlignLayerNode::ROIAlignLayerNode(ROIPoolingLayerInfo &pool_info) + : _pool_info(pool_info) +{ + _input_edges.resize(2, EmptyEdgeID); + _outputs.resize(1, NullTensorID); +} + +const ROIPoolingLayerInfo &ROIAlignLayerNode::pooling_info() const +{ + return _pool_info; +} + +bool ROIAlignLayerNode::forward_descriptors() +{ + if((input_id(0) != NullTensorID) && (input_id(1) != NullTensorID) && (output_id(0) != NullTensorID)) + { + Tensor *dst = output(0); + ARM_COMPUTE_ERROR_ON(dst == nullptr); + dst->desc() = configure_output(0); + return true; + } + return false; +} + +TensorDescriptor ROIAlignLayerNode::configure_output(size_t idx) const +{ + ARM_COMPUTE_UNUSED(idx); + ARM_COMPUTE_ERROR_ON(idx >= _outputs.size()); + + const Tensor *src = input(0); + const Tensor *rois = input(1); + ARM_COMPUTE_ERROR_ON(src == nullptr); + ARM_COMPUTE_ERROR_ON(rois == nullptr); + + TensorDescriptor output_desc = src->desc(); + + const size_t idx_n = get_data_layout_dimension_index(output_desc.layout, DataLayoutDimension::BATCHES); + const size_t idx_c = get_data_layout_dimension_index(output_desc.layout, DataLayoutDimension::CHANNEL); + const size_t idx_h = get_data_layout_dimension_index(output_desc.layout, DataLayoutDimension::HEIGHT); + const size_t idx_w = get_data_layout_dimension_index(output_desc.layout, DataLayoutDimension::WIDTH); + + output_desc.shape.set(idx_n, rois->desc().shape[1]); + output_desc.shape.set(idx_c, src->desc().shape[idx_c]); + output_desc.shape.set(idx_h, _pool_info.pooled_height()); + output_desc.shape.set(idx_w, _pool_info.pooled_width()); + + return output_desc; +} + +NodeType ROIAlignLayerNode::type() const +{ + return NodeType::ROIAlignLayer; +} + +void ROIAlignLayerNode::accept(INodeVisitor &v) +{ + v.visit(*this); +} +} // namespace graph +} // namespace arm_compute \ No newline at end of file -- cgit v1.2.1