From 27904eecebcb057b4f96e85574d2f441db593dbb Mon Sep 17 00:00:00 2001 From: Evgenya Stepyreva Date: Thu, 27 Aug 2020 11:52:04 +0300 Subject: [PATCH] ExperimentalDetectron* shape infer old-style (#1962) * ExperimentalDetectron* shape infer old-style * nGraph reshape tests --- .../src/inference_engine/generic_ie.cpp | 5 +- .../built-in/ie_built_in_holder.cpp | 10 + .../ie_detectionoutput_onnx_shape_infer.hpp | 51 ++ ...ie_priorgridgenerator_onnx_shape_infer.hpp | 57 +++ .../built-in/ie_proposal_onnx_shape_infer.hpp | 40 ++ ...e_roifeatureextractor_onnx_shape_infer.hpp | 50 ++ .../built-in/ie_topkrois_onnx_shape_infer.hpp | 41 ++ .../inference_engine/ngraph_reshape_tests.cpp | 449 ++++++++++++++++++ 8 files changed, 701 insertions(+), 2 deletions(-) create mode 100644 inference-engine/src/legacy_api/src/shape_infer/built-in/ie_detectionoutput_onnx_shape_infer.hpp create mode 100644 inference-engine/src/legacy_api/src/shape_infer/built-in/ie_priorgridgenerator_onnx_shape_infer.hpp create mode 100644 inference-engine/src/legacy_api/src/shape_infer/built-in/ie_proposal_onnx_shape_infer.hpp create mode 100644 inference-engine/src/legacy_api/src/shape_infer/built-in/ie_roifeatureextractor_onnx_shape_infer.hpp create mode 100644 inference-engine/src/legacy_api/src/shape_infer/built-in/ie_topkrois_onnx_shape_infer.hpp diff --git a/inference-engine/src/inference_engine/generic_ie.cpp b/inference-engine/src/inference_engine/generic_ie.cpp index 23737799004d8f..543eac56a3ca77 100644 --- a/inference-engine/src/inference_engine/generic_ie.cpp +++ b/inference-engine/src/inference_engine/generic_ie.cpp @@ -129,8 +129,9 @@ void ngraph::op::GenericIE::validate_and_infer_types() { } } - // WA: Proposal shape infer has to know number of outputs - if (type == "Proposal" && parameters.find("num_outputs") == parameters.end()) { + // WA: shape infer has to know number of outputs + if ((type == "Proposal" || type == "ExperimentalDetectronROIFeatureExtractor" || type == "ExperimentalDetectronDetectionOutput") + && parameters.find("num_outputs") == parameters.end()) { parameters["num_outputs"] = std::to_string(outputs.size()); } diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_built_in_holder.cpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_built_in_holder.cpp index f2119b64050b11..405906c4bb968d 100644 --- a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_built_in_holder.cpp +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_built_in_holder.cpp @@ -17,6 +17,7 @@ #include "ie_deconv_shape_infer.hpp" #include "ie_deformable_conv_shape_infer.hpp" #include "ie_depth_to_space_shape_infer.hpp" +#include "ie_detectionoutput_onnx_shape_infer.hpp" #include "ie_detection_output_shape_infer.hpp" #include "ie_eltwise_shape_infer.hpp" #include "ie_equal_shape_infer.hpp" @@ -35,6 +36,8 @@ #include "ie_pool_shape_infer.hpp" #include "ie_priorbox_clustered_shape_infer.hpp" #include "ie_priorbox_shape_infer.hpp" +#include "ie_priorgridgenerator_onnx_shape_infer.hpp" +#include "ie_proposal_onnx_shape_infer.hpp" #include "ie_proposal_shape_infer.hpp" #include "ie_psroi_pooling_shape_infer.hpp" #include "ie_quantize_shape_infer.hpp" @@ -48,6 +51,7 @@ #include "ie_rnn_cell_shape_infer.hpp" #include "ie_rnn_shape_infer.hpp" #include "ie_roi_pooling_shape_infer.hpp" +#include "ie_roifeatureextractor_onnx_shape_infer.hpp" #include "ie_scatter_shape_infer.hpp" #include "ie_select_shape_infer.hpp" #include "ie_shape_shape_infer.hpp" @@ -65,6 +69,7 @@ #include "ie_tensor_iterator_shape_infer.hpp" #include "ie_tile_shape_infer.hpp" #include "ie_topk_shape_infer.hpp" +#include "ie_topkrois_onnx_shape_infer.hpp" #include "ie_unique_shape_infer.hpp" #include "ie_unsqueeze_shape_infer.hpp" #include "ie_upsampling_shape_infer.hpp" @@ -157,6 +162,11 @@ REG_SHAPE_INFER_FOR_TYPE(ReshapeShapeProp, Reshape); REG_SHAPE_INFER_FOR_TYPE(DetectionOutputShapeProp, DetectionOutput); REG_SHAPE_INFER_FOR_TYPE(PriorBoxClusteredShapeProp, PriorBoxClustered); REG_SHAPE_INFER_FOR_TYPE(PriorBoxShapeProp, PriorBox); +REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronDetectionOutputShapeProp, ExperimentalDetectronDetectionOutput); +REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronPriorGridGeneratorShapeProp, ExperimentalDetectronPriorGridGenerator); +REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronGenerateProposalsSingleImageShapeProp, ExperimentalDetectronGenerateProposalsSingleImage); +REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronROIFeatureExtractorShapeProp, ExperimentalDetectronROIFeatureExtractor); +REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronTopKROIsShapeProp, ExperimentalDetectronTopKROIs); REG_SHAPE_INFER_FOR_TYPE(RoiPoolingShapeProp, ROIPooling); REG_SHAPE_INFER_FOR_TYPE(PSRoiPoolingShapeProp, PSROIPooling); REG_SHAPE_INFER_FOR_TYPE(UpsamplingShapeProp, Upsampling); diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_detectionoutput_onnx_shape_infer.hpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_detectionoutput_onnx_shape_infer.hpp new file mode 100644 index 00000000000000..4e4dd60803fb8e --- /dev/null +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_detectionoutput_onnx_shape_infer.hpp @@ -0,0 +1,51 @@ +// Copyright (C) 2018-2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "ie_built_in_impl.hpp" + +namespace InferenceEngine { +namespace ShapeInfer { + +/** +*@brief Implementation of Shape inference for ExperimentalDetectronDetectionOutput layer +*/ +class ExperimentalDetectronDetectionOutputShapeProp : public BuiltInShapeInferImpl { +protected: + const int ROIS = 0; + const int FEATMAPS = 1; + +public: + explicit ExperimentalDetectronDetectionOutputShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {} + + void inferShapesImpl(const std::vector& inBlobs, const std::map& params, + const std::map& blobs, std::vector& outShapes) override { + LayerParams lp {}; + CNNLayer cnnLayer(lp); + cnnLayer.params = params; + cnnLayer.type = _type; + validate(&cnnLayer, inBlobs, params, blobs); + + auto rois_num = cnnLayer.GetParamAsUInt("max_detections_per_image"); + outShapes.push_back({rois_num, 4}); + + auto num_outputs = cnnLayer.GetParamAsUInt("num_outputs"); + if (num_outputs > 3) THROW_IE_EXCEPTION << "Incorrect value num_outputs: " << num_outputs; + if (num_outputs >= 2) { + outShapes.push_back({rois_num}); + } + if (num_outputs == 3) { + outShapes.push_back({rois_num}); + } + } +}; + +} // namespace ShapeInfer +} // namespace InferenceEngine diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_priorgridgenerator_onnx_shape_infer.hpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_priorgridgenerator_onnx_shape_infer.hpp new file mode 100644 index 00000000000000..92e8da6113dbb6 --- /dev/null +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_priorgridgenerator_onnx_shape_infer.hpp @@ -0,0 +1,57 @@ +// Copyright (C) 2018-2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include "ie_built_in_impl.hpp" + +namespace InferenceEngine { +namespace ShapeInfer { + +/** +*@brief Implementation of Shape inference for ExperimentalDetectronPriorGridGenerator layer +*/ +class ExperimentalDetectronPriorGridGeneratorShapeProp : public BuiltInShapeInferImpl { +protected: + const int PRIORS = 0; + const int FEATMAP = 1; + const int H = 2; + const int W = 3; + +public: + explicit ExperimentalDetectronPriorGridGeneratorShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {} + + void inferShapesImpl(const std::vector& inBlobs, const std::map& params, + const std::map& blobs, std::vector& outShapes) override { + LayerParams lp {}; + CNNLayer cnnLayer(lp); + cnnLayer.params = params; + cnnLayer.type = _type; + validate(&cnnLayer, inBlobs, params, blobs); + + const auto& priors_shape = inShapes.at(PRIORS); + const auto priors_num = priors_shape.at(0); + const auto& featmap_shape = inShapes.at(FEATMAP); + const auto grid_height = featmap_shape.at(H); + const auto grid_width = featmap_shape.at(W); + + const bool flatten = cnnLayer.GetParamAsBool("flatten", true); + if (flatten) { + outShapes.push_back({grid_height * grid_width * priors_num, 4}); + } else { + outShapes.push_back({grid_height, grid_width, priors_num, 4}); + } + } +}; + +} // namespace ShapeInfer +} // namespace InferenceEngine diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_proposal_onnx_shape_infer.hpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_proposal_onnx_shape_infer.hpp new file mode 100644 index 00000000000000..d2f88363d6f917 --- /dev/null +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_proposal_onnx_shape_infer.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2018-2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "ie_built_in_impl.hpp" + +namespace InferenceEngine { +namespace ShapeInfer { + +/** +*@brief Implementation of Shape inference for ExperimentalDetectronGenerateProposalsSingleImage layer +*/ +class ExperimentalDetectronGenerateProposalsSingleImageShapeProp : public BuiltInShapeInferImpl { +public: + explicit ExperimentalDetectronGenerateProposalsSingleImageShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {} + + void inferShapesImpl(const std::vector& inBlobs, const std::map& params, + const std::map& blobs, std::vector& outShapes) override { + LayerParams lp {}; + CNNLayer cnnLayer(lp); + cnnLayer.params = params; + cnnLayer.type = _type; + validate(&cnnLayer, inBlobs, params, blobs); + + size_t post_nms_count = static_cast(cnnLayer.GetParamAsInt("post_nms_count")); + + outShapes.push_back({post_nms_count, 4}); + outShapes.push_back({post_nms_count, }); + } +}; + +} // namespace ShapeInfer +} // namespace InferenceEngine diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_roifeatureextractor_onnx_shape_infer.hpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_roifeatureextractor_onnx_shape_infer.hpp new file mode 100644 index 00000000000000..bbf6ca20c5ea19 --- /dev/null +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_roifeatureextractor_onnx_shape_infer.hpp @@ -0,0 +1,50 @@ +// Copyright (C) 2018-2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "ie_built_in_impl.hpp" + +namespace InferenceEngine { +namespace ShapeInfer { + +/** +*@brief Implementation of Shape inference for ExperimentalDetectronROIFeatureExtractor layer +*/ +class ExperimentalDetectronROIFeatureExtractorShapeProp : public BuiltInShapeInferImpl { +protected: + const int ROIS = 0; + const int FEATMAPS = 1; + +public: + explicit ExperimentalDetectronROIFeatureExtractorShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {} + + void inferShapesImpl(const std::vector& inBlobs, const std::map& params, + const std::map& blobs, std::vector& outShapes) override { + LayerParams lp {}; + CNNLayer cnnLayer(lp); + cnnLayer.params = params; + cnnLayer.type = _type; + validate(&cnnLayer, inBlobs, params, blobs); + + size_t rois_num = inShapes.at(ROIS).at(0); + size_t channels_num = inShapes.at(FEATMAPS).at(1); + size_t output_size = static_cast(cnnLayer.GetParamAsInt("output_size")); + outShapes.push_back({rois_num, channels_num, output_size, output_size}); + + auto num_outputs = cnnLayer.GetParamAsUInt("num_outputs"); + if (num_outputs > 2) THROW_IE_EXCEPTION << "Incorrect value num_outputs: " << num_outputs; + if (num_outputs == 2) { + outShapes.push_back({rois_num, 4}); + } + } +}; + +} // namespace ShapeInfer +} // namespace InferenceEngine diff --git a/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_topkrois_onnx_shape_infer.hpp b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_topkrois_onnx_shape_infer.hpp new file mode 100644 index 00000000000000..35891b67006bb0 --- /dev/null +++ b/inference-engine/src/legacy_api/src/shape_infer/built-in/ie_topkrois_onnx_shape_infer.hpp @@ -0,0 +1,41 @@ +// Copyright (C) 2018-2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include "ie_built_in_impl.hpp" + +namespace InferenceEngine { +namespace ShapeInfer { + +/** +*@brief Implementation of Shape inference for ExperimentalDetectronTopKROIs layer +*/ +class ExperimentalDetectronTopKROIsShapeProp : public BuiltInShapeInferImpl { +public: + explicit ExperimentalDetectronTopKROIsShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {} + + void inferShapesImpl(const std::vector& inBlobs, const std::map& params, + const std::map& blobs, std::vector& outShapes) override { + LayerParams lp {}; + CNNLayer cnnLayer(lp); + cnnLayer.params = params; + cnnLayer.type = _type; + validate(&cnnLayer, inBlobs, params, blobs); + + const bool max_rois = cnnLayer.GetParamAsInt("max_rois"); + outShapes.push_back({max_rois, 4}); + } +}; + +} // namespace ShapeInfer +} // namespace InferenceEngine diff --git a/inference-engine/tests/functional/inference_engine/ngraph_reshape_tests.cpp b/inference-engine/tests/functional/inference_engine/ngraph_reshape_tests.cpp index 3657c432753f44..e59033e21ba523 100644 --- a/inference-engine/tests/functional/inference_engine/ngraph_reshape_tests.cpp +++ b/inference-engine/tests/functional/inference_engine/ngraph_reshape_tests.cpp @@ -535,3 +535,452 @@ TEST_F(NGraphReshapeTests, ReshapeWithDefaultGenericOps) { ASSERT_NO_THROW(network.reshape(newShapes)); } + +TEST_F(NGraphReshapeTests, ReshapeEDDetectionOutput) { + std::string model = R"V0G0N( + + + + + + + 1000 + 4 + + + + + + + + 1000 + 324 + + + + + + + + 1000 + 81 + + + + + + + + 1 + 3 + + + + + + + + 1000 + 4 + + + 1000 + 324 + + + 1000 + 81 + + + 1 + 3 + + + + + 100 + 4 + + + 100 + + + 100 + + + + + + + 100 + 4 + + + + + + + 100 + + + + + + + 100 + + + + + + + + + + + + + + +)V0G0N"; + InferenceEngine::Core ie; + Blob::Ptr weights; + auto network = ie.ReadNetwork(model, weights); + InferenceEngine::ICNNNetwork::InputShapes newShapes; + newShapes["in0"] = {2000, 4}; + newShapes["in1"] = {2000, 324}; + newShapes["in2"] = {2000, 81}; + + ASSERT_NO_THROW(network.reshape(newShapes)); +} + +TEST_F(NGraphReshapeTests, ReshapeEDPriorGridGenerator) { + std::string model = R"V0G0N( + + + + + + + 3 + 4 + + + + + + + + 1 + 256 + 200 + 336 + + + + + + + + 1000 + 81 + + + + + + + + 3 + 4 + + + 1 + 256 + 200 + 336 + + + 1 + 3 + 800 + 1344 + + + + + 201600 + 4 + + + + + + + 201600 + 4 + + + + + + + + + + + +)V0G0N"; + InferenceEngine::Core ie; + Blob::Ptr weights; + auto network = ie.ReadNetwork(model, weights); + InferenceEngine::ICNNNetwork::InputShapes newShapes; + newShapes["in1"] = {2, 256, 200, 336}; + newShapes["in2"] = {2, 3, 800, 1344}; + ASSERT_NO_THROW(network.reshape(newShapes)); +} + +TEST_F(NGraphReshapeTests, ReshapeEDGenerateProposalsSingleImage) { + std::string model = R"V0G0N( + + + + + + + 3 + + + + + + + + 201600 + 4 + + + + + + + + 12 + 200 + 336 + + + + + + + + 3 + 200 + 336 + + + + + + + + 3 + + + 201600 + 4 + + + 12 + 200 + 336 + + + 3 + 200 + 336 + + + + + 1000 + 4 + + + 1000 + + + + + + + 1000 + 4 + + + + + + + 1000 + + + + + + + + + + + + + +)V0G0N"; + InferenceEngine::Core ie; + Blob::Ptr weights; + auto network = ie.ReadNetwork(model, weights); + InferenceEngine::ICNNNetwork::InputShapes newShapes; + newShapes["in2"] = {12, 200, 300}; + newShapes["in3"] = {2, 200, 300}; + ASSERT_NO_THROW(network.reshape(newShapes)); +} + +TEST_F(NGraphReshapeTests, ReshapeEDROIFeatureExtractor) { + std::string model = R"V0G0N( + + + + + + + 1000 + 4 + + + + + + + + 1 + 256 + 200 + 336 + + + + + + + + 1000 + 4 + + + 1 + 256 + 200 + 336 + + + + + 1000 + 256 + 7 + 7 + + + + + + + 1000 + 256 + 7 + 7 + + + + + + + + + + +)V0G0N"; + InferenceEngine::Core ie; + Blob::Ptr weights; + auto network = ie.ReadNetwork(model, weights); + InferenceEngine::ICNNNetwork::InputShapes newShapes; + newShapes["in0"] = {1, 256, 300, 400}; + newShapes["in1"] = {1000, 256, 7, 7}; + ASSERT_NO_THROW(network.reshape(newShapes)); +} + +TEST_F(NGraphReshapeTests, ReshapeEDTopKROIs) { + std::string model = R"V0G0N( + + + + + + + 5000 + 4 + + + + + + + + 5000 + + + + + + + + 5000 + 4 + + + 5000 + + + + + 1000 + 4 + + + + + + + 1000 + 4 + + + + + + + + + + +)V0G0N"; + InferenceEngine::Core ie; + Blob::Ptr weights; + auto network = ie.ReadNetwork(model, weights); + InferenceEngine::ICNNNetwork::InputShapes newShapes; + newShapes["in0"] = {10000, 4}; + newShapes["in1"] = {10000}; + ASSERT_NO_THROW(network.reshape(newShapes)); +}