From a7acb3cbabeb66ce647684466a04c96b2963c9c9 Mon Sep 17 00:00:00 2001 From: Isabella Gottardi Date: Tue, 8 Jan 2019 13:48:44 +0000 Subject: COMPMID-1849: Implement CPPDetectionPostProcessLayer * Add DetectionPostProcessLayer * Add DetectionPostProcessLayer at the graph Change-Id: I7e56f6cffc26f112d26dfe74853085bb8ec7d849 Signed-off-by: Isabella Gottardi Reviewed-on: https://review.mlplatform.org/c/1639 Reviewed-by: Giuseppe Rossini Tested-by: Arm Jenkins --- src/graph/GraphBuilder.cpp | 30 +++++++ src/graph/backends/CL/CLFunctionsFactory.cpp | 58 ++++++++++++ src/graph/backends/CL/CLNodeValidator.cpp | 2 + src/graph/backends/GLES/GCNodeValidator.cpp | 2 + src/graph/backends/NEON/NEFunctionFactory.cpp | 2 + src/graph/backends/NEON/NENodeValidator.cpp | 2 + src/graph/nodes/DetectionPostProcessLayerNode.cpp | 104 ++++++++++++++++++++++ 7 files changed, 200 insertions(+) create mode 100644 src/graph/nodes/DetectionPostProcessLayerNode.cpp (limited to 'src/graph') diff --git a/src/graph/GraphBuilder.cpp b/src/graph/GraphBuilder.cpp index 54bd066712..228f2d211a 100644 --- a/src/graph/GraphBuilder.cpp +++ b/src/graph/GraphBuilder.cpp @@ -393,6 +393,36 @@ NodeID GraphBuilder::add_detection_output_node(Graph &g, NodeParams params, Node return detect_nid; } +NodeID GraphBuilder::add_detection_post_process_node(Graph &g, NodeParams params, NodeIdxPair input_box_encoding, NodeIdxPair input_class_prediction, const DetectionPostProcessLayerInfo &detect_info, + ITensorAccessorUPtr anchors_accessor, const QuantizationInfo &anchor_quant_info) +{ + check_nodeidx_pair(input_box_encoding, g); + check_nodeidx_pair(input_class_prediction, g); + + // Get input tensor descriptor + const TensorDescriptor input_box_encoding_tensor_desc = get_tensor_descriptor(g, g.node(input_box_encoding.node_id)->outputs()[0]); + + // Calculate anchor descriptor + TensorDescriptor anchor_desc = input_box_encoding_tensor_desc; + if(!anchor_quant_info.empty()) + { + anchor_desc.quant_info = anchor_quant_info; + } + + // Create anchors nodes + auto anchors_nid = add_const_node_with_name(g, params, "Anchors", anchor_desc, std::move(anchors_accessor)); + + // Create detection_output node and connect + NodeID detect_nid = g.add_node(detect_info); + g.add_connection(input_box_encoding.node_id, input_box_encoding.index, detect_nid, 0); + g.add_connection(input_class_prediction.node_id, input_class_prediction.index, detect_nid, 1); + g.add_connection(anchors_nid, 0, detect_nid, 2); + + set_node_params(g, detect_nid, params); + + return detect_nid; +} + NodeID GraphBuilder::add_dummy_node(Graph &g, NodeParams params, NodeIdxPair input, TensorShape shape) { return create_simple_single_input_output_node(g, params, input, shape); diff --git a/src/graph/backends/CL/CLFunctionsFactory.cpp b/src/graph/backends/CL/CLFunctionsFactory.cpp index b9f22f6199..82b6dd6a54 100644 --- a/src/graph/backends/CL/CLFunctionsFactory.cpp +++ b/src/graph/backends/CL/CLFunctionsFactory.cpp @@ -166,6 +166,62 @@ std::unique_ptr create_detection_output_layer +std::unique_ptr create_detection_post_process_layer(DetectionPostProcessLayerNode &node) +{ + validate_node(node, 3 /* expected inputs */, 4 /* expected outputs */); + + // Extract IO and info + CLTargetInfo::TensorType *input0 = get_backing_tensor(node.input(0)); + CLTargetInfo::TensorType *input1 = get_backing_tensor(node.input(1)); + CLTargetInfo::TensorType *input2 = get_backing_tensor(node.input(2)); + CLTargetInfo::TensorType *output0 = get_backing_tensor(node.output(0)); + CLTargetInfo::TensorType *output1 = get_backing_tensor(node.output(1)); + CLTargetInfo::TensorType *output2 = get_backing_tensor(node.output(2)); + CLTargetInfo::TensorType *output3 = get_backing_tensor(node.output(3)); + const DetectionPostProcessLayerInfo detect_info = node.detection_post_process_info(); + + ARM_COMPUTE_ERROR_ON(input0 == nullptr); + ARM_COMPUTE_ERROR_ON(input1 == nullptr); + ARM_COMPUTE_ERROR_ON(input2 == nullptr); + ARM_COMPUTE_ERROR_ON(output0 == nullptr); + ARM_COMPUTE_ERROR_ON(output1 == nullptr); + ARM_COMPUTE_ERROR_ON(output2 == nullptr); + ARM_COMPUTE_ERROR_ON(output3 == nullptr); + + // Create and configure function + auto func = support::cpp14::make_unique(); + func->configure(input0, input1, input2, output0, output1, output2, output3, detect_info); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " + << node.name() + << " Type: " << node.type() + << " Target: " << CLTargetInfo::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() + << " Output0 shape: " << output0->info()->tensor_shape() + << " Output1 shape: " << output1->info()->tensor_shape() + << " Output2 shape: " << output2->info()->tensor_shape() + << " Output3 shape: " << output3->info()->tensor_shape() + << " DetectionPostProcessLayer info: " << detect_info + << std::endl); + + auto wrap_function = support::cpp14::make_unique(); + + wrap_function->register_function(std::move(func)); + wrap_function->register_tensor(input0); + wrap_function->register_tensor(input1); + wrap_function->register_tensor(input2); + wrap_function->register_tensor(output0); + wrap_function->register_tensor(output1); + wrap_function->register_tensor(output2); + wrap_function->register_tensor(output3); + + return std::move(wrap_function); +} } // namespace detail std::unique_ptr CLFunctionFactory::create(INode *node, GraphContext &ctx) @@ -196,6 +252,8 @@ std::unique_ptr CLFunctionFactory::create(INode *node, GraphContext & return detail::create_depthwise_convolution_layer(*polymorphic_downcast(node)); case NodeType::DetectionOutputLayer: return detail::create_detection_output_layer(*polymorphic_downcast(node)); + case NodeType::DetectionPostProcessLayer: + return detail::create_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::EltwiseLayer: return detail::create_eltwise_layer(*polymorphic_downcast(node)); case NodeType::FlattenLayer: diff --git a/src/graph/backends/CL/CLNodeValidator.cpp b/src/graph/backends/CL/CLNodeValidator.cpp index 78771102e8..40ec508767 100644 --- a/src/graph/backends/CL/CLNodeValidator.cpp +++ b/src/graph/backends/CL/CLNodeValidator.cpp @@ -62,6 +62,8 @@ Status CLNodeValidator::validate(INode *node) CLDepthwiseConvolutionLayer3x3>(*polymorphic_downcast(node)); case NodeType::DetectionOutputLayer: return detail::validate_detection_output_layer(*polymorphic_downcast(node)); + case NodeType::DetectionPostProcessLayer: + return detail::validate_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::GenerateProposalsLayer: return detail::validate_generate_proposals_layer(*polymorphic_downcast(node)); case NodeType::NormalizePlanarYUVLayer: diff --git a/src/graph/backends/GLES/GCNodeValidator.cpp b/src/graph/backends/GLES/GCNodeValidator.cpp index a767d7b107..9cbb9a12ef 100644 --- a/src/graph/backends/GLES/GCNodeValidator.cpp +++ b/src/graph/backends/GLES/GCNodeValidator.cpp @@ -113,6 +113,8 @@ Status GCNodeValidator::validate(INode *node) return validate_depthwise_convolution_layer(*polymorphic_downcast(node)); case NodeType::DetectionOutputLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : DetectionOutputLayer"); + case NodeType::DetectionPostProcessLayer: + return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : DetectionPostProcessLayer"); case NodeType::FlattenLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : FlattenLayer"); case NodeType::GenerateProposalsLayer: diff --git a/src/graph/backends/NEON/NEFunctionFactory.cpp b/src/graph/backends/NEON/NEFunctionFactory.cpp index b808ef81f9..852de549fa 100644 --- a/src/graph/backends/NEON/NEFunctionFactory.cpp +++ b/src/graph/backends/NEON/NEFunctionFactory.cpp @@ -215,6 +215,8 @@ std::unique_ptr NEFunctionFactory::create(INode *node, GraphContext & return detail::create_depthwise_convolution_layer(*polymorphic_downcast(node)); case NodeType::DetectionOutputLayer: return detail::create_detection_output_layer(*polymorphic_downcast(node)); + case NodeType::DetectionPostProcessLayer: + return detail::create_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::EltwiseLayer: return detail::create_eltwise_layer(*polymorphic_downcast(node)); case NodeType::FlattenLayer: diff --git a/src/graph/backends/NEON/NENodeValidator.cpp b/src/graph/backends/NEON/NENodeValidator.cpp index 3b1d2aa59c..734b3401f7 100644 --- a/src/graph/backends/NEON/NENodeValidator.cpp +++ b/src/graph/backends/NEON/NENodeValidator.cpp @@ -62,6 +62,8 @@ Status NENodeValidator::validate(INode *node) NEDepthwiseConvolutionLayer3x3>(*polymorphic_downcast(node)); case NodeType::DetectionOutputLayer: return detail::validate_detection_output_layer(*polymorphic_downcast(node)); + case NodeType::DetectionPostProcessLayer: + return detail::validate_detection_post_process_layer(*polymorphic_downcast(node)); case NodeType::GenerateProposalsLayer: return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported operation : GenerateProposalsLayer"); case NodeType::NormalizePlanarYUVLayer: diff --git a/src/graph/nodes/DetectionPostProcessLayerNode.cpp b/src/graph/nodes/DetectionPostProcessLayerNode.cpp new file mode 100644 index 0000000000..4a5df1ac4e --- /dev/null +++ b/src/graph/nodes/DetectionPostProcessLayerNode.cpp @@ -0,0 +1,104 @@ +/* + * 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/graph/nodes/DetectionPostProcessLayerNode.h" + +#include "arm_compute/core/Utils.h" +#include "arm_compute/graph/Graph.h" +#include "arm_compute/graph/INodeVisitor.h" +#include "arm_compute/graph/Utils.h" + +namespace arm_compute +{ +namespace graph +{ +DetectionPostProcessLayerNode::DetectionPostProcessLayerNode(DetectionPostProcessLayerInfo detection_info) + : _info(detection_info) +{ + _input_edges.resize(3, EmptyEdgeID); + _outputs.resize(4, NullTensorID); +} + +DetectionPostProcessLayerInfo DetectionPostProcessLayerNode::detection_post_process_info() const +{ + return _info; +} + +bool DetectionPostProcessLayerNode::forward_descriptors() +{ + if((input_id(0) != NullTensorID) && (input_id(1) != NullTensorID) && (input_id(2) != NullTensorID) && (output_id(0) != NullTensorID) && (output_id(1) != NullTensorID) + && (output_id(2) != NullTensorID) && (output_id(3) != NullTensorID)) + { + for(unsigned int i = 0; i < 4; ++i) + { + Tensor *dst = output(i); + ARM_COMPUTE_ERROR_ON(dst == nullptr); + dst->desc() = configure_output(i); + } + return true; + } + return false; +} + +TensorDescriptor DetectionPostProcessLayerNode::configure_output(size_t idx) const +{ + ARM_COMPUTE_UNUSED(idx); + ARM_COMPUTE_ERROR_ON(idx >= _outputs.size()); + + TensorDescriptor output_desc; + const unsigned int num_detected_box = _info.max_detections() * _info.max_classes_per_detection(); + + switch(idx) + { + case 0: + // Configure boxes output + output_desc.shape = TensorShape(kNumCoordBox, num_detected_box, kBatchSize); + break; + case 1: + case 2: + // Configure classes or scores output + output_desc.shape = TensorShape(num_detected_box, kBatchSize); + break; + case 3: + // Configure num_detection + output_desc.shape = TensorShape(1); + break; + default: + ARM_COMPUTE_ERROR("Unsupported output index"); + } + output_desc.data_type = DataType::F32; + + return output_desc; +} + +NodeType DetectionPostProcessLayerNode::type() const +{ + return NodeType::DetectionPostProcessLayer; +} + +void DetectionPostProcessLayerNode::accept(INodeVisitor &v) +{ + v.visit(*this); +} +} // namespace graph +} // namespace arm_compute \ No newline at end of file -- cgit v1.2.1