Merge pull request #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
This commit is contained in:
Orest Chura
2020-07-16 21:33:35 +03:00
committed by GitHub
parent a216b8bf87
commit d17ab271e8
6 changed files with 301 additions and 148 deletions
+10 -61
View File
@@ -2,7 +2,7 @@
// 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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -41,6 +41,7 @@
#include "compiler/gmodel.hpp"
#include "backends/ie/util.hpp"
#include "backends/ie/giebackend/giewrapper.hpp"
#include "api/gbackend_priv.hpp" // FIXME: Make it part of Backend SDK!
@@ -176,14 +177,9 @@ struct IEUnit {
explicit IEUnit(const cv::gapi::ie::detail::ParamDesc &pp)
: params(pp) {
IE::CNNNetReader reader;
reader.ReadNetwork(params.model_path);
reader.ReadWeights(params.weights_path);
net = reader.getNetwork();
inputs = net.getInputsInfo();
net = cv::gimpl::ie::wrap::readNetwork(params);
inputs = net.getInputsInfo();
outputs = net.getOutputsInfo();
// The practice shows that not all inputs and not all outputs
// are mandatory to specify in IE model.
// So what we're concerned here about is:
@@ -208,55 +204,9 @@ struct IEUnit {
}
// This method is [supposed to be] called at Island compilation stage
// TODO: Move to a new OpenVINO Core API!
cv::gimpl::ie::IECompiled compile() const {
auto this_plugin = IE::PluginDispatcher().getPluginByDevice(params.device_id);
#if INF_ENGINE_RELEASE < 2020000000 // <= 2019.R3
// Load extensions (taken from DNN module)
if (params.device_id == "CPU" || params.device_id == "FPGA")
{
const std::string suffixes[] = { "_avx2", "_sse4", ""};
const bool haveFeature[] = {
cv::checkHardwareSupport(CPU_AVX2),
cv::checkHardwareSupport(CPU_SSE4_2),
true
};
std::vector<std::string> candidates;
for (auto &&it : ade::util::zip(ade::util::toRange(suffixes),
ade::util::toRange(haveFeature)))
{
std::string suffix;
bool available = false;
std::tie(suffix, available) = it;
if (!available) continue;
#ifdef _WIN32
candidates.push_back("cpu_extension" + suffix + ".dll");
#elif defined(__APPLE__)
candidates.push_back("libcpu_extension" + suffix + ".so"); // built as loadable module
candidates.push_back("libcpu_extension" + suffix + ".dylib"); // built as shared library
#else
candidates.push_back("libcpu_extension" + suffix + ".so");
#endif // _WIN32
}
for (auto &&extlib : candidates)
{
try
{
this_plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib));
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
break;
}
catch(...)
{
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
}
}
}
#endif
auto this_network = this_plugin.LoadNetwork(net, {}); // FIXME: 2nd parameter to be
// configurable via the API
auto plugin = cv::gimpl::ie::wrap::getPlugin(params);
auto this_network = cv::gimpl::ie::wrap::loadNetwork(plugin, net, params);
auto this_request = this_network.CreateInferRequest();
// Bind const data to infer request
@@ -267,8 +217,7 @@ struct IEUnit {
// Still, constant data is to set only once.
this_request.SetBlob(p.first, wrapIE(p.second.first, p.second.second));
}
return {this_plugin, this_network, this_request};
return {plugin, this_network, this_request};
}
};
@@ -796,18 +745,18 @@ cv::gapi::GBackend cv::gapi::ie::backend() {
return this_backend;
}
cv::Mat cv::gapi::ie::util::to_ocv(InferenceEngine::Blob::Ptr blob) {
cv::Mat cv::gapi::ie::util::to_ocv(IE::Blob::Ptr blob) {
const auto& tdesc = blob->getTensorDesc();
return cv::Mat(toCV(tdesc.getDims()),
toCV(tdesc.getPrecision()),
blob->buffer().as<uint8_t*>());
}
std::vector<int> cv::gapi::ie::util::to_ocv(const InferenceEngine::SizeVector &dims) {
std::vector<int> cv::gapi::ie::util::to_ocv(const IE::SizeVector &dims) {
return toCV(dims);
}
InferenceEngine::Blob::Ptr cv::gapi::ie::util::to_ie(cv::Mat &blob) {
IE::Blob::Ptr cv::gapi::ie::util::to_ie(cv::Mat &blob) {
return wrapIE(blob, cv::gapi::ie::TraitAs::IMAGE);
}
+5 -21
View File
@@ -2,7 +2,7 @@
// 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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#ifndef OPENCV_GAPI_GIEBACKEND_HPP
#define OPENCV_GAPI_GIEBACKEND_HPP
@@ -14,28 +14,8 @@
#include <ade/util/algorithm.hpp> // type_list_index
////////////////////////////////////////////////////////////////////////////////
// FIXME: Suppress deprecation warnings for OpenVINO 2019R2+
// BEGIN {{{
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#ifdef _MSC_VER
#pragma warning(disable: 4996) // was declared deprecated
#endif
#if defined(__GNUC__)
#pragma GCC visibility push(default)
#endif
#include <inference_engine.hpp>
#if defined(__GNUC__)
#pragma GCC visibility pop
#endif
// END }}}
////////////////////////////////////////////////////////////////////////////////
#include <opencv2/gapi/garg.hpp>
#include <opencv2/gapi/gproto.hpp>
@@ -48,7 +28,11 @@ namespace gimpl {
namespace ie {
struct IECompiled {
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
InferenceEngine::InferencePlugin this_plugin;
#else
InferenceEngine::Core this_core;
#endif
InferenceEngine::ExecutableNetwork this_network;
InferenceEngine::InferRequest this_request;
};
@@ -0,0 +1,123 @@
// 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) 2020 Intel Corporation
#ifdef HAVE_INF_ENGINE
#include <vector>
#include <string>
#include <tuple>
#include "backends/ie/giebackend/giewrapper.hpp"
#include <ade/util/range.hpp>
#include <ade/util/zip_range.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/core/utils/logger.hpp>
namespace IE = InferenceEngine;
namespace giewrap = cv::gimpl::ie::wrap;
using GIEParam = cv::gapi::ie::detail::ParamDesc;
#if INF_ENGINE_RELEASE < 2020000000 // < 2020.1
// Load extensions (taken from DNN module)
std::vector<std::string> giewrap::getExtensions(const GIEParam& params) {
std::vector<std::string> candidates;
if (params.device_id == "CPU" || params.device_id == "FPGA")
{
const std::string suffixes[] = { "_avx2", "_sse4", ""};
const bool haveFeature[] = {
cv::checkHardwareSupport(CPU_AVX2),
cv::checkHardwareSupport(CPU_SSE4_2),
true
};
for (auto &&it : ade::util::zip(ade::util::toRange(suffixes),
ade::util::toRange(haveFeature)))
{
std::string suffix;
bool available = false;
std::tie(suffix, available) = it;
if (!available) continue;
#ifdef _WIN32
candidates.push_back("cpu_extension" + suffix + ".dll");
#elif defined(__APPLE__)
candidates.push_back("libcpu_extension" + suffix + ".so"); // built as loadable module
candidates.push_back("libcpu_extension" + suffix + ".dylib"); // built as shared library
#else
candidates.push_back("libcpu_extension" + suffix + ".so");
#endif // _WIN32
}
}
return candidates;
}
IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
IE::CNNNetReader reader;
reader.ReadNetwork(params.model_path);
reader.ReadWeights(params.weights_path);
return reader.getNetwork();
}
#else // >= 2020.1
std::vector<std::string> giewrap::getExtensions(const GIEParam&) {
return std::vector<std::string>();
}
IE::CNNNetwork giewrap::readNetwork(const GIEParam& params) {
auto core = giewrap::getCore();
return core.ReadNetwork(params.model_path, params.weights_path);
}
#endif // INF_ENGINE_RELEASE < 2020000000
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
IE::InferencePlugin giewrap::getPlugin(const GIEParam& params) {
auto plugin = IE::PluginDispatcher().getPluginByDevice(params.device_id);
if (params.device_id == "CPU" || params.device_id == "FPGA")
{
for (auto &&extlib : giewrap::getExtensions(params))
{
try
{
plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib));
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
break;
}
catch(...)
{
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
}
}
}
return plugin;
}
#else // >= 2019.R2
IE::Core giewrap::getCore() {
static IE::Core core;
return core;
}
IE::Core giewrap::getPlugin(const GIEParam& params) {
auto plugin = giewrap::getCore();
if (params.device_id == "CPU" || params.device_id == "FPGA")
{
for (auto &&extlib : giewrap::getExtensions(params))
{
try
{
plugin.AddExtension(IE::make_so_pointer<IE::IExtension>(extlib), params.device_id);
CV_LOG_INFO(NULL, "DNN-IE: Loaded extension plugin: " << extlib);
break;
}
catch(...)
{
CV_LOG_INFO(NULL, "Failed to load IE extension: " << extlib);
}
}
}
return plugin;
}
#endif // INF_ENGINE_RELEASE < 2019020000
#endif //HAVE_INF_ENGINE
@@ -0,0 +1,51 @@
// 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) 2020 Intel Corporation
#ifndef OPENCV_GAPI_IEWRAPPER_HPP
#define OPENCV_GAPI_IEWRAPPER_HPP
#ifdef HAVE_INF_ENGINE
#include <inference_engine.hpp>
#include <vector>
#include <string>
#include "opencv2/gapi/infer/ie.hpp"
namespace IE = InferenceEngine;
using GIEParam = cv::gapi::ie::detail::ParamDesc;
namespace cv {
namespace gimpl {
namespace ie {
namespace wrap {
// NB: These functions are EXPORTed to make them accessible by the
// test suite only.
GAPI_EXPORTS std::vector<std::string> getExtensions(const GIEParam& params);
GAPI_EXPORTS IE::CNNNetwork readNetwork(const GIEParam& params);
#if INF_ENGINE_RELEASE < 2019020000 // < 2019.R2
GAPI_EXPORTS IE::InferencePlugin getPlugin(const GIEParam& params);
GAPI_EXPORTS inline IE::ExecutableNetwork loadNetwork( IE::InferencePlugin& plugin,
const IE::CNNNetwork& net,
const GIEParam&) {
return plugin.LoadNetwork(net, {}); // FIXME: 2nd parameter to be
// configurable via the API
}
#else // >= 2019.R2
GAPI_EXPORTS IE::Core getCore();
GAPI_EXPORTS IE::Core getPlugin(const GIEParam& params);
GAPI_EXPORTS inline IE::ExecutableNetwork loadNetwork( IE::Core& core,
const IE::CNNNetwork& net,
const GIEParam& params) {
return core.LoadNetwork(net, params.device_id);
}
#endif // INF_ENGINE_RELEASE < 2019020000
}}}}
#endif //HAVE_INF_ENGINE
#endif // OPENCV_GAPI_IEWRAPPER_HPP