-
-
Notifications
You must be signed in to change notification settings - Fork 56.6k
Expand file tree
/
Copy pathop_inf_engine.cpp
More file actions
386 lines (337 loc) · 11.7 KB
/
op_inf_engine.cpp
File metadata and controls
386 lines (337 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// 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.
//
// Copyright (C) 2018-2019, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
#include "precomp.hpp"
#include "op_inf_engine.hpp"
#include <opencv2/dnn/shape_utils.hpp>
#ifdef HAVE_INF_ENGINE
#include <openvino/core/extension.hpp>
#elif defined(ENABLE_PLUGINS)
// using plugin API
#include "backend.hpp"
#include "factory.hpp"
#endif
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>
namespace cv { namespace dnn {
#ifdef HAVE_INF_ENGINE
CV__DNN_INLINE_NS_BEGIN
cv::String getInferenceEngineBackendType()
{
return "NGRAPH";
}
cv::String setInferenceEngineBackendType(const cv::String& newBackendType)
{
if (newBackendType != "NGRAPH")
CV_Error(Error::StsNotImplemented, cv::format("DNN/IE: only NGRAPH backend is supported: %s", newBackendType.c_str()));
return newBackendType;
}
CV__DNN_INLINE_NS_END
Mat infEngineBlobToMat(const ov::Tensor& blob)
{
std::vector<size_t> dims = blob.get_shape();
std::vector<int> size(dims.begin(), dims.end());
auto precision = blob.get_element_type();
int type = -1;
switch (precision)
{
case ov::element::f32: type = CV_32F; break;
case ov::element::u8: type = CV_8U; break;
default:
CV_Error(Error::StsNotImplemented, "Unsupported blob precision");
}
return Mat(size, type, const_cast<void*>(blob.data()));
}
void infEngineBlobsToMats(const ov::TensorVector& blobs,
std::vector<Mat>& mats)
{
mats.resize(blobs.size());
for (int i = 0; i < blobs.size(); ++i)
mats[i] = infEngineBlobToMat(blobs[i]);
}
static bool init_IE_plugins()
{
// load and hold IE plugins
static ov::Core* init_core = new ov::Core(); // 'delete' is never called
(void)init_core->get_available_devices();
return true;
}
static ov::Core& retrieveIECore(const std::string& id, std::map<std::string, std::shared_ptr<ov::Core> >& cores)
{
AutoLock lock(getInitializationMutex());
std::map<std::string, std::shared_ptr<ov::Core> >::iterator i = cores.find(id);
if (i == cores.end())
{
std::shared_ptr<ov::Core> core = std::make_shared<ov::Core>();
cores[id] = core;
return *core.get();
}
return *(i->second).get();
}
static ov::Core& create_IE_Core_instance(const std::string& id)
{
static std::map<std::string, std::shared_ptr<ov::Core> > cores;
return retrieveIECore(id, cores);
}
static ov::Core& create_IE_Core_pointer(const std::string& id)
{
// load and hold IE plugins
static std::map<std::string, std::shared_ptr<ov::Core> >* cores =
new std::map<std::string, std::shared_ptr<ov::Core> >();
return retrieveIECore(id, *cores);
}
ov::Core& getCore(const std::string& id)
{
// to make happy memory leak tools use:
// - OPENCV_DNN_INFERENCE_ENGINE_HOLD_PLUGINS=0
// - OPENCV_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND=0
static bool param_DNN_INFERENCE_ENGINE_HOLD_PLUGINS = utils::getConfigurationParameterBool("OPENCV_DNN_INFERENCE_ENGINE_HOLD_PLUGINS", true);
static bool init_IE_plugins_ = param_DNN_INFERENCE_ENGINE_HOLD_PLUGINS && init_IE_plugins(); CV_UNUSED(init_IE_plugins_);
static bool param_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND =
utils::getConfigurationParameterBool("OPENCV_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND",
#ifdef _WIN32
true
#else
false
#endif
);
ov::Core& core = param_DNN_INFERENCE_ENGINE_CORE_LIFETIME_WORKAROUND
? create_IE_Core_pointer(id)
: create_IE_Core_instance(id);
return core;
}
static bool detectArmPlugin_()
{
ov::Core& ie = getCore("CPU");
const std::vector<std::string> devices = ie.get_available_devices();
for (std::vector<std::string>::const_iterator i = devices.begin(); i != devices.end(); ++i)
{
if (i->find("CPU") != std::string::npos)
{
const std::string name = ie.get_property(*i, ov::device::full_name);
CV_LOG_INFO(NULL, "CPU plugin: " << name);
return name.find("arm_compute::NEON") != std::string::npos;
}
}
return false;
}
#if !defined(OPENCV_DNN_IE_VPU_TYPE_DEFAULT)
static bool detectMyriadX_(const std::string& device)
{
AutoLock lock(getInitializationMutex());
// Lightweight detection
ov::Core& ie = getCore(device);
const std::vector<std::string> devices = ie.get_available_devices();
for (std::vector<std::string>::const_iterator i = devices.begin(); i != devices.end(); ++i)
{
if (i->find(device) != std::string::npos)
{
const std::string name = ie.get_property(*i, ov::device::full_name);
CV_LOG_INFO(NULL, "Myriad device: " << name);
return name.find("MyriadX") != std::string::npos || name.find("Myriad X") != std::string::npos || name.find("HDDL") != std::string::npos;
}
}
return false;
}
#endif // !defined(OPENCV_DNN_IE_VPU_TYPE_DEFAULT)
#endif // HAVE_INF_ENGINE
CV__DNN_INLINE_NS_BEGIN
void resetMyriadDevice()
{
#ifdef HAVE_INF_ENGINE
CV_LOG_INFO(NULL, "DNN: Unregistering both 'MYRIAD' and 'HETERO:MYRIAD,CPU' plugins");
AutoLock lock(getInitializationMutex());
ov::Core& ie = getCore("MYRIAD");
try
{
ie.unload_plugin("MYRIAD");
ie.unload_plugin("HETERO");
}
catch (...) {}
#endif // HAVE_INF_ENGINE
}
void releaseHDDLPlugin()
{
#ifdef HAVE_INF_ENGINE
CV_LOG_INFO(NULL, "DNN: Unregistering both 'HDDL' and 'HETERO:HDDL,CPU' plugins");
AutoLock lock(getInitializationMutex());
ov::Core& ie = getCore("HDDL");
try
{
ie.unload_plugin("HDDL");
ie.unload_plugin("HETERO");
}
catch (...) {}
#endif // HAVE_INF_ENGINE
}
#ifdef HAVE_INF_ENGINE
bool isMyriadX()
{
static bool myriadX = getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X;
return myriadX;
}
bool isArmComputePlugin()
{
static bool armPlugin = getInferenceEngineCPUType() == CV_DNN_INFERENCE_ENGINE_CPU_TYPE_ARM_COMPUTE;
return armPlugin;
}
static std::string getInferenceEngineVPUType_()
{
static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_DNN_IE_VPU_TYPE", "");
if (param_vpu_type == "")
{
#if defined(OPENCV_DNN_IE_VPU_TYPE_DEFAULT)
param_vpu_type = OPENCV_DNN_IE_VPU_TYPE_DEFAULT;
#else
CV_LOG_INFO(NULL, "OpenCV-DNN: running Inference Engine VPU autodetection: Myriad2/X or HDDL. In case of other accelerator types specify 'OPENCV_DNN_IE_VPU_TYPE' parameter");
try {
bool isMyriadX_ = detectMyriadX_("MYRIAD");
bool isHDDL_ = detectMyriadX_("HDDL");
if (isMyriadX_ || isHDDL_)
{
param_vpu_type = CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X;
}
else
{
param_vpu_type = CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2;
}
}
catch (...)
{
CV_LOG_WARNING(NULL, "OpenCV-DNN: Failed Inference Engine VPU autodetection. Specify 'OPENCV_DNN_IE_VPU_TYPE' parameter.");
param_vpu_type.clear();
}
#endif
}
CV_LOG_INFO(NULL, "OpenCV-DNN: Inference Engine VPU type='" << param_vpu_type << "'");
return param_vpu_type;
}
cv::String getInferenceEngineVPUType()
{
static cv::String vpu_type = getInferenceEngineVPUType_();
return vpu_type;
}
cv::String getInferenceEngineCPUType()
{
static cv::String cpu_type = detectArmPlugin_() ?
CV_DNN_INFERENCE_ENGINE_CPU_TYPE_ARM_COMPUTE :
CV_DNN_INFERENCE_ENGINE_CPU_TYPE_X86;
return cpu_type;
}
namespace openvino {
bool checkTarget(Target target)
{
// Lightweight detection
const std::vector<std::string> devices = getCore("").get_available_devices();
for (std::vector<std::string>::const_iterator i = devices.begin(); i != devices.end(); ++i)
{
if (std::string::npos != i->find("MYRIAD") && target == DNN_TARGET_MYRIAD)
return true;
if (std::string::npos != i->find("HDDL") && target == DNN_TARGET_HDDL)
return true;
else if (std::string::npos != i->find("FPGA") && target == DNN_TARGET_FPGA)
return true;
else if (std::string::npos != i->find("CPU") && target == DNN_TARGET_CPU)
return true;
else if (std::string::npos != i->find("GPU") && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
return true;
else if (std::string::npos != i->find("NPU") && target == DNN_TARGET_NPU)
return true;
}
return false;
}
} // namespace openvino
#else // HAVE_INF_ENGINE
namespace openvino {
bool checkTarget(Target target)
{
#if defined(ENABLE_PLUGINS)
try
{
auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino");
return networkBackend.checkTarget(target);
}
catch (const std::exception& e)
{
CV_LOG_INFO(NULL, "DNN/OpenVINO: checkTarget failed: " << e.what())
}
#endif
return false;
}
} // namespace openvino
cv::String getInferenceEngineBackendType()
{
#if defined(ENABLE_PLUGINS)
try
{
auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino");
CV_UNUSED(networkBackend);
return CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH;
}
catch (const std::exception& e)
{
CV_LOG_INFO(NULL, "DNN/OpenVINO: plugin is not available: " << e.what())
}
#endif
CV_Error(Error::StsNotImplemented, "This OpenCV build doesn't include InferenceEngine support");
}
cv::String setInferenceEngineBackendType(const cv::String& newBackendType)
{
#if defined(ENABLE_PLUGINS)
try
{
auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino");
CV_UNUSED(networkBackend);
CV_Assert(newBackendType == CV_DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
}
catch (const std::exception& e)
{
CV_LOG_INFO(NULL, "DNN/OpenVINO: plugin is not available: " << e.what())
}
#endif
CV_UNUSED(newBackendType);
CV_Error(Error::StsNotImplemented, "This OpenCV build doesn't include InferenceEngine support");
}
cv::String getInferenceEngineVPUType()
{
#if defined(ENABLE_PLUGINS)
try
{
auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino");
if (networkBackend.checkTarget(DNN_TARGET_MYRIAD))
return CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X; // 2021.4 supports NCS2 only
CV_Error(Error::StsError, "DNN/OpenVINO: DNN_TARGET_MYRIAD is not available");
}
catch (const std::exception& e)
{
CV_LOG_INFO(NULL, "DNN/OpenVINO: plugin is not available: " << e.what())
}
#endif
CV_Error(Error::StsNotImplemented, "This OpenCV build doesn't include InferenceEngine support");
}
cv::String getInferenceEngineCPUType()
{
#if defined(ENABLE_PLUGINS)
try
{
auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino");
CV_UNUSED(networkBackend);
#if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
return CV_DNN_INFERENCE_ENGINE_CPU_TYPE_ARM_COMPUTE;
#else
return CV_DNN_INFERENCE_ENGINE_CPU_TYPE_X86;
#endif
}
catch (const std::exception& e)
{
CV_LOG_INFO(NULL, "DNN/OpenVINO: plugin is not available: " << e.what())
}
#endif
CV_Error(Error::StsNotImplemented, "This OpenCV build doesn't include InferenceEngine support");
}
#endif // HAVE_INF_ENGINE
CV__DNN_INLINE_NS_END
}} // namespace dnn, namespace cv