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 ++++++++++++++++++++++++++++ 9 files changed, 185 insertions(+) create mode 100644 arm_compute/graph/nodes/ROIAlignLayerNode.h (limited to 'arm_compute/graph') 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__ */ -- cgit v1.2.1