1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

Merge pull request #24024 from TolyaTalamanov:at/add-onnx-openvino-execution-provider

G-API: Support OpenVINO Execution Provider for ONNXRT Backend #24024

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [ ] I agree to contribute to the project under Apache 2 License.
- [ ] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Anatoliy Talamanov
2023-07-21 10:18:06 +01:00
committed by GitHub
parent e41ba90f17
commit 5261961a6e
5 changed files with 207 additions and 2 deletions
@@ -21,6 +21,18 @@ cv::gapi::onnx::PyParams& cv::gapi::onnx::PyParams::cfgNormalize(const std::stri
return *this;
}
cv::gapi::onnx::PyParams&
cv::gapi::onnx::PyParams::cfgExecutionProvider(cv::gapi::onnx::ep::OpenVINO ov_ep) {
m_priv->cfgExecutionProvider(std::move(ov_ep));
return *this;
}
cv::gapi::onnx::PyParams&
cv::gapi::onnx::PyParams::cfgDisableMemPattern() {
m_priv->cfgDisableMemPattern();
return *this;
}
cv::gapi::GBackend cv::gapi::onnx::PyParams::backend() const {
return m_priv->backend();
}
@@ -143,6 +143,41 @@ public:
void run();
};
static void appendExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::EP &execution_provider) {
namespace ep = cv::gapi::onnx::ep;
switch (execution_provider.index()) {
case ep::EP::index_of<ep::OpenVINO>(): {
GAPI_LOG_INFO(NULL, "OpenVINO Execution Provider is selected.");
const auto &ovep = cv::util::get<ep::OpenVINO>(execution_provider);
OrtOpenVINOProviderOptions options;
options.device_id = ovep.device_id.c_str();
options.cache_dir = ovep.cache_dir.c_str();
options.enable_opencl_throttling = ovep.enable_opencl_throttling;
options.enable_dynamic_shapes = ovep.enable_dynamic_shapes;
// NB: If are not specified, will be taken from onnxruntime build.
if (ovep.device_type) {
options.device_type = ovep.device_type->c_str();
}
if (ovep.num_of_threads) {
options.num_of_threads = *ovep.num_of_threads;
}
try {
session_options->AppendExecutionProvider_OpenVINO(options);
} catch (const std::exception &e) {
std::stringstream ss;
ss << "ONNX Backend: Failed to enable OpenVINO Execution Provider: "
<< e.what() << "\nMake sure that onnxruntime has"
" been compiled with OpenVINO support.";
cv::util::throw_error(std::runtime_error(ss.str()));
}
break;
}
default:
break;
}
}
} // namespace onnx
} // namespace gimpl
} // namespace cv
@@ -592,9 +627,13 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
cv::util::throw_error(std::logic_error("Please specify output layer names for "
+ params.model_path));
}
// Create and initialize the ONNX session
Ort::SessionOptions session_options;
cv::gimpl::onnx::appendExecutionProvider(&session_options, pp.execution_provider);
if (pp.disable_mem_pattern) {
session_options.DisableMemPattern();
}
this_env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "");
#ifndef _WIN32
this_session = Ort::Session(this_env, params.model_path.data(), session_options);