From ce6684954634d8697f942364fa235786bdfb57f8 Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 10:06:51 +0100 Subject: [PATCH 1/8] Extend G-API onnx::Params to pass arbitrary session options --- modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 14 ++++++++++++++ modules/gapi/src/backends/onnx/gonnxbackend.cpp | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index f985b41d71bf..124ee1001ac4 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -351,6 +351,7 @@ struct ParamDesc { std::unordered_map > generic_mstd; std::unordered_map generic_norm; + std::unordered_map session_options_map; std::vector execution_providers; bool disable_mem_pattern; }; @@ -634,6 +635,19 @@ template class Params { return *this; } + /** @brief Configures session options for ONNX Runtime. + + This function allows setting various session options for the ONNX Runtime + session by accepting a map of key-value pairs. + + @param options A map of session options to be applied to the ONNX Runtime session. + @return the reference on modified object. + */ + Params& cfgSessionOptions(const std::unordered_map& options) { + desc.session_options_map.insert(options.begin(), options.end()); + return *this; + } + // BEGIN(G-API's network parametrization API) GBackend backend() const { return cv::gapi::onnx::backend(); } std::string tag() const { return Net::tag(); } diff --git a/modules/gapi/src/backends/onnx/gonnxbackend.cpp b/modules/gapi/src/backends/onnx/gonnxbackend.cpp index 92b908da700d..e81f265f8efb 100644 --- a/modules/gapi/src/backends/onnx/gonnxbackend.cpp +++ b/modules/gapi/src/backends/onnx/gonnxbackend.cpp @@ -702,6 +702,10 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp) cv::gimpl::onnx::addExecutionProvider(&session_options, ep); } + for (const auto &option : pp.session_options_map) { + session_options.AddConfigEntry(option.first.c_str(), option.second.c_str()); + } + if (pp.disable_mem_pattern) { session_options.DisableMemPattern(); } From 8ecb4dab049eb06d03e2a365d62914c721115914 Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:22:24 +0100 Subject: [PATCH 2/8] Addressed comments and corrected build error --- modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 11 ++++++----- modules/gapi/src/backends/onnx/gonnxbackend.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index 124ee1001ac4..bb40c4d70d1a 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -351,7 +351,7 @@ struct ParamDesc { std::unordered_map > generic_mstd; std::unordered_map generic_norm; - std::unordered_map session_options_map; + std::map session_options; std::vector execution_providers; bool disable_mem_pattern; }; @@ -640,11 +640,11 @@ template class Params { This function allows setting various session options for the ONNX Runtime session by accepting a map of key-value pairs. - @param options A map of session options to be applied to the ONNX Runtime session. + @param options A map of session option to be applied to the ONNX Runtime session. @return the reference on modified object. */ - Params& cfgSessionOptions(const std::unordered_map& options) { - desc.session_options_map.insert(options.begin(), options.end()); + Params& cfgSessionOptions(const std::map& options) { + desc.session_options.insert(options.begin(), options.end()); return *this; } @@ -675,7 +675,8 @@ class Params { @param model_path path to model file (.onnx file). */ Params(const std::string& tag, const std::string& model_path) - : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, false }, m_tag(tag) {} + : desc{ + model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} /** @see onnx::Params::cfgMeanStdDev. */ void cfgMeanStdDev(const std::string &layer, diff --git a/modules/gapi/src/backends/onnx/gonnxbackend.cpp b/modules/gapi/src/backends/onnx/gonnxbackend.cpp index e81f265f8efb..e465a1ccc7ba 100644 --- a/modules/gapi/src/backends/onnx/gonnxbackend.cpp +++ b/modules/gapi/src/backends/onnx/gonnxbackend.cpp @@ -702,7 +702,7 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp) cv::gimpl::onnx::addExecutionProvider(&session_options, ep); } - for (const auto &option : pp.session_options_map) { + for (const auto &option : pp.session_options) { session_options.AddConfigEntry(option.first.c_str(), option.second.c_str()); } From afcfba6fd38a3149fdd16851b0d314df31c7293d Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:23:52 +0100 Subject: [PATCH 3/8] Corrected a formatting error --- modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index bb40c4d70d1a..c6d6a2bd8427 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -675,8 +675,7 @@ class Params { @param model_path path to model file (.onnx file). */ Params(const std::string& tag, const std::string& model_path) - : desc{ - model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} + : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} /** @see onnx::Params::cfgMeanStdDev. */ void cfgMeanStdDev(const std::string &layer, From d03320bd482a702da243f2f7799759c70f9ef23d Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:25:13 +0100 Subject: [PATCH 4/8] Corrected a formatting error --- modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index c6d6a2bd8427..a2a971d0f5be 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -675,7 +675,7 @@ class Params { @param model_path path to model file (.onnx file). */ Params(const std::string& tag, const std::string& model_path) - : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} + : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} /** @see onnx::Params::cfgMeanStdDev. */ void cfgMeanStdDev(const std::string &layer, From 3386df5247f33e207c08fc11feabdd1513895bfd Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:34:50 +0100 Subject: [PATCH 5/8] Addressed some docstring comments --- .../gapi/include/opencv2/gapi/infer/onnx.hpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index a2a971d0f5be..ebed93a5d30f 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -637,7 +637,7 @@ template class Params { /** @brief Configures session options for ONNX Runtime. - This function allows setting various session options for the ONNX Runtime + This function is used for setting various session options for the ONNX Runtime session by accepting a map of key-value pairs. @param options A map of session option to be applied to the ONNX Runtime session. @@ -675,7 +675,26 @@ class Params { @param model_path path to model file (.onnx file). */ Params(const std::string& tag, const std::string& model_path) - : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} + : desc{ + model_path, // model_path + 0u, // num_in + 0u, // num_out + {}, // input_names + {}, // output_names + {}, // const_inputs + {}, // mean + {}, // stdev + {}, // out_metas + {}, // custom_post_proc + {}, // normalize + {}, // names_to_remap + true, // is_generic + {}, // generic_mstd + {}, // generic_norm + {}, // session_options + {}, // execution_providers + false // disable_mem_pattern + }, m_tag(tag) {} /** @see onnx::Params::cfgMeanStdDev. */ void cfgMeanStdDev(const std::string &layer, From d6801e98f02ab0141f53fd172fa75b49a92ab043 Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:36:24 +0100 Subject: [PATCH 6/8] Modified the docstring --- modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index ebed93a5d30f..d86f956436da 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -637,7 +637,7 @@ template class Params { /** @brief Configures session options for ONNX Runtime. - This function is used for setting various session options for the ONNX Runtime + This function is used to set various session options for the ONNX Runtime session by accepting a map of key-value pairs. @param options A map of session option to be applied to the ONNX Runtime session. From 51ecf1b1b6c7ff8030b4003442d9018d32e3e3b6 Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 11:43:50 +0100 Subject: [PATCH 7/8] Reverted back to the train constructor --- .../gapi/include/opencv2/gapi/infer/onnx.hpp | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index d86f956436da..4ed46bf6f77d 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -675,26 +675,7 @@ class Params { @param model_path path to model file (.onnx file). */ Params(const std::string& tag, const std::string& model_path) - : desc{ - model_path, // model_path - 0u, // num_in - 0u, // num_out - {}, // input_names - {}, // output_names - {}, // const_inputs - {}, // mean - {}, // stdev - {}, // out_metas - {}, // custom_post_proc - {}, // normalize - {}, // names_to_remap - true, // is_generic - {}, // generic_mstd - {}, // generic_norm - {}, // session_options - {}, // execution_providers - false // disable_mem_pattern - }, m_tag(tag) {} + : desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {} /** @see onnx::Params::cfgMeanStdDev. */ void cfgMeanStdDev(const std::string &layer, From bb5d50e8f862b4c9d931ae3862b1677070cf0234 Mon Sep 17 00:00:00 2001 From: Kadian Date: Thu, 20 Jun 2024 13:50:12 +0100 Subject: [PATCH 8/8] Added cfgSessionOptions to generic Params and made python related changes --- modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp | 3 +++ modules/gapi/include/opencv2/gapi/infer/onnx.hpp | 5 +++++ modules/gapi/src/backends/onnx/bindings_onnx.cpp | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp index fb2376ece881..0b6dab6a9d41 100644 --- a/modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp @@ -51,6 +51,9 @@ class GAPI_EXPORTS_W_SIMPLE PyParams { GAPI_WRAP PyParams& cfgDisableMemPattern(); + GAPI_WRAP + PyParams& cfgSessionOptions(const std::map& options); + GBackend backend() const; std::string tag() const; cv::util::any params() const; diff --git a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp index 4ed46bf6f77d..fd0f69a768e4 100644 --- a/modules/gapi/include/opencv2/gapi/infer/onnx.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/onnx.hpp @@ -719,6 +719,11 @@ class Params { desc.disable_mem_pattern = true; } + /** @see onnx::Params::cfgSessionOptions. */ + void cfgSessionOptions(const std::map& options) { + desc.session_options.insert(options.begin(), options.end()); + } + // BEGIN(G-API's network parametrization API) GBackend backend() const { return cv::gapi::onnx::backend(); } std::string tag() const { return m_tag; } diff --git a/modules/gapi/src/backends/onnx/bindings_onnx.cpp b/modules/gapi/src/backends/onnx/bindings_onnx.cpp index 0703f1753dd8..294ad8a3cc21 100644 --- a/modules/gapi/src/backends/onnx/bindings_onnx.cpp +++ b/modules/gapi/src/backends/onnx/bindings_onnx.cpp @@ -57,6 +57,12 @@ cv::gapi::onnx::PyParams::cfgDisableMemPattern() { return *this; } +cv::gapi::onnx::PyParams& +cv::gapi::onnx::PyParams::cfgSessionOptions(const std::map& options) { + m_priv->cfgSessionOptions(options); + return *this; +} + cv::gapi::GBackend cv::gapi::onnx::PyParams::backend() const { return m_priv->backend(); }