2018-02-06 16:57:35 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
//
|
2019-03-29 21:42:58 +08:00
|
|
|
// Copyright (C) 2018-2019, Intel Corporation, all rights reserved.
|
2018-02-06 16:57:35 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
|
|
|
|
#ifndef __OPENCV_DNN_OP_INF_ENGINE_HPP__
|
|
|
|
#define __OPENCV_DNN_OP_INF_ENGINE_HPP__
|
|
|
|
|
2018-07-20 00:22:23 +08:00
|
|
|
#include "opencv2/core/cvdef.h"
|
2018-07-28 00:56:35 +08:00
|
|
|
#include "opencv2/core/cvstd.hpp"
|
|
|
|
#include "opencv2/dnn.hpp"
|
2018-07-20 00:22:23 +08:00
|
|
|
|
2019-05-01 19:51:12 +08:00
|
|
|
#include "opencv2/core/async.hpp"
|
|
|
|
#include "opencv2/core/detail/async_promise.hpp"
|
|
|
|
|
2019-03-29 21:42:58 +08:00
|
|
|
#include "opencv2/dnn/utils/inference_engine.hpp"
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
#ifdef HAVE_INF_ENGINE
|
2018-07-30 23:21:17 +08:00
|
|
|
|
2018-12-20 21:41:14 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2018R5 2018050000
|
2019-04-01 20:00:25 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2019R1 2019010000
|
2019-06-24 19:57:44 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2019R2 2019020000
|
2019-09-02 20:35:35 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2019R3 2019030000
|
2020-01-14 21:20:12 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2020_1 2020010000
|
2020-02-27 19:28:50 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2020_2 2020020000
|
2020-04-07 21:17:21 +08:00
|
|
|
#define INF_ENGINE_RELEASE_2020_3 2020030000
|
2018-07-30 23:21:17 +08:00
|
|
|
|
|
|
|
#ifndef INF_ENGINE_RELEASE
|
2020-04-07 21:17:21 +08:00
|
|
|
#warning("IE version have not been provided via command-line. Using 2020.3 by default")
|
|
|
|
#define INF_ENGINE_RELEASE INF_ENGINE_RELEASE_2020_3
|
2018-07-30 23:21:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define INF_ENGINE_VER_MAJOR_GT(ver) (((INF_ENGINE_RELEASE) / 10000) > ((ver) / 10000))
|
2018-08-27 20:45:44 +08:00
|
|
|
#define INF_ENGINE_VER_MAJOR_GE(ver) (((INF_ENGINE_RELEASE) / 10000) >= ((ver) / 10000))
|
2019-01-14 14:55:44 +08:00
|
|
|
#define INF_ENGINE_VER_MAJOR_LT(ver) (((INF_ENGINE_RELEASE) / 10000) < ((ver) / 10000))
|
2019-04-16 23:13:14 +08:00
|
|
|
#define INF_ENGINE_VER_MAJOR_LE(ver) (((INF_ENGINE_RELEASE) / 10000) <= ((ver) / 10000))
|
2019-02-11 22:13:39 +08:00
|
|
|
#define INF_ENGINE_VER_MAJOR_EQ(ver) (((INF_ENGINE_RELEASE) / 10000) == ((ver) / 10000))
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2019-04-16 23:13:14 +08:00
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 5
|
|
|
|
//#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsuggest-override"
|
|
|
|
#endif
|
|
|
|
|
2020-03-03 16:01:44 +08:00
|
|
|
#ifdef HAVE_DNN_IE_NN_BUILDER_2019
|
2019-06-27 22:04:10 +08:00
|
|
|
//#define INFERENCE_ENGINE_DEPRECATED // turn off deprecation warnings from IE
|
Fix modules/ typos
Found using `codespell -q 3 -S ./3rdparty -L activ,amin,ang,atleast,childs,dof,endwhile,halfs,hist,iff,nd,od,uint`
backporting of commit: ec43292e1ea9da963e67427505b4113750829c3e
2019-08-16 06:02:09 +08:00
|
|
|
//there is no way to suppress warnings from IE only at this moment, so we are forced to suppress warnings globally
|
2019-06-27 22:04:10 +08:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable: 4996) // was declared deprecated
|
|
|
|
#endif
|
2020-03-03 16:01:44 +08:00
|
|
|
#endif // HAVE_DNN_IE_NN_BUILDER_2019
|
2019-06-27 22:04:10 +08:00
|
|
|
|
2020-02-12 22:22:44 +08:00
|
|
|
#if defined(__GNUC__) && INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2020_1)
|
2019-04-16 23:13:14 +08:00
|
|
|
#pragma GCC visibility push(default)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <inference_engine.hpp>
|
|
|
|
|
2020-04-13 04:36:21 +08:00
|
|
|
#ifdef HAVE_DNN_IE_NN_BUILDER_2019
|
2019-01-14 14:55:44 +08:00
|
|
|
#include <ie_builders.hpp>
|
2020-04-13 04:36:21 +08:00
|
|
|
#endif
|
2018-07-30 23:21:17 +08:00
|
|
|
|
2020-02-12 22:22:44 +08:00
|
|
|
#if defined(__GNUC__) && INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2020_1)
|
2019-04-16 23:13:14 +08:00
|
|
|
#pragma GCC visibility pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 5
|
|
|
|
//#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
#endif // HAVE_INF_ENGINE
|
|
|
|
|
|
|
|
namespace cv { namespace dnn {
|
|
|
|
|
|
|
|
#ifdef HAVE_INF_ENGINE
|
|
|
|
|
2019-12-02 21:16:06 +08:00
|
|
|
Backend& getInferenceEngineBackendTypeParam();
|
|
|
|
|
2020-03-03 16:01:44 +08:00
|
|
|
Mat infEngineBlobToMat(const InferenceEngine::Blob::Ptr& blob);
|
|
|
|
|
|
|
|
void infEngineBlobsToMats(const std::vector<InferenceEngine::Blob::Ptr>& blobs,
|
|
|
|
std::vector<Mat>& mats);
|
|
|
|
|
|
|
|
#ifdef HAVE_DNN_IE_NN_BUILDER_2019
|
|
|
|
|
2019-01-14 14:55:44 +08:00
|
|
|
class InfEngineBackendNet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InfEngineBackendNet();
|
|
|
|
|
|
|
|
InfEngineBackendNet(InferenceEngine::CNNNetwork& net);
|
|
|
|
|
2019-02-14 18:30:30 +08:00
|
|
|
void addLayer(InferenceEngine::Builder::Layer& layer);
|
2019-01-14 14:55:44 +08:00
|
|
|
|
|
|
|
void addOutput(const std::string& name);
|
|
|
|
|
|
|
|
void connect(const std::vector<Ptr<BackendWrapper> >& inputs,
|
|
|
|
const std::vector<Ptr<BackendWrapper> >& outputs,
|
|
|
|
const std::string& layerName);
|
|
|
|
|
|
|
|
bool isInitialized();
|
|
|
|
|
2019-12-02 21:16:06 +08:00
|
|
|
void init(Target targetId);
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2019-04-20 02:01:19 +08:00
|
|
|
void forward(const std::vector<Ptr<BackendWrapper> >& outBlobsWrappers,
|
|
|
|
bool isAsync);
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2019-08-07 03:20:26 +08:00
|
|
|
void initPlugin(InferenceEngine::CNNNetwork& net);
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2019-06-27 18:13:48 +08:00
|
|
|
void addBlobs(const std::vector<cv::Ptr<BackendWrapper> >& ptrs);
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2020-04-19 00:42:48 +08:00
|
|
|
void reset();
|
|
|
|
|
2019-01-14 14:55:44 +08:00
|
|
|
private:
|
|
|
|
InferenceEngine::Builder::Network netBuilder;
|
|
|
|
|
|
|
|
InferenceEngine::ExecutableNetwork netExec;
|
|
|
|
InferenceEngine::BlobMap allBlobs;
|
2019-08-07 03:20:26 +08:00
|
|
|
std::string device_name;
|
|
|
|
#if INF_ENGINE_VER_MAJOR_LE(2019010000)
|
|
|
|
InferenceEngine::InferenceEnginePluginPtr enginePtr;
|
|
|
|
InferenceEngine::InferencePlugin plugin;
|
|
|
|
#else
|
|
|
|
bool isInit = false;
|
|
|
|
#endif
|
2019-01-14 14:55:44 +08:00
|
|
|
|
2019-04-20 02:01:19 +08:00
|
|
|
struct InfEngineReqWrapper
|
|
|
|
{
|
|
|
|
InfEngineReqWrapper() : isReady(true) {}
|
|
|
|
|
|
|
|
void makePromises(const std::vector<Ptr<BackendWrapper> >& outs);
|
|
|
|
|
|
|
|
InferenceEngine::InferRequest req;
|
2019-05-01 19:51:12 +08:00
|
|
|
std::vector<cv::AsyncPromise> outProms;
|
2019-04-20 02:01:19 +08:00
|
|
|
std::vector<std::string> outsNames;
|
|
|
|
bool isReady;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Ptr<InfEngineReqWrapper> > infRequests;
|
|
|
|
|
2019-01-14 14:55:44 +08:00
|
|
|
InferenceEngine::CNNNetwork cnn;
|
|
|
|
bool hasNetOwner;
|
|
|
|
|
|
|
|
std::map<std::string, int> layers;
|
|
|
|
std::vector<std::string> requestedOutputs;
|
|
|
|
|
2019-11-28 00:37:56 +08:00
|
|
|
std::set<std::pair<int, int> > unconnectedPorts;
|
2019-01-14 14:55:44 +08:00
|
|
|
};
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
class InfEngineBackendNode : public BackendNode
|
|
|
|
{
|
|
|
|
public:
|
2019-01-14 14:55:44 +08:00
|
|
|
InfEngineBackendNode(const InferenceEngine::Builder::Layer& layer);
|
2018-02-06 16:57:35 +08:00
|
|
|
|
2019-09-03 23:58:57 +08:00
|
|
|
InfEngineBackendNode(Ptr<Layer>& layer, std::vector<Mat*>& inputs,
|
|
|
|
std::vector<Mat>& outputs, std::vector<Mat>& internals);
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
void connect(std::vector<Ptr<BackendWrapper> >& inputs,
|
|
|
|
std::vector<Ptr<BackendWrapper> >& outputs);
|
|
|
|
|
|
|
|
// Inference Engine network object that allows to obtain the outputs of this layer.
|
2019-01-14 14:55:44 +08:00
|
|
|
InferenceEngine::Builder::Layer layer;
|
2018-02-06 16:57:35 +08:00
|
|
|
Ptr<InfEngineBackendNet> net;
|
2019-09-03 23:58:57 +08:00
|
|
|
// CPU fallback in case of unsupported Inference Engine layer.
|
|
|
|
Ptr<dnn::Layer> cvLayer;
|
2018-02-06 16:57:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class InfEngineBackendWrapper : public BackendWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InfEngineBackendWrapper(int targetId, const Mat& m);
|
|
|
|
|
2018-06-05 04:51:28 +08:00
|
|
|
InfEngineBackendWrapper(Ptr<BackendWrapper> wrapper);
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
~InfEngineBackendWrapper();
|
|
|
|
|
2018-06-05 04:51:28 +08:00
|
|
|
static Ptr<BackendWrapper> create(Ptr<BackendWrapper> wrapper);
|
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
virtual void copyToHost() CV_OVERRIDE;
|
2018-02-06 16:57:35 +08:00
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
virtual void setHostDirty() CV_OVERRIDE;
|
2018-02-06 16:57:35 +08:00
|
|
|
|
|
|
|
InferenceEngine::DataPtr dataPtr;
|
2018-06-05 04:51:28 +08:00
|
|
|
InferenceEngine::Blob::Ptr blob;
|
2019-05-01 19:51:12 +08:00
|
|
|
AsyncArray futureMat;
|
2018-02-06 16:57:35 +08:00
|
|
|
};
|
|
|
|
|
2018-06-05 04:51:28 +08:00
|
|
|
InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, InferenceEngine::Layout layout = InferenceEngine::Layout::ANY);
|
2018-02-06 16:57:35 +08:00
|
|
|
|
2018-06-05 04:51:28 +08:00
|
|
|
InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, const std::vector<size_t>& shape, InferenceEngine::Layout layout);
|
2018-02-06 16:57:35 +08:00
|
|
|
|
|
|
|
InferenceEngine::DataPtr infEngineDataNode(const Ptr<BackendWrapper>& ptr);
|
|
|
|
|
2018-03-12 22:35:28 +08:00
|
|
|
// Convert Inference Engine blob with FP32 precision to FP16 precision.
|
|
|
|
// Allocates memory for a new blob.
|
2019-02-11 22:13:39 +08:00
|
|
|
InferenceEngine::Blob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob);
|
2018-02-06 16:57:35 +08:00
|
|
|
|
2019-02-14 18:30:30 +08:00
|
|
|
void addConstantData(const std::string& name, InferenceEngine::Blob::Ptr data, InferenceEngine::Builder::Layer& l);
|
|
|
|
|
2018-03-17 00:27:04 +08:00
|
|
|
// 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
|
|
|
|
// Inference Engine. The main difference is that they do not perform forward pass.
|
|
|
|
class InfEngineBackendLayer : public Layer
|
|
|
|
{
|
|
|
|
public:
|
2019-01-11 01:29:44 +08:00
|
|
|
InfEngineBackendLayer(const InferenceEngine::CNNNetwork &t_net_) : t_net(t_net_) {};
|
2018-03-17 00:27:04 +08:00
|
|
|
|
|
|
|
virtual bool getMemoryShapes(const std::vector<MatShape> &inputs,
|
|
|
|
const int requiredOutputs,
|
|
|
|
std::vector<MatShape> &outputs,
|
2018-03-15 21:16:56 +08:00
|
|
|
std::vector<MatShape> &internals) const CV_OVERRIDE;
|
2018-03-17 00:27:04 +08:00
|
|
|
|
|
|
|
virtual void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs,
|
2018-03-15 21:16:56 +08:00
|
|
|
OutputArrayOfArrays internals) CV_OVERRIDE;
|
2018-03-17 00:27:04 +08:00
|
|
|
|
2018-03-15 21:16:56 +08:00
|
|
|
virtual bool supportBackend(int backendId) CV_OVERRIDE;
|
2018-03-17 00:27:04 +08:00
|
|
|
|
|
|
|
private:
|
2019-01-11 01:29:44 +08:00
|
|
|
InferenceEngine::CNNNetwork t_net;
|
2018-03-17 00:27:04 +08:00
|
|
|
};
|
|
|
|
|
2019-12-02 21:16:06 +08:00
|
|
|
class InfEngineExtension : public InferenceEngine::IExtension
|
|
|
|
{
|
|
|
|
public:
|
2020-06-13 01:27:21 +08:00
|
|
|
#if INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2020_2)
|
2019-12-02 21:16:06 +08:00
|
|
|
virtual void SetLogCallback(InferenceEngine::IErrorListener&) noexcept {}
|
2020-06-13 01:27:21 +08:00
|
|
|
#endif
|
2019-12-02 21:16:06 +08:00
|
|
|
virtual void Unload() noexcept {}
|
|
|
|
virtual void Release() noexcept {}
|
|
|
|
virtual void GetVersion(const InferenceEngine::Version*&) const noexcept {}
|
|
|
|
|
|
|
|
virtual InferenceEngine::StatusCode getPrimitiveTypes(char**&, unsigned int&,
|
|
|
|
InferenceEngine::ResponseDesc*) noexcept
|
|
|
|
{
|
|
|
|
return InferenceEngine::StatusCode::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
InferenceEngine::StatusCode getFactoryFor(InferenceEngine::ILayerImplFactory*& factory,
|
|
|
|
const InferenceEngine::CNNLayer* cnnLayer,
|
|
|
|
InferenceEngine::ResponseDesc* resp) noexcept;
|
|
|
|
};
|
|
|
|
|
2020-03-03 16:01:44 +08:00
|
|
|
#endif // HAVE_DNN_IE_NN_BUILDER_2019
|
|
|
|
|
2019-12-02 21:16:06 +08:00
|
|
|
|
2019-03-29 21:42:58 +08:00
|
|
|
CV__DNN_EXPERIMENTAL_NS_BEGIN
|
|
|
|
|
|
|
|
bool isMyriadX();
|
|
|
|
|
|
|
|
CV__DNN_EXPERIMENTAL_NS_END
|
|
|
|
|
2020-03-13 23:33:27 +08:00
|
|
|
InferenceEngine::Core& getCore(const std::string& id);
|
2019-12-02 21:16:06 +08:00
|
|
|
|
|
|
|
template<typename T = size_t>
|
|
|
|
static inline std::vector<T> getShape(const Mat& mat)
|
|
|
|
{
|
|
|
|
std::vector<T> result(mat.dims);
|
|
|
|
for (int i = 0; i < mat.dims; i++)
|
|
|
|
result[i] = (T)mat.size[i];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-06 16:57:35 +08:00
|
|
|
#endif // HAVE_INF_ENGINE
|
|
|
|
|
|
|
|
bool haveInfEngine();
|
|
|
|
|
2019-04-20 02:01:19 +08:00
|
|
|
void forwardInfEngine(const std::vector<Ptr<BackendWrapper> >& outBlobsWrappers,
|
|
|
|
Ptr<BackendNode>& node, bool isAsync);
|
2018-02-06 16:57:35 +08:00
|
|
|
|
|
|
|
}} // namespace dnn, namespace cv
|
|
|
|
|
|
|
|
#endif // __OPENCV_DNN_OP_INF_ENGINE_HPP__
|