Skip to content

Commit

Permalink
Merge pull request opencv#17134 from alalek:dnn_ie_avoid_conversion_t…
Browse files Browse the repository at this point in the history
…o_legacy
  • Loading branch information
alalek committed Apr 23, 2020
2 parents 10808cc + f756923 commit 2df978b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
58 changes: 53 additions & 5 deletions modules/dnn/src/dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,8 @@ Net Net::Impl::createNetworkFromModelOptimizer(InferenceEngine::CNNNetwork& ieNe
{
CV_TRACE_FUNCTION();

CV_TRACE_REGION("register_inputs");

std::vector<String> inputsNames;
std::vector<MatShape> inp_shapes;
for (auto& it : ieNet.getInputsInfo())
Expand All @@ -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> backendNode;
#ifdef HAVE_DNN_NGRAPH
if (DNN_BACKEND_INFERENCE_ENGINE_NGRAPH == getInferenceEngineBackendTypeParam())
Expand All @@ -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<ngraph::Node> > ngraphOperations;
#else
std::vector< std::shared_ptr<ngraph::Node> > 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);

Expand All @@ -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<Layer> 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
Expand All @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions modules/dnn/test/test_ie_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,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());

ASSERT_FALSE(backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) <<
"Inference Engine backend is required";

if (backendId != DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
throw SkipTestException("No support for async forward");
#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);
Expand All @@ -288,7 +297,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<std::string, OpenVINOModelTestCaseInfo>& models = getOpenVINOTestModels();
Expand Down

0 comments on commit 2df978b

Please sign in to comment.