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 <bsgcomp@arm.com>
Reviewed-by: Pablo Marquez <pablo.tello@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h
index 33a13f1..cb905e7 100644
--- a/arm_compute/graph/GraphBuilder.h
+++ b/arm_compute/graph/GraphBuilder.h
@@ -201,6 +201,18 @@
      * @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 2df2574..573d642 100644
--- a/arm_compute/graph/INodeVisitor.h
+++ b/arm_compute/graph/INodeVisitor.h
@@ -71,6 +71,11 @@
      * @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 @@
     {
         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 d633091..e33c984 100644
--- a/arm_compute/graph/TypePrinter.h
+++ b/arm_compute/graph/TypePrinter.h
@@ -83,6 +83,9 @@
         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 b6803c8..60fe0a8 100644
--- a/arm_compute/graph/Types.h
+++ b/arm_compute/graph/Types.h
@@ -45,6 +45,7 @@
 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 @@
     ConvolutionLayer,
     DeconvolutionLayer,
     DepthwiseConvolutionLayer,
+    DetectionOutputLayer,
     EltwiseLayer,
     FlattenLayer,
     FullyConnectedLayer,
diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h
index 3e71e39..96adffe 100644
--- a/arm_compute/graph/backends/FunctionHelpers.h
+++ b/arm_compute/graph/backends/FunctionHelpers.h
@@ -489,6 +489,51 @@
     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 <typename DetectionOutputLayerFunction, typename TargetInfo>
+std::unique_ptr<IFunction> create_detection_output_layer(DetectionOutputLayerNode &node)
+{
+    validate_node<TargetInfo>(node, 3 /* expected inputs */, 1 /* expected outputs */);
+
+    // Extract IO and info
+    typename TargetInfo::TensorType *input0      = get_backing_tensor<TargetInfo>(node.input(0));
+    typename TargetInfo::TensorType *input1      = get_backing_tensor<TargetInfo>(node.input(1));
+    typename TargetInfo::TensorType *input2      = get_backing_tensor<TargetInfo>(node.input(2));
+    typename TargetInfo::TensorType *output      = get_backing_tensor<TargetInfo>(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<DetectionOutputLayerFunction>();
+    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 75e2363..f1e5361 100644
--- a/arm_compute/graph/backends/ValidateHelpers.h
+++ b/arm_compute/graph/backends/ValidateHelpers.h
@@ -203,6 +203,30 @@
 
     return status;
 }
+/** Validates a detection output layer node
+ *
+ * @tparam DetectionOutputLayer DetectionOutput layer type
+ *
+ * @param[in] node Node to validate
+ *
+ * @return Status
+ */
+template <typename DetectionOutputLayer>
+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 d070331..72353a2 100644
--- a/arm_compute/graph/frontend/Layers.h
+++ b/arm_compute/graph/frontend/Layers.h
@@ -458,7 +458,35 @@
     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 0000000..da1b051
--- /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 5c7599f..c85c4dc 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 f956b54..542c129 100644
--- a/arm_compute/graph/nodes/NodesFwd.h
+++ b/arm_compute/graph/nodes/NodesFwd.h
@@ -39,6 +39,7 @@
 class ConvolutionLayerNode;
 class DeconvolutionLayerNode;
 class DepthwiseConvolutionLayerNode;
+class DetectionOutputLayerNode;
 class DummyNode;
 class EltwiseLayerNode;
 class FlattenLayerNode;