From f7569232711ca499556711577c63d78c9c8b54eb Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 22 Apr 2020 22:33:12 +0000 Subject: [PATCH] dnn(ie): avoid conversion to legacy CNNNetworkImpl --- modules/dnn/src/dnn.cpp | 58 ++++++++++++++++++++++++++--- modules/dnn/test/test_ie_models.cpp | 14 +++++-- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index f8a3dcab9b..0f23ed6d01 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -3157,6 +3157,8 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe { CV_TRACE_FUNCTION(); + CV_TRACE_REGION("register_inputs"); + std::vector inputsNames; std::vector inp_shapes; for (auto& it : ieNet.getInputsInfo()) @@ -3175,6 +3177,8 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe cvNet.setInputShape(inputsNames[inp_id], inp_shapes[inp_id]); } + CV_TRACE_REGION_NEXT("backendNode"); + Ptr backendNode; #ifdef HAVE_DNN_NGRAPH if (DNN_BACKEND_INFERENCE_ENGINE_NGRAPH == getInferenceEngineBackendTypeParam()) @@ -3195,8 +3199,26 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe CV_Error(Error::StsNotImplemented, "This OpenCV version is built without Inference Engine NN Builder API support"); #endif } + + CV_TRACE_REGION_NEXT("register_outputs"); + +#ifdef HAVE_DNN_NGRAPH + auto ngraphFunction = ieNet.getFunction(); +#if INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2020_2) + std::list< std::shared_ptr > ngraphOperations; +#else + std::vector< std::shared_ptr > ngraphOperations; +#endif + if (ngraphFunction) + { + ngraphOperations = ngraphFunction->get_ops(); + } +#endif + for (auto& it : ieNet.getOutputsInfo()) { + CV_TRACE_REGION("output"); + LayerParams lp; int lid = cvNet.addLayer(it.first, "", lp); @@ -3205,15 +3227,38 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe #ifdef HAVE_DNN_NGRAPH if (DNN_BACKEND_INFERENCE_ENGINE_NGRAPH == getInferenceEngineBackendTypeParam()) { + const auto& outputName = it.first; Ptr cvLayer(new NgraphBackendLayer(ieNet)); + cvLayer->name = outputName; + cvLayer->type = "_unknown_"; - InferenceEngine::CNNLayerPtr ieLayer = ieNet.getLayerByName(it.first.c_str()); - CV_Assert(ieLayer); + if (ngraphFunction) + { + CV_TRACE_REGION("ngraph_function"); + bool found = false; + for (const auto& op : ngraphOperations) + { + CV_Assert(op); + if (op->get_friendly_name() == outputName) + { + const std::string typeName = op->get_type_info().name; + cvLayer->type = typeName; + found = true; + break; + } + } + if (!found) + CV_LOG_WARNING(NULL, "DNN/IE: Can't determine output layer type: '" << outputName << "'"); + } + else + { + CV_TRACE_REGION("legacy_cnn_layer"); + InferenceEngine::CNNLayerPtr ieLayer = ieNet.getLayerByName(it.first.c_str()); + CV_Assert(ieLayer); - cvLayer->name = it.first; - cvLayer->type = ieLayer->type; + cvLayer->type = ieLayer->type; + } ld.layerInstance = cvLayer; - ld.backendNodes[DNN_BACKEND_INFERENCE_ENGINE_NGRAPH] = backendNode; } else @@ -3238,6 +3283,9 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe for (int i = 0; i < inputsNames.size(); ++i) cvNet.connect(0, i, lid, i); } + + CV_TRACE_REGION_NEXT("finalize"); + cvNet.setPreferableBackend(getInferenceEngineBackendTypeParam()); cvNet.impl->skipInfEngineInit = true; diff --git a/modules/dnn/test/test_ie_models.cpp b/modules/dnn/test/test_ie_models.cpp index d8096daa7b..4191fa892f 100644 --- a/modules/dnn/test/test_ie_models.cpp +++ b/modules/dnn/test/test_ie_models.cpp @@ -273,9 +273,18 @@ TEST_P(DNNTestOpenVINO, models) const Backend backendId = get<0>(get<0>(GetParam())); const Target targetId = get<1>(get<0>(GetParam())); + std::string modelName = get<1>(GetParam()); - if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) - throw SkipTestException("No support for async forward"); + ASSERT_FALSE(backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) << + "Inference Engine backend is required"; + +#if INF_ENGINE_VER_MAJOR_GE(2020020000) + if (targetId == DNN_TARGET_MYRIAD && backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) + { + if (modelName == "person-detection-retail-0013") // IRv10 + applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION); + } +#endif if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) setInferenceEngineBackendType(CV_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_API); @@ -284,7 +293,6 @@ TEST_P(DNNTestOpenVINO, models) else FAIL() << "Unknown backendId"; - std::string modelName = get<1>(GetParam()); bool isFP16 = (targetId == DNN_TARGET_OPENCL_FP16 || targetId == DNN_TARGET_MYRIAD); const std::map& models = getOpenVINOTestModels();