Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/op_inf_engine.hpp
16337 views
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.
4
//
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#ifndef __OPENCV_DNN_OP_INF_ENGINE_HPP__
9
#define __OPENCV_DNN_OP_INF_ENGINE_HPP__
10
11
#include "opencv2/core/cvdef.h"
12
#include "opencv2/core/cvstd.hpp"
13
#include "opencv2/dnn.hpp"
14
15
#ifdef HAVE_INF_ENGINE
16
#if defined(__GNUC__) && __GNUC__ >= 5
17
//#pragma GCC diagnostic push
18
#pragma GCC diagnostic ignored "-Wsuggest-override"
19
#endif
20
#include <inference_engine.hpp>
21
#if defined(__GNUC__) && __GNUC__ >= 5
22
//#pragma GCC diagnostic pop
23
#endif
24
25
#define INF_ENGINE_RELEASE_2018R1 2018010000
26
#define INF_ENGINE_RELEASE_2018R2 2018020000
27
#define INF_ENGINE_RELEASE_2018R3 2018030000
28
29
#ifndef INF_ENGINE_RELEASE
30
#warning("IE version have not been provided via command-line. Using 2018R2 by default")
31
#define INF_ENGINE_RELEASE INF_ENGINE_RELEASE_2018R2
32
#endif
33
34
#define INF_ENGINE_VER_MAJOR_GT(ver) (((INF_ENGINE_RELEASE) / 10000) > ((ver) / 10000))
35
#define INF_ENGINE_VER_MAJOR_GE(ver) (((INF_ENGINE_RELEASE) / 10000) >= ((ver) / 10000))
36
37
#endif // HAVE_INF_ENGINE
38
39
namespace cv { namespace dnn {
40
41
#ifdef HAVE_INF_ENGINE
42
43
class InfEngineBackendNet : public InferenceEngine::ICNNNetwork
44
{
45
public:
46
InfEngineBackendNet();
47
48
InfEngineBackendNet(InferenceEngine::CNNNetwork& net);
49
50
virtual void Release() CV_NOEXCEPT CV_OVERRIDE;
51
52
void setPrecision(InferenceEngine::Precision p) CV_NOEXCEPT;
53
54
virtual InferenceEngine::Precision getPrecision() CV_NOEXCEPT;
55
56
virtual InferenceEngine::Precision getPrecision() const CV_NOEXCEPT;
57
58
virtual void getOutputsInfo(InferenceEngine::OutputsDataMap &out) CV_NOEXCEPT /*CV_OVERRIDE*/;
59
60
virtual void getOutputsInfo(InferenceEngine::OutputsDataMap &out) const CV_NOEXCEPT /*CV_OVERRIDE*/;
61
62
virtual void getInputsInfo(InferenceEngine::InputsDataMap &inputs) CV_NOEXCEPT /*CV_OVERRIDE*/;
63
64
virtual void getInputsInfo(InferenceEngine::InputsDataMap &inputs) const CV_NOEXCEPT /*CV_OVERRIDE*/;
65
66
virtual InferenceEngine::InputInfo::Ptr getInput(const std::string &inputName) CV_NOEXCEPT;
67
68
virtual InferenceEngine::InputInfo::Ptr getInput(const std::string &inputName) const CV_NOEXCEPT;
69
70
virtual void getName(char *pName, size_t len) CV_NOEXCEPT;
71
72
virtual void getName(char *pName, size_t len) const CV_NOEXCEPT;
73
74
virtual const std::string& getName() const CV_NOEXCEPT;
75
76
virtual size_t layerCount() CV_NOEXCEPT;
77
78
virtual size_t layerCount() const CV_NOEXCEPT;
79
80
virtual InferenceEngine::DataPtr& getData(const char *dname) CV_NOEXCEPT CV_OVERRIDE;
81
82
virtual void addLayer(const InferenceEngine::CNNLayerPtr &layer) CV_NOEXCEPT CV_OVERRIDE;
83
84
virtual InferenceEngine::StatusCode addOutput(const std::string &layerName,
85
size_t outputIndex = 0,
86
InferenceEngine::ResponseDesc *resp = nullptr) CV_NOEXCEPT;
87
88
virtual InferenceEngine::StatusCode getLayerByName(const char *layerName,
89
InferenceEngine::CNNLayerPtr &out,
90
InferenceEngine::ResponseDesc *resp) CV_NOEXCEPT;
91
92
virtual InferenceEngine::StatusCode getLayerByName(const char *layerName,
93
InferenceEngine::CNNLayerPtr &out,
94
InferenceEngine::ResponseDesc *resp) const CV_NOEXCEPT;
95
96
virtual void setTargetDevice(InferenceEngine::TargetDevice device) CV_NOEXCEPT CV_OVERRIDE;
97
98
virtual InferenceEngine::TargetDevice getTargetDevice() CV_NOEXCEPT;
99
100
virtual InferenceEngine::TargetDevice getTargetDevice() const CV_NOEXCEPT;
101
102
virtual InferenceEngine::StatusCode setBatchSize(const size_t size) CV_NOEXCEPT CV_OVERRIDE;
103
104
virtual InferenceEngine::StatusCode setBatchSize(size_t size, InferenceEngine::ResponseDesc* responseDesc) CV_NOEXCEPT;
105
106
virtual size_t getBatchSize() const CV_NOEXCEPT CV_OVERRIDE;
107
108
#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R2)
109
virtual InferenceEngine::StatusCode AddExtension(const InferenceEngine::IShapeInferExtensionPtr& extension, InferenceEngine::ResponseDesc* resp) CV_NOEXCEPT;
110
virtual InferenceEngine::StatusCode reshape(const InputShapes& inputShapes, InferenceEngine::ResponseDesc* resp) CV_NOEXCEPT;
111
#endif
112
113
void init(int targetId);
114
115
void addBlobs(const std::vector<Ptr<BackendWrapper> >& wrappers);
116
117
void forward();
118
119
bool isInitialized();
120
121
private:
122
std::vector<InferenceEngine::CNNLayerPtr> layers;
123
InferenceEngine::InputsDataMap inputs;
124
InferenceEngine::OutputsDataMap outputs;
125
InferenceEngine::BlobMap inpBlobs;
126
InferenceEngine::BlobMap outBlobs;
127
InferenceEngine::BlobMap allBlobs;
128
InferenceEngine::TargetDevice targetDevice;
129
InferenceEngine::Precision precision;
130
InferenceEngine::InferenceEnginePluginPtr enginePtr;
131
InferenceEngine::InferencePlugin plugin;
132
InferenceEngine::ExecutableNetwork netExec;
133
InferenceEngine::InferRequest infRequest;
134
// In case of models from Model Optimizer we need to manage their lifetime.
135
InferenceEngine::CNNNetwork netOwner;
136
137
std::string name;
138
139
void initPlugin(InferenceEngine::ICNNNetwork& net);
140
};
141
142
class InfEngineBackendNode : public BackendNode
143
{
144
public:
145
InfEngineBackendNode(const InferenceEngine::CNNLayerPtr& layer);
146
147
void connect(std::vector<Ptr<BackendWrapper> >& inputs,
148
std::vector<Ptr<BackendWrapper> >& outputs);
149
150
InferenceEngine::CNNLayerPtr layer;
151
// Inference Engine network object that allows to obtain the outputs of this layer.
152
Ptr<InfEngineBackendNet> net;
153
};
154
155
class InfEngineBackendWrapper : public BackendWrapper
156
{
157
public:
158
InfEngineBackendWrapper(int targetId, const Mat& m);
159
160
InfEngineBackendWrapper(Ptr<BackendWrapper> wrapper);
161
162
~InfEngineBackendWrapper();
163
164
static Ptr<BackendWrapper> create(Ptr<BackendWrapper> wrapper);
165
166
virtual void copyToHost() CV_OVERRIDE;
167
168
virtual void setHostDirty() CV_OVERRIDE;
169
170
InferenceEngine::DataPtr dataPtr;
171
InferenceEngine::Blob::Ptr blob;
172
};
173
174
InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, InferenceEngine::Layout layout = InferenceEngine::Layout::ANY);
175
176
InferenceEngine::Blob::Ptr wrapToInfEngineBlob(const Mat& m, const std::vector<size_t>& shape, InferenceEngine::Layout layout);
177
178
InferenceEngine::DataPtr infEngineDataNode(const Ptr<BackendWrapper>& ptr);
179
180
Mat infEngineBlobToMat(const InferenceEngine::Blob::Ptr& blob);
181
182
// Convert Inference Engine blob with FP32 precision to FP16 precision.
183
// Allocates memory for a new blob.
184
InferenceEngine::TBlob<int16_t>::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob);
185
186
// This is a fake class to run networks from Model Optimizer. Objects of that
187
// class simulate responses of layers are imported by OpenCV and supported by
188
// Inference Engine. The main difference is that they do not perform forward pass.
189
class InfEngineBackendLayer : public Layer
190
{
191
public:
192
InfEngineBackendLayer(const InferenceEngine::DataPtr& output);
193
194
virtual bool getMemoryShapes(const std::vector<MatShape> &inputs,
195
const int requiredOutputs,
196
std::vector<MatShape> &outputs,
197
std::vector<MatShape> &internals) const CV_OVERRIDE;
198
199
virtual void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs,
200
OutputArrayOfArrays internals) CV_OVERRIDE;
201
202
virtual bool supportBackend(int backendId) CV_OVERRIDE;
203
204
private:
205
InferenceEngine::DataPtr output;
206
};
207
208
#endif // HAVE_INF_ENGINE
209
210
bool haveInfEngine();
211
212
void forwardInfEngine(Ptr<BackendNode>& node);
213
214
}} // namespace dnn, namespace cv
215
216
#endif // __OPENCV_DNN_OP_INF_ENGINE_HPP__
217
218