From 0711dab09ddf5a549be6e542150d0b8f90e0e783 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 11 Feb 2019 17:13:39 +0300 Subject: [PATCH] Fix Intel's Inference Engine backend from future. Second try. --- modules/dnn/src/dnn.cpp | 24 ++++++++++++++++++- .../dnn/src/layers/normalize_bbox_layer.cpp | 20 ++++++++++------ modules/dnn/src/layers/resize_layer.cpp | 2 +- modules/dnn/src/op_inf_engine.cpp | 2 +- modules/dnn/src/op_inf_engine.hpp | 3 ++- modules/dnn/test/test_halide_layers.cpp | 5 ++++ 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index f910185b83..fc5548cd8d 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -1637,6 +1637,27 @@ struct Net::Impl preferableTarget == DNN_TARGET_MYRIAD || preferableTarget == DNN_TARGET_FPGA) && !fused) { +#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R5) + bool hasWeights = false; + for (const std::string& name : {"weights", "biases"}) + { + auto it = ieNode->layer.getParameters().find(name); + if (it != ieNode->layer.getParameters().end()) + { + InferenceEngine::Blob::CPtr bp = it->second.as(); + it->second = (InferenceEngine::Blob::CPtr)convertFp16(std::const_pointer_cast(bp)); + hasWeights = true; + } + } + if (!hasWeights) + { + InferenceEngine::Blob::Ptr blob = InferenceEngine::make_shared_blob( + InferenceEngine::Precision::FP16, + InferenceEngine::Layout::C, {1}); + blob->allocate(); + ieNode->layer.getParameters()["weights"] = (InferenceEngine::Blob::CPtr)blob; + } +#else auto& blobs = ieNode->layer.getConstantData(); if (blobs.empty()) { @@ -1653,6 +1674,7 @@ struct Net::Impl for (auto& it : blobs) it.second = convertFp16(std::const_pointer_cast(it.second)); } +#endif } if (!fused) @@ -1724,7 +1746,7 @@ struct Net::Impl if (!ieNode->net->isInitialized()) { -#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R3) +#if INF_ENGINE_VER_MAJOR_EQ(INF_ENGINE_RELEASE_2018R4) // For networks which is built in runtime we need to specify a // version of it's hyperparameters. std::string versionTrigger = "" diff --git a/modules/dnn/src/layers/normalize_bbox_layer.cpp b/modules/dnn/src/layers/normalize_bbox_layer.cpp index 4766f1704e..8e21f116e4 100644 --- a/modules/dnn/src/layers/normalize_bbox_layer.cpp +++ b/modules/dnn/src/layers/normalize_bbox_layer.cpp @@ -276,23 +276,29 @@ public: InferenceEngine::Builder::Layer l = ieLayer; const int numChannels = input->dims[2]; // NOTE: input->dims are reversed (whcn) + InferenceEngine::Blob::Ptr weights; if (blobs.empty()) { - auto weights = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP32, - InferenceEngine::Layout::C, - {(size_t)numChannels}); - weights->allocate(); + auto onesBlob = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP32, + InferenceEngine::Layout::C, + {(size_t)numChannels}); + onesBlob->allocate(); std::vector ones(numChannels, 1); - weights->set(ones); - l.addConstantData("weights", weights); + onesBlob->set(ones); + weights = onesBlob; l.getParameters()["channel_shared"] = false; } else { CV_Assert(numChannels == blobs[0].total()); - l.addConstantData("weights", wrapToInfEngineBlob(blobs[0], {(size_t)numChannels}, InferenceEngine::Layout::C)); + weights = wrapToInfEngineBlob(blobs[0], {(size_t)numChannels}, InferenceEngine::Layout::C); l.getParameters()["channel_shared"] = blobs[0].total() == 1; } +#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R5) + l.getParameters()["weights"] = (InferenceEngine::Blob::CPtr)weights; +#else + l.addConstantData("weights", weights); +#endif l.getParameters()["across_spatial"] = acrossSpatial; return Ptr(new InfEngineBackendNode(l)); } diff --git a/modules/dnn/src/layers/resize_layer.cpp b/modules/dnn/src/layers/resize_layer.cpp index 03d806ad2c..d5ab1acd6e 100644 --- a/modules/dnn/src/layers/resize_layer.cpp +++ b/modules/dnn/src/layers/resize_layer.cpp @@ -173,7 +173,7 @@ public: ieLayer.getParameters()["antialias"] = false; if (scaleWidth != scaleHeight) CV_Error(Error::StsNotImplemented, "resample with sw != sh"); - ieLayer.getParameters()["factor"] = 1.0 / scaleWidth; + ieLayer.getParameters()["factor"] = 1.0f / scaleWidth; } else if (interpolation == "bilinear") { diff --git a/modules/dnn/src/op_inf_engine.cpp b/modules/dnn/src/op_inf_engine.cpp index 0349d44710..a452064337 100644 --- a/modules/dnn/src/op_inf_engine.cpp +++ b/modules/dnn/src/op_inf_engine.cpp @@ -766,7 +766,7 @@ void InfEngineBackendLayer::forward(InputArrayOfArrays inputs, OutputArrayOfArra CV_Error(Error::StsInternal, "Choose Inference Engine as a preferable backend."); } -InferenceEngine::TBlob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob) +InferenceEngine::Blob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob) { auto halfs = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP16, blob->layout(), blob->dims()); halfs->allocate(); diff --git a/modules/dnn/src/op_inf_engine.hpp b/modules/dnn/src/op_inf_engine.hpp index 89e3e339cf..e912725296 100644 --- a/modules/dnn/src/op_inf_engine.hpp +++ b/modules/dnn/src/op_inf_engine.hpp @@ -36,6 +36,7 @@ #define INF_ENGINE_VER_MAJOR_GT(ver) (((INF_ENGINE_RELEASE) / 10000) > ((ver) / 10000)) #define INF_ENGINE_VER_MAJOR_GE(ver) (((INF_ENGINE_RELEASE) / 10000) >= ((ver) / 10000)) #define INF_ENGINE_VER_MAJOR_LT(ver) (((INF_ENGINE_RELEASE) / 10000) < ((ver) / 10000)) +#define INF_ENGINE_VER_MAJOR_EQ(ver) (((INF_ENGINE_RELEASE) / 10000) == ((ver) / 10000)) #if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2018R5) #include @@ -252,7 +253,7 @@ Mat infEngineBlobToMat(const InferenceEngine::Blob::Ptr& blob); // Convert Inference Engine blob with FP32 precision to FP16 precision. // Allocates memory for a new blob. -InferenceEngine::TBlob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob); +InferenceEngine::Blob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob); // This is a fake class to run networks from Model Optimizer. Objects of that // class simulate responses of layers are imported by OpenCV and supported by diff --git a/modules/dnn/test/test_halide_layers.cpp b/modules/dnn/test/test_halide_layers.cpp index 9cbfb0c402..879dd7bbf0 100644 --- a/modules/dnn/test/test_halide_layers.cpp +++ b/modules/dnn/test/test_halide_layers.cpp @@ -694,6 +694,11 @@ TEST_P(Eltwise, Accuracy) Backend backendId = get<0>(get<4>(GetParam())); Target targetId = get<1>(get<4>(GetParam())); +#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE > 2018050000 + if (backendId == DNN_BACKEND_INFERENCE_ENGINE && targetId == DNN_TARGET_OPENCL) + throw SkipTestException(""); +#endif + Net net; std::vector convLayerIds(numConv); -- GitLab