1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
9 #include "op_inf_engine.hpp"
10 #include <opencv2/dnn/shape_utils.hpp>
12 #ifdef HAVE_INF_ENGINE
13 #include <ie_extension.h>
14 #include <ie_plugin_dispatcher.hpp>
15 #endif // HAVE_INF_ENGINE
17 namespace cv { namespace dnn {
19 #ifdef HAVE_INF_ENGINE
21 InfEngineBackendNode::InfEngineBackendNode(const InferenceEngine::CNNLayerPtr& _layer)
22 : BackendNode(DNN_BACKEND_INFERENCE_ENGINE), layer(_layer) {}
24 void InfEngineBackendNode::connect(std::vector<Ptr<BackendWrapper> >& inputs,
25 std::vector<Ptr<BackendWrapper> >& outputs)
27 layer->insData.resize(inputs.size());
28 for (int i = 0; i < inputs.size(); ++i)
30 InferenceEngine::DataPtr dataPtr = infEngineDataNode(inputs[i]);
31 layer->insData[i] = InferenceEngine::DataWeakPtr(dataPtr);
32 dataPtr->inputTo[layer->name] = layer;
35 CV_Assert(!outputs.empty());
37 layer->outData.resize(1);
38 InferenceEngine::DataPtr dataPtr = infEngineDataNode(outputs[0]);
39 dataPtr->name = layer->name;
40 layer->outData[0] = dataPtr;
41 dataPtr->creatorLayer = InferenceEngine::CNNLayerWeakPtr(layer);
44 static std::vector<Ptr<InfEngineBackendWrapper> >
45 infEngineWrappers(const std::vector<Ptr<BackendWrapper> >& ptrs)
47 std::vector<Ptr<InfEngineBackendWrapper> > wrappers(ptrs.size());
48 for (int i = 0; i < ptrs.size(); ++i)
50 CV_Assert(!ptrs[i].empty());
51 wrappers[i] = ptrs[i].dynamicCast<InfEngineBackendWrapper>();
52 CV_Assert(!wrappers[i].empty());
57 static InferenceEngine::Layout estimateLayout(const Mat& m)
60 return InferenceEngine::Layout::NCHW;
62 return InferenceEngine::Layout::NC;
64 return InferenceEngine::Layout::ANY;
67 static InferenceEngine::DataPtr wrapToInfEngineDataNode(const Mat& m, const std::string& name = "")
69 std::vector<size_t> reversedShape(&m.size[0], &m.size[0] + m.dims);
70 std::reverse(reversedShape.begin(), reversedShape.end());
71 if (m.type() == CV_32F)
72 return InferenceEngine::DataPtr(
73 new InferenceEngine::Data(name, reversedShape, InferenceEngine::Precision::FP32, estimateLayout(m))
75 else if (m.type() == CV_8U)
76 return InferenceEngine::DataPtr(
77 new InferenceEngine::Data(name, reversedShape, InferenceEngine::Precision::U8, estimateLayout(m))
80 CV_Error(Error::StsNotImplemented, format("Unsupported data type %d", m.type()));
83 InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, const std::vector<size_t>& shape,
84 InferenceEngine::Layout layout)
86 if (m.type() == CV_32F)
87 return InferenceEngine::make_shared_blob<float>(InferenceEngine::Precision::FP32,
88 layout, shape, (float*)m.data);
89 else if (m.type() == CV_8U)
90 return InferenceEngine::make_shared_blob<uint8_t>(InferenceEngine::Precision::U8,
91 layout, shape, (uint8_t*)m.data);
93 CV_Error(Error::StsNotImplemented, format("Unsupported data type %d", m.type()));
96 InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, InferenceEngine::Layout layout)
98 std::vector<size_t> reversedShape(&m.size[0], &m.size[0] + m.dims);
99 std::reverse(reversedShape.begin(), reversedShape.end());
100 return wrapToInfEngineBlob(m, reversedShape, layout);
103 InferenceEngine::DataPtr infEngineDataNode(const Ptr<BackendWrapper>& ptr)
105 CV_Assert(!ptr.empty());
106 Ptr<InfEngineBackendWrapper> p = ptr.dynamicCast<InfEngineBackendWrapper>();
107 CV_Assert(!p.empty());
111 InfEngineBackendWrapper::InfEngineBackendWrapper(int targetId, const cv::Mat& m)
112 : BackendWrapper(DNN_BACKEND_INFERENCE_ENGINE, targetId)
114 dataPtr = wrapToInfEngineDataNode(m);
115 blob = wrapToInfEngineBlob(m, estimateLayout(m));
118 InfEngineBackendWrapper::InfEngineBackendWrapper(Ptr<BackendWrapper> wrapper)
119 : BackendWrapper(DNN_BACKEND_INFERENCE_ENGINE, wrapper->targetId)
121 Ptr<InfEngineBackendWrapper> ieWrapper = wrapper.dynamicCast<InfEngineBackendWrapper>();
122 CV_Assert(!ieWrapper.empty());
123 InferenceEngine::DataPtr srcData = ieWrapper->dataPtr;
124 dataPtr = InferenceEngine::DataPtr(
125 new InferenceEngine::Data(srcData->name, srcData->dims, srcData->precision,
128 blob = ieWrapper->blob;
131 Ptr<BackendWrapper> InfEngineBackendWrapper::create(Ptr<BackendWrapper> wrapper)
133 return Ptr<BackendWrapper>(new InfEngineBackendWrapper(wrapper));
136 InfEngineBackendWrapper::~InfEngineBackendWrapper()
141 void InfEngineBackendWrapper::copyToHost()
146 void InfEngineBackendWrapper::setHostDirty()
151 InfEngineBackendNet::InfEngineBackendNet()
153 targetDevice = InferenceEngine::TargetDevice::eCPU;
154 precision = InferenceEngine::Precision::FP32;
157 InfEngineBackendNet::InfEngineBackendNet(InferenceEngine::CNNNetwork& net)
159 targetDevice = InferenceEngine::TargetDevice::eCPU;
160 precision = InferenceEngine::Precision::FP32;
161 inputs = net.getInputsInfo();
162 outputs = net.getOutputsInfo();
163 layers.resize(net.layerCount()); // A hack to execute InfEngineBackendNet::layerCount correctly.
166 void InfEngineBackendNet::Release() noexcept
173 void InfEngineBackendNet::setPrecision(InferenceEngine::Precision p) noexcept
178 InferenceEngine::Precision InfEngineBackendNet::getPrecision() noexcept
183 // Assume that outputs of network is unconnected blobs.
184 void InfEngineBackendNet::getOutputsInfo(InferenceEngine::OutputsDataMap &outputs_) noexcept
188 void InfEngineBackendNet::getOutputsInfo(InferenceEngine::OutputsDataMap &outputs_) const noexcept
193 // Returns input references that aren't connected to internal outputs.
194 void InfEngineBackendNet::getInputsInfo(InferenceEngine::InputsDataMap &inputs_) noexcept
199 // Returns input references that aren't connected to internal outputs.
200 void InfEngineBackendNet::getInputsInfo(InferenceEngine::InputsDataMap &inputs_) const noexcept
205 InferenceEngine::InputInfo::Ptr InfEngineBackendNet::getInput(const std::string &inputName) noexcept
207 getInputsInfo(inputs);
208 const auto& it = inputs.find(inputName);
209 CV_Assert(it != inputs.end());
213 void InfEngineBackendNet::getName(char*, size_t) noexcept
217 void InfEngineBackendNet::getName(char*, size_t) const noexcept
221 size_t InfEngineBackendNet::layerCount() noexcept
223 return layers.size();
226 InferenceEngine::DataPtr& InfEngineBackendNet::getData(const char *dname) noexcept
228 CV_Error(Error::StsNotImplemented, "");
229 return outputs.begin()->second; // Just return something.
232 void InfEngineBackendNet::addLayer(const InferenceEngine::CNNLayerPtr &layer) noexcept
234 layers.push_back(layer);
239 InferenceEngine::StatusCode
240 InfEngineBackendNet::addOutput(const std::string &layerName, size_t outputIndex,
241 InferenceEngine::ResponseDesc *resp) noexcept
243 for (const auto& l : layers)
245 for (const InferenceEngine::DataPtr& out : l->outData)
247 if (out->name == layerName)
249 outputs[out->name] = out;
250 return InferenceEngine::StatusCode::OK;
254 CV_Error(Error::StsObjectNotFound, "Cannot find a layer " + layerName);
255 return InferenceEngine::StatusCode::OK;
258 InferenceEngine::StatusCode
259 InfEngineBackendNet::getLayerByName(const char *layerName, InferenceEngine::CNNLayerPtr &out,
260 InferenceEngine::ResponseDesc *resp) noexcept
262 for (auto& l : layers)
264 if (l->name == layerName)
267 return InferenceEngine::StatusCode::OK;
270 CV_Error(Error::StsObjectNotFound, cv::format("Cannot find a layer %s", layerName));
271 return InferenceEngine::StatusCode::NOT_FOUND;
274 void InfEngineBackendNet::setTargetDevice(InferenceEngine::TargetDevice device) noexcept
276 if (device != InferenceEngine::TargetDevice::eCPU &&
277 device != InferenceEngine::TargetDevice::eGPU &&
278 device != InferenceEngine::TargetDevice::eMYRIAD)
279 CV_Error(Error::StsNotImplemented, "");
280 targetDevice = device;
283 InferenceEngine::TargetDevice InfEngineBackendNet::getTargetDevice() noexcept
288 InferenceEngine::StatusCode InfEngineBackendNet::setBatchSize(const size_t size) noexcept
290 CV_Error(Error::StsNotImplemented, "");
291 return InferenceEngine::StatusCode::OK;
294 size_t InfEngineBackendNet::getBatchSize() const noexcept
296 CV_Error(Error::StsNotImplemented, "");
300 void InfEngineBackendNet::init(int targetId)
304 // Collect all external input blobs.
306 std::map<std::string, InferenceEngine::DataPtr> internalOutputs;
307 for (const auto& l : layers)
309 for (const InferenceEngine::DataWeakPtr& ptr : l->insData)
311 InferenceEngine::DataPtr inp(ptr);
312 if (internalOutputs.find(inp->name) == internalOutputs.end())
314 InferenceEngine::InputInfo::Ptr inpInfo(new InferenceEngine::InputInfo());
315 inpInfo->setInputData(inp);
316 if (inputs.find(inp->name) == inputs.end())
317 inputs[inp->name] = inpInfo;
320 for (const InferenceEngine::DataPtr& out : l->outData)
322 // TODO: Replace to uniqueness assertion.
323 if (internalOutputs.find(out->name) == internalOutputs.end())
324 internalOutputs[out->name] = out;
327 CV_Assert(!inputs.empty());
332 // Add all unconnected blobs to output blobs.
333 InferenceEngine::OutputsDataMap unconnectedOuts;
334 for (const auto& l : layers)
337 for (const InferenceEngine::DataPtr& out : l->outData)
339 // TODO: Replace to uniqueness assertion.
340 if (unconnectedOuts.find(out->name) == unconnectedOuts.end())
341 unconnectedOuts[out->name] = out;
343 // Remove internally connected outputs.
344 for (const InferenceEngine::DataWeakPtr& inp : l->insData)
346 unconnectedOuts.erase(InferenceEngine::DataPtr(inp)->name);
349 CV_Assert(!unconnectedOuts.empty());
351 for (auto it = unconnectedOuts.begin(); it != unconnectedOuts.end(); ++it)
353 outputs[it->first] = it->second;
357 // Set up input blobs.
359 for (const auto& it : inputs)
361 CV_Assert(allBlobs.find(it.first) != allBlobs.end());
362 inpBlobs[it.first] = allBlobs[it.first];
363 it.second->setPrecision(inpBlobs[it.first]->precision());
366 // Set up output blobs.
368 for (const auto& it : outputs)
370 CV_Assert(allBlobs.find(it.first) != allBlobs.end());
371 outBlobs[it.first] = allBlobs[it.first];
376 case DNN_TARGET_CPU: setTargetDevice(InferenceEngine::TargetDevice::eCPU); break;
377 case DNN_TARGET_OPENCL_FP16: setPrecision(InferenceEngine::Precision::FP16); // Fallback to the next.
378 case DNN_TARGET_OPENCL: setTargetDevice(InferenceEngine::TargetDevice::eGPU); break;
379 case DNN_TARGET_MYRIAD:
381 setPrecision(InferenceEngine::Precision::FP16);
382 setTargetDevice(InferenceEngine::TargetDevice::eMYRIAD); break;
385 CV_Error(Error::StsError, format("Unknown target identifier: %d", targetId));
388 if (!isInitialized())
392 void InfEngineBackendNet::initPlugin(InferenceEngine::ICNNNetwork& net)
394 CV_Assert(!isInitialized());
398 static std::map<std::string, InferenceEngine::InferenceEnginePluginPtr> sharedPlugins;
399 std::string deviceName = InferenceEngine::getDeviceName(targetDevice);
400 auto pluginIt = sharedPlugins.find(deviceName);
401 if (pluginIt != sharedPlugins.end())
403 enginePtr = pluginIt->second;
407 enginePtr = InferenceEngine::PluginDispatcher({""}).getSuitablePlugin(targetDevice);
408 sharedPlugins[deviceName] = enginePtr;
410 if (targetDevice == InferenceEngine::TargetDevice::eCPU)
412 std::string suffixes[] = {"_avx2", "_sse4", ""};
413 bool haveFeature[] = {
414 checkHardwareSupport(CPU_AVX2),
415 checkHardwareSupport(CPU_SSE4_2),
418 for (int i = 0; i < 3; ++i)
423 std::string libName = "cpu_extension" + suffixes[i] + ".dll";
425 std::string libName = "libcpu_extension" + suffixes[i] + ".so";
429 InferenceEngine::IExtensionPtr extension =
430 InferenceEngine::make_so_pointer<InferenceEngine::IExtension>(libName);
431 enginePtr->AddExtension(extension, 0);
436 // Some of networks can work without a library of extra layers.
439 plugin = InferenceEngine::InferencePlugin(enginePtr);
441 netExec = plugin.LoadNetwork(net, {});
442 infRequest = netExec.CreateInferRequest();
443 infRequest.SetInput(inpBlobs);
444 infRequest.SetOutput(outBlobs);
446 catch (const std::exception& ex)
448 CV_Error(Error::StsAssert, format("Failed to initialize Inference Engine backend: %s", ex.what()));
452 bool InfEngineBackendNet::isInitialized()
454 return (bool)enginePtr;
457 void InfEngineBackendNet::addBlobs(const std::vector<Ptr<BackendWrapper> >& ptrs)
459 auto wrappers = infEngineWrappers(ptrs);
460 for (const auto& wrapper : wrappers)
462 allBlobs.insert({wrapper->dataPtr->name, wrapper->blob});
466 void InfEngineBackendNet::forward()
471 Mat infEngineBlobToMat(const InferenceEngine::Blob::Ptr& blob)
473 // NOTE: Inference Engine sizes are reversed.
474 std::vector<size_t> dims = blob->dims();
475 std::vector<int> size(dims.begin(), dims.end());
476 std::reverse(size.begin(), size.end());
477 return Mat(size, CV_32F, (void*)blob->buffer());
480 InfEngineBackendLayer::InfEngineBackendLayer(const InferenceEngine::DataPtr& output_)
485 bool InfEngineBackendLayer::getMemoryShapes(const std::vector<MatShape> &inputs,
486 const int requiredOutputs,
487 std::vector<MatShape> &outputs,
488 std::vector<MatShape> &internals) const
490 std::vector<size_t> dims = output->dims;
491 std::vector<int> shape(dims.begin(), dims.end());
492 std::reverse(shape.begin(), shape.end());
493 outputs.assign(1, shape);
497 bool InfEngineBackendLayer::supportBackend(int backendId)
499 return backendId == DNN_BACKEND_DEFAULT ||
500 backendId == DNN_BACKEND_INFERENCE_ENGINE && haveInfEngine();
503 void InfEngineBackendLayer::forward(std::vector<Mat*> &input, std::vector<Mat> &output,
504 std::vector<Mat> &internals)
506 CV_Error(Error::StsError, "Choose Inference Engine as a preferable backend.");
509 void InfEngineBackendLayer::forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs,
510 OutputArrayOfArrays internals)
512 CV_Error(Error::StsInternal, "Choose Inference Engine as a preferable backend.");
515 InferenceEngine::TBlob<int16_t>::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob)
517 auto halfs = InferenceEngine::make_shared_blob<int16_t>(InferenceEngine::Precision::FP16, blob->layout(), blob->dims());
519 Mat floatsData(1, blob->size(), CV_32F, blob->buffer());
520 Mat halfsData(1, blob->size(), CV_16SC1, halfs->buffer());
521 convertFp16(floatsData, halfsData);
525 #endif // HAVE_INF_ENGINE
529 #ifdef HAVE_INF_ENGINE
533 #endif // HAVE_INF_ENGINE
536 void forwardInfEngine(Ptr<BackendNode>& node)
538 CV_Assert(haveInfEngine());
539 #ifdef HAVE_INF_ENGINE
540 CV_Assert(!node.empty());
541 Ptr<InfEngineBackendNode> ieNode = node.dynamicCast<InfEngineBackendNode>();
542 CV_Assert(!ieNode.empty());
543 ieNode->net->forward();
544 #endif // HAVE_INF_ENGINE
547 }} // namespace dnn, namespace cv