Skip to content

Commit d17ab27

Browse files
authored
Merge pull request opencv#17668 from OrestChura:oc/giebackend_migration_to_core
GAPI: Migration to IE Core API * Migration to IE Core API - both versions are maintained - checked building with all the OpenVINO versions (2019.R1, R2, R3, 2020.4 (newest)) * commit to awake builders * Addressing comments - migrated to Core API in 'gapi_ie_infer_test.cpp' - made Core a singleton object - dropped redundant steps * Addressing comments - modified Mutex locking * Update * Addressing comments - remove getInitMutex() - reduce amount of #ifdef by abstracting into functions * return to single IE::Core * Divide functions readNet and loadNet to avoid warnings on GCC * Fix deprecated code warnings * Fix deprecated code warnings on CMake level * Functions wrapped - All the functions depended on IE version wrapped into a cv::gapi::ie::wrap namesapace - All this contained to a new "giebackend/gieapi.hpp" header - The header shared with G-API infer tests to avoid code duplications * Addressing comments - Renamed `gieapi.hpp` -> `giewrapper.hpp`, `cv::gapi::ie::wrap` -> `cv::gimpl::ie::wrap` - Created new `giewrapper.cpp` source file to avoid potential "multiple definition" problems - removed unnecessary step SetLayout() in tests * Enabling two NN infer teest * Two-NN infer test change for CI - deleted additional network - inference of two identical NN used instead * Fix CI fileNotFound * Disable MYRIAD test not to fail Custom CI runs
1 parent a216b8b commit d17ab27

File tree

6 files changed

+301
-148
lines changed

6 files changed

+301
-148
lines changed

modules/gapi/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ if(MSVC)
3030
# Disable obsollete warning C4503 popping up on MSVC <<2017
3131
# https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4503?view=vs-2019
3232
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4503)
33+
if (OPENCV_GAPI_INF_ENGINE AND NOT INF_ENGINE_RELEASE VERSION_GREATER "2021000000")
34+
# Disable IE deprecated code warning C4996 for releases < 2021.1
35+
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4996)
36+
endif()
3337
endif()
3438

3539
file(GLOB gapi_ext_hdrs
@@ -116,6 +120,7 @@ set(gapi_srcs
116120
# IE Backend. FIXME: should be included by CMake
117121
# if and only if IE support is enabled
118122
src/backends/ie/giebackend.cpp
123+
src/backends/ie/giebackend/giewrapper.cpp
119124

120125
# Render Backend.
121126
src/backends/render/grenderocvbackend.cpp

modules/gapi/src/backends/ie/giebackend.cpp

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018 Intel Corporation
5+
// Copyright (C) 2018-2020 Intel Corporation
66

77
#include "precomp.hpp"
88

@@ -41,6 +41,7 @@
4141
#include "compiler/gmodel.hpp"
4242

4343
#include "backends/ie/util.hpp"
44+
#include "backends/ie/giebackend/giewrapper.hpp"
4445

4546
#include "api/gbackend_priv.hpp" // FIXME: Make it part of Backend SDK!
4647

@@ -176,14 +177,9 @@ struct IEUnit {
176177

177178
explicit IEUnit(const cv::gapi::ie::detail::ParamDesc &pp)
178179
: params(pp) {
179-
180-
IE::CNNNetReader reader;
181-
reader.ReadNetwork(params.model_path);
182-
reader.ReadWeights(params.weights_path);
183-
net = reader.getNetwork();
184-
inputs = net.getInputsInfo();
180+
net = cv::gimpl::ie::wrap::readNetwork(params);
181+
inputs = net.getInputsInfo();
185182
outputs = net.getOutputsInfo();
186-
187183
// The practice shows that not all inputs and not all outputs
188184
// are mandatory to specify in IE model.
189185
// So what we're concerned here about is:
@@ -208,55 +204,9 @@ struct IEUnit {
208204
}
209205

210206
// This method is [supposed to be] called at Island compilation stage
211-
// TODO: Move to a new OpenVINO Core API!
212207
cv::gimpl::ie::IECompiled compile() const {
213-
auto this_plugin = IE::PluginDispatcher().getPluginByDevice(params.device_id);
214-
215-
#if INF_ENGINE_RELEASE < 2020000000 // <= 2019.R3
216-
// Load extensions (taken from DNN module)
217-
if (params.device_id == "CPU" || params.device_id == "FPGA")
218-
{
219-
const std::string suffixes[] = { "_avx2", "_sse4", ""};
220-
const bool haveFeature[] = {
221-
cv::checkHardwareSupport(CPU_AVX2),
222-
cv::checkHardwareSupport(CPU_SSE4_2),
223-
true
224-
};
225-
std::vector<std::string> candidates;
226-
for (auto &&it : ade::util::zip(ade::util::toRange(suffixes),
227-
ade::util::toRange(haveFeature)))
228-
{
229-
std::string suffix;
230-
bool available = false;
231-
std::tie(suffix, available) = it;
232-
if (!available) continue;
233-
#ifdef _WIN32
234-
candidates.push_back("cpu_extension" + suffix + ".dll");
235-
#elif defined(__APPLE__)
236-
candidates.push_back("libcpu_extension" + suffix + ".so"); // built as loadable module
237-
candidates.push_back("libcpu_extension" + suffix + ".dylib"); // built as shared library
238-
#else
239-
candidates.push_back("libcpu_extension" + suffix + ".so");
240-
#endif // _WIN32
241-
}
242-
for (auto &&extlib : candidates)
243-
{
244-
try
245-
{
246-
this_plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib));
247-
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
248-
break;
249-
}
250-
catch(...)
251-
{
252-
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
253-
}
254-
}
255-
}
256-
#endif
257-
258-
auto this_network = this_plugin.LoadNetwork(net, {}); // FIXME: 2nd parameter to be
259-
// configurable via the API
208+
auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
209+
auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
260210
auto this_request = this_network.CreateInferRequest();
261211

262212
// Bind const data to infer request
@@ -267,8 +217,7 @@ struct IEUnit {
267217
// Still, constant data is to set only once.
268218
this_request.SetBlob(p.first, wrapIE(p.second.first, p.second.second));
269219
}
270-
271-
return {this_plugin, this_network, this_request};
220+
return {plugin, this_network, this_request};
272221
}
273222
};
274223

@@ -796,18 +745,18 @@ cv::gapi::GBackend cv::gapi::ie::backend() {
796745
return this_backend;
797746
}
798747

799-
cv::Mat cv::gapi::ie::util::to_ocv(InferenceEngine::Blob::Ptr blob) {
748+
cv::Mat cv::gapi::ie::util::to_ocv(IE::Blob::Ptr blob) {
800749
const auto& tdesc = blob->getTensorDesc();
801750
return cv::Mat(toCV(tdesc.getDims()),
802751
toCV(tdesc.getPrecision()),
803752
blob->buffer().as<uint8_t*>());
804753
}
805754

806-
std::vector<int> cv::gapi::ie::util::to_ocv(const InferenceEngine::SizeVector &dims) {
755+
std::vector<int> cv::gapi::ie::util::to_ocv(const IE::SizeVector &dims) {
807756
return toCV(dims);
808757
}
809758

810-
InferenceEngine::Blob::Ptr cv::gapi::ie::util::to_ie(cv::Mat &blob) {
759+
IE::Blob::Ptr cv::gapi::ie::util::to_ie(cv::Mat &blob) {
811760
return wrapIE(blob, cv::gapi::ie::TraitAs::IMAGE);
812761
}
813762

modules/gapi/src/backends/ie/giebackend.hpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
//
5-
// Copyright (C) 2018 Intel Corporation
5+
// Copyright (C) 2018-2020 Intel Corporation
66

77
#ifndef OPENCV_GAPI_GIEBACKEND_HPP
88
#define OPENCV_GAPI_GIEBACKEND_HPP
@@ -14,28 +14,8 @@
1414

1515
#include <ade/util/algorithm.hpp> // type_list_index
1616

17-
////////////////////////////////////////////////////////////////////////////////
18-
// FIXME: Suppress deprecation warnings for OpenVINO 2019R2+
19-
// BEGIN {{{
20-
#if defined(__GNUC__)
21-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
22-
#endif
23-
#ifdef _MSC_VER
24-
#pragma warning(disable: 4996) // was declared deprecated
25-
#endif
26-
27-
#if defined(__GNUC__)
28-
#pragma GCC visibility push(default)
29-
#endif
30-
3117
#include <inference_engine.hpp>
3218

33-
#if defined(__GNUC__)
34-
#pragma GCC visibility pop
35-
#endif
36-
// END }}}
37-
////////////////////////////////////////////////////////////////////////////////
38-
3919
#include <opencv2/gapi/garg.hpp>
4020
#include <opencv2/gapi/gproto.hpp>
4121

@@ -48,7 +28,11 @@ namespace gimpl {
4828
namespace ie {
4929

5030
struct IECompiled {
31+
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
5132
InferenceEngine::InferencePlugin this_plugin;
33+
#else
34+
InferenceEngine::Core this_core;
35+
#endif
5236
InferenceEngine::ExecutableNetwork this_network;
5337
InferenceEngine::InferRequest this_request;
5438
};
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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) 2020 Intel Corporation
6+
7+
#ifdef HAVE_INF_ENGINE
8+
9+
#include <vector>
10+
#include <string>
11+
#include <tuple>
12+
13+
#include "backends/ie/giebackend/giewrapper.hpp"
14+
15+
#include <ade/util/range.hpp>
16+
#include <ade/util/zip_range.hpp>
17+
18+
#include <opencv2/core/utility.hpp>
19+
#include <opencv2/core/utils/logger.hpp>
20+
21+
namespace IE = InferenceEngine;
22+
namespace giewrap = cv::gimpl::ie::wrap;
23+
using GIEParam = cv::gapi::ie::detail::ParamDesc;
24+
25+
#if INF_ENGINE_RELEASE < 2020000000 // < 2020.1
26+
// Load extensions (taken from DNN module)
27+
std::vector<std::string> giewrap::getExtensions(const GIEParam& params) {
28+
std::vector<std::string> candidates;
29+
if (params.device_id == "CPU" || params.device_id == "FPGA")
30+
{
31+
const std::string suffixes[] = { "_avx2", "_sse4", ""};
32+
const bool haveFeature[] = {
33+
cv::checkHardwareSupport(CPU_AVX2),
34+
cv::checkHardwareSupport(CPU_SSE4_2),
35+
true
36+
};
37+
for (auto &&it : ade::util::zip(ade::util::toRange(suffixes),
38+
ade::util::toRange(haveFeature)))
39+
{
40+
std::string suffix;
41+
bool available = false;
42+
std::tie(suffix, available) = it;
43+
if (!available) continue;
44+
#ifdef _WIN32
45+
candidates.push_back("cpu_extension" + suffix + ".dll");
46+
#elif defined(__APPLE__)
47+
candidates.push_back("libcpu_extension" + suffix + ".so"); // built as loadable module
48+
candidates.push_back("libcpu_extension" + suffix + ".dylib"); // built as shared library
49+
#else
50+
candidates.push_back("libcpu_extension" + suffix + ".so");
51+
#endif // _WIN32
52+
}
53+
}
54+
return candidates;
55+
}
56+
57+
IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
58+
IE::CNNNetReader reader;
59+
reader.ReadNetwork(params.model_path);
60+
reader.ReadWeights(params.weights_path);
61+
return reader.getNetwork();
62+
}
63+
#else // >= 2020.1
64+
std::vector<std::string> giewrap::getExtensions(const GIEParam&) {
65+
return std::vector<std::string>();
66+
}
67+
68+
IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
69+
auto core = giewrap::getCore();
70+
return core.ReadNetwork(params.model_path, params.weights_path);
71+
}
72+
#endif // INF_ENGINE_RELEASE < 2020000000
73+
74+
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
75+
IE::InferencePlugin giewrap::getPlugin(const GIEParam& params) {
76+
auto plugin = IE::PluginDispatcher().getPluginByDevice(params.device_id);
77+
if (params.device_id == "CPU" || params.device_id == "FPGA")
78+
{
79+
for (auto &&extlib : giewrap::getExtensions(params))
80+
{
81+
try
82+
{
83+
plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib));
84+
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
85+
break;
86+
}
87+
catch(...)
88+
{
89+
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
90+
}
91+
}
92+
}
93+
return plugin;
94+
}
95+
#else // >= 2019.R2
96+
IE::Core giewrap::getCore() {
97+
static IE::Core core;
98+
return core;
99+
}
100+
101+
IE::Core giewrap::getPlugin(const GIEParam& params) {
102+
auto plugin = giewrap::getCore();
103+
if (params.device_id == "CPU" || params.device_id == "FPGA")
104+
{
105+
for (auto &&extlib : giewrap::getExtensions(params))
106+
{
107+
try
108+
{
109+
plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib), params.device_id);
110+
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
111+
break;
112+
}
113+
catch(...)
114+
{
115+
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
116+
}
117+
}
118+
}
119+
return plugin;
120+
}
121+
#endif // INF_ENGINE_RELEASE < 2019020000
122+
123+
#endif //HAVE_INF_ENGINE
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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) 2020 Intel Corporation
6+
7+
#ifndef OPENCV_GAPI_IEWRAPPER_HPP
8+
#define OPENCV_GAPI_IEWRAPPER_HPP
9+
10+
#ifdef HAVE_INF_ENGINE
11+
12+
#include <inference_engine.hpp>
13+
14+
#include <vector>
15+
#include <string>
16+
17+
#include "opencv2/gapi/infer/ie.hpp"
18+
19+
namespace IE = InferenceEngine;
20+
using GIEParam = cv::gapi::ie::detail::ParamDesc;
21+
22+
namespace cv {
23+
namespace gimpl {
24+
namespace ie {
25+
namespace wrap {
26+
// NB: These functions are EXPORTed to make them accessible by the
27+
// test suite only.
28+
GAPI_EXPORTS std::vector<std::string> getExtensions(const GIEParam& params);
29+
GAPI_EXPORTS IE::CNNNetwork readNetwork(const GIEParam& params);
30+
31+
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
32+
GAPI_EXPORTS IE::InferencePlugin getPlugin(const GIEParam& params);
33+
GAPI_EXPORTS inline IE::ExecutableNetwork loadNetwork( IE::InferencePlugin& plugin,
34+
const IE::CNNNetwork& net,
35+
const GIEParam&) {
36+
return plugin.LoadNetwork(net, {}); // FIXME: 2nd parameter to be
37+
// configurable via the API
38+
}
39+
#else // >= 2019.R2
40+
GAPI_EXPORTS IE::Core getCore();
41+
GAPI_EXPORTS IE::Core getPlugin(const GIEParam& params);
42+
GAPI_EXPORTS inline IE::ExecutableNetwork loadNetwork( IE::Core& core,
43+
const IE::CNNNetwork& net,
44+
const GIEParam& params) {
45+
return core.LoadNetwork(net, params.device_id);
46+
}
47+
#endif // INF_ENGINE_RELEASE < 2019020000
48+
}}}}
49+
50+
#endif //HAVE_INF_ENGINE
51+
#endif // OPENCV_GAPI_IEWRAPPER_HPP

0 commit comments

Comments
 (0)