From 7234ed8c3d07c76963eb3bce9530994421ad7e67 Mon Sep 17 00:00:00 2001 From: Isabella Gottardi Date: Tue, 27 Nov 2018 08:51:10 +0000 Subject: COMPMID-1808: Add Detection Output Layer to the GraphAPI COMPMID-1710: Integrate Detection ouput in MobilenetSSD graph example Change-Id: I384d1eb492ef14ece58f2023ad7bbc16f834450b Reviewed-on: https://review.mlplatform.org/356 Tested-by: Arm Jenkins Reviewed-by: Pablo Marquez Reviewed-by: Georgios Pinitas --- arm_compute/graph/GraphBuilder.h | 12 ++++ arm_compute/graph/INodeVisitor.h | 9 +++ arm_compute/graph/TypePrinter.h | 3 + arm_compute/graph/Types.h | 2 + arm_compute/graph/backends/FunctionHelpers.h | 45 ++++++++++++++ arm_compute/graph/backends/ValidateHelpers.h | 24 ++++++++ arm_compute/graph/frontend/Layers.h | 28 +++++++++ arm_compute/graph/nodes/DetectionOutputLayerNode.h | 70 ++++++++++++++++++++++ arm_compute/graph/nodes/Nodes.h | 1 + arm_compute/graph/nodes/NodesFwd.h | 1 + 10 files changed, 195 insertions(+) create mode 100644 arm_compute/graph/nodes/DetectionOutputLayerNode.h (limited to 'arm_compute/graph') diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h index 33a13f1836..cb905e700e 100644 --- a/arm_compute/graph/GraphBuilder.h +++ b/arm_compute/graph/GraphBuilder.h @@ -201,6 +201,18 @@ public: * @return Node ID of the created node, EmptyNodeID in case of error */ static NodeID add_elementwise_node(Graph &g, NodeParams params, NodeIdxPair input0, NodeIdxPair input1, EltwiseOperation operation); + /** Adds a detection output layer node to the graph + * + * @param[in] g Graph to add the node to + * @param[in] params Common node parameters + * @param[in] input_loc Location input to the detection output layer node as a NodeID-Index pair + * @param[in] input_conf Confidence input to the detection output layer node as a NodeID-Index pair + * @param[in] input_priorbox PriorBox input to the detection output layer node as a NodeID-Index pair + * @param[in] detect_info Detection output layer parameters + * + * @return Node ID of the created node, EmptyNodeID in case of error + */ + static NodeID add_detection_output_node(Graph &g, NodeParams params, NodeIdxPair input_loc, NodeIdxPair input_conf, NodeIdxPair input_priorbox, DetectionOutputLayerInfo detect_info); /** Adds a Dummy node to the graph * * @note this node if for debugging purposes. Just alters the shape of the graph pipeline as requested. diff --git a/arm_compute/graph/INodeVisitor.h b/arm_compute/graph/INodeVisitor.h index 2df2574d62..573d642892 100644 --- a/arm_compute/graph/INodeVisitor.h +++ b/arm_compute/graph/INodeVisitor.h @@ -71,6 +71,11 @@ public: * @param[in] n Node to visit. */ virtual void visit(DepthwiseConvolutionLayerNode &n) = 0; + /** Visit DetectionOutputLayerNode. + * + * @param[in] n Node to visit. + */ + virtual void visit(DetectionOutputLayerNode &n) = 0; /** Visit EltwiseLayerNode. * * @param[in] n Node to visit. @@ -170,6 +175,10 @@ public: { default_visit(); } + virtual void visit(DetectionOutputLayerNode &n) override + { + default_visit(); + } virtual void visit(DepthwiseConvolutionLayerNode &n) override { default_visit(); diff --git a/arm_compute/graph/TypePrinter.h b/arm_compute/graph/TypePrinter.h index d633091d16..e33c984fd6 100644 --- a/arm_compute/graph/TypePrinter.h +++ b/arm_compute/graph/TypePrinter.h @@ -83,6 +83,9 @@ inline ::std::ostream &operator<<(::std::ostream &os, const NodeType &node_type) case NodeType::DeconvolutionLayer: os << "DeconvolutionLayer"; break; + case NodeType::DetectionOutputLayer: + os << "DetectionOutputLayer"; + break; case NodeType::DepthwiseConvolutionLayer: os << "DepthwiseConvolutionLayer"; break; diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index b6803c89bc..60fe0a883e 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -45,6 +45,7 @@ using arm_compute::Size2D; using arm_compute::PermutationVector; using arm_compute::ActivationLayerInfo; +using arm_compute::DetectionOutputLayerInfo; using arm_compute::NormType; using arm_compute::NormalizationLayerInfo; using arm_compute::FullyConnectedLayerInfo; @@ -133,6 +134,7 @@ enum class NodeType ConvolutionLayer, DeconvolutionLayer, DepthwiseConvolutionLayer, + DetectionOutputLayer, EltwiseLayer, FlattenLayer, FullyConnectedLayer, diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h index 3e71e3922a..96adffee46 100644 --- a/arm_compute/graph/backends/FunctionHelpers.h +++ b/arm_compute/graph/backends/FunctionHelpers.h @@ -489,6 +489,51 @@ std::unique_ptr create_depthwise_convolution_layer(DepthwiseConvoluti return func; } +/** Create a backend detection output layer function + * + * @tparam DetectionOutputLayer Function Backend detection output function + * @tparam TargetInfo Target-specific information + * + * @param[in] node Node to create the backend function for + * + * @return Backend detection output layer function + */ +template +std::unique_ptr create_detection_output_layer(DetectionOutputLayerNode &node) +{ + validate_node(node, 3 /* expected inputs */, 1 /* expected outputs */); + + // Extract IO and info + typename TargetInfo::TensorType *input0 = get_backing_tensor(node.input(0)); + typename TargetInfo::TensorType *input1 = get_backing_tensor(node.input(1)); + typename TargetInfo::TensorType *input2 = get_backing_tensor(node.input(2)); + typename TargetInfo::TensorType *output = get_backing_tensor(node.output(0)); + const DetectionOutputLayerInfo detect_info = node.detection_output_info(); + + ARM_COMPUTE_ERROR_ON(input0 == nullptr); + ARM_COMPUTE_ERROR_ON(input1 == nullptr); + ARM_COMPUTE_ERROR_ON(input2 == nullptr); + ARM_COMPUTE_ERROR_ON(output == nullptr); + + // Create and configure function + auto func = support::cpp14::make_unique(); + func->configure(input0, input1, input2, output, detect_info); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " + << node.name() + << " Type: " << node.type() + << " Target: " << TargetInfo::TargetType + << " Data Type: " << input0->info()->data_type() + << " Input0 shape: " << input0->info()->tensor_shape() + << " Input1 shape: " << input1->info()->tensor_shape() + << " Input2 shape: " << input2->info()->tensor_shape() + << " Output shape: " << output->info()->tensor_shape() + << " DetectionOutputLayer info: " << detect_info + << std::endl); + + return std::move(func); +} /** Create a backend element-wise operation layer function * * @tparam EltwiseFunctions Backend element-wise function diff --git a/arm_compute/graph/backends/ValidateHelpers.h b/arm_compute/graph/backends/ValidateHelpers.h index 75e2363f82..f1e53613ab 100644 --- a/arm_compute/graph/backends/ValidateHelpers.h +++ b/arm_compute/graph/backends/ValidateHelpers.h @@ -203,6 +203,30 @@ Status validate_depthwise_convolution_layer(DepthwiseConvolutionLayerNode &node) return status; } +/** Validates a detection output layer node + * + * @tparam DetectionOutputLayer DetectionOutput layer type + * + * @param[in] node Node to validate + * + * @return Status + */ +template +Status validate_detection_output_layer(DetectionOutputLayerNode &node) +{ + ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating DetectionOutputLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 3); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1); + + // Extract IO and info + arm_compute::ITensorInfo *input0 = get_backing_tensor_info(node.input(0)); + arm_compute::ITensorInfo *input1 = get_backing_tensor_info(node.input(1)); + arm_compute::ITensorInfo *input2 = get_backing_tensor_info(node.input(2)); + arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0)); + const DetectionOutputLayerInfo detect_info = node.detection_output_info(); + + return DetectionOutputLayer::validate(input0, input1, input2, output, detect_info); +} /** Validates a Generate Proposals layer node * diff --git a/arm_compute/graph/frontend/Layers.h b/arm_compute/graph/frontend/Layers.h index d0703317cd..72353a2bbd 100644 --- a/arm_compute/graph/frontend/Layers.h +++ b/arm_compute/graph/frontend/Layers.h @@ -458,7 +458,35 @@ private: int _depth_multiplier; const QuantizationInfo _quant_info; }; +/** DetectionOutput Layer */ +class DetectionOutputLayer final : public ILayer +{ +public: + /** Construct a detection output layer. + * + * @param[in] sub_stream_conf Confidence graph sub-stream. + * @param[in] sub_stream_prior PriorBox graph sub-stream. + * @param[in] detect_info DetectionOutput parameters. + */ + DetectionOutputLayer(SubStream &&sub_stream_conf, SubStream &&sub_stream_prior, DetectionOutputLayerInfo detect_info) + : _ss_conf(std::move(sub_stream_conf)), _ss_prior(std::move(sub_stream_prior)), _detect_info(detect_info) + { + } + NodeID create_layer(IStream &s) override + { + NodeParams common_params = { name(), s.hints().target_hint }; + NodeIdxPair input_loc = { s.tail_node(), 0 }; + NodeIdxPair input_conf = { _ss_conf.tail_node(), 0 }; + NodeIdxPair input_priorbox = { _ss_prior.tail_node(), 0 }; + return GraphBuilder::add_detection_output_node(s.graph(), common_params, input_loc, input_conf, input_priorbox, _detect_info); + } + +private: + SubStream _ss_conf; + SubStream _ss_prior; + DetectionOutputLayerInfo _detect_info; +}; /** Dummy Layer */ class DummyLayer final : public ILayer { diff --git a/arm_compute/graph/nodes/DetectionOutputLayerNode.h b/arm_compute/graph/nodes/DetectionOutputLayerNode.h new file mode 100644 index 0000000000..da1b051528 --- /dev/null +++ b/arm_compute/graph/nodes/DetectionOutputLayerNode.h @@ -0,0 +1,70 @@ +/* + * 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_DETECTION_OUTPUT_LAYER_NODE_H__ +#define __ARM_COMPUTE_GRAPH_DETECTION_OUTPUT_LAYER_NODE_H__ + +#include "arm_compute/graph/INode.h" + +namespace arm_compute +{ +namespace graph +{ +/** DetectionOutput Layer node */ +class DetectionOutputLayerNode final : public INode +{ +public: + /** Constructor + * + * @param[in] detection_info DetectionOutput Layer information + */ + DetectionOutputLayerNode(DetectionOutputLayerInfo detection_info); + /** DetectionOutput metadata accessor + * + * @return DetectionOutput Layer info + */ + DetectionOutputLayerInfo detection_output_info() const; + /** Computes detection output output descriptor + * + * @param[in] input_descriptor Input descriptor + * @param[in] info DetectionOutput operation attributes + * + * @return Output descriptor + */ + static TensorDescriptor compute_output_descriptor(const TensorDescriptor &input_descriptor, const DetectionOutputLayerInfo &info); + + // 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: + DetectionOutputLayerInfo _info; + + // Each detection contains a bounding box, given by its coordinates xmin, ymin, xmax, ymax, associated at the respective image, label and a confidence + static const int detection_size = 7; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_DETECTION_OUTPUT_LAYER_NODE_H__ */ diff --git a/arm_compute/graph/nodes/Nodes.h b/arm_compute/graph/nodes/Nodes.h index 5c7599fbfd..c85c4dc375 100644 --- a/arm_compute/graph/nodes/Nodes.h +++ b/arm_compute/graph/nodes/Nodes.h @@ -33,6 +33,7 @@ #include "arm_compute/graph/nodes/ConvolutionLayerNode.h" #include "arm_compute/graph/nodes/DeconvolutionLayerNode.h" #include "arm_compute/graph/nodes/DepthwiseConvolutionLayerNode.h" +#include "arm_compute/graph/nodes/DetectionOutputLayerNode.h" #include "arm_compute/graph/nodes/DummyNode.h" #include "arm_compute/graph/nodes/EltwiseLayerNode.h" #include "arm_compute/graph/nodes/FlattenLayerNode.h" diff --git a/arm_compute/graph/nodes/NodesFwd.h b/arm_compute/graph/nodes/NodesFwd.h index f956b54c66..542c129ad6 100644 --- a/arm_compute/graph/nodes/NodesFwd.h +++ b/arm_compute/graph/nodes/NodesFwd.h @@ -39,6 +39,7 @@ class ConstNode; class ConvolutionLayerNode; class DeconvolutionLayerNode; class DepthwiseConvolutionLayerNode; +class DetectionOutputLayerNode; class DummyNode; class EltwiseLayerNode; class FlattenLayerNode; -- cgit v1.2.1