Skip to content

Commit 3919f33

Browse files
Merge pull request opencv#26293 from SeptimiuIoachimNeagaIntel:EISW-140103_optimization_flag
G-API: Introduce level optimization flag for ONNXRT backend opencv#26293 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] 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 - [x] 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. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 489df18 commit 3919f33

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class GAPI_EXPORTS_W_SIMPLE PyParams {
5454
GAPI_WRAP
5555
PyParams& cfgSessionOptions(const std::map<std::string, std::string>& options);
5656

57+
GAPI_WRAP
58+
PyParams& cfgOptLevel(const int opt_level);
59+
5760
GBackend backend() const;
5861
std::string tag() const;
5962
cv::util::any params() const;

modules/gapi/include/opencv2/gapi/infer/onnx.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <opencv2/gapi/opencv_includes.hpp>
1717
#include <opencv2/gapi/util/any.hpp>
18+
#include <opencv2/gapi/util/optional.hpp>
1819

1920
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
2021
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
@@ -354,6 +355,7 @@ struct ParamDesc {
354355
std::map<std::string, std::string> session_options;
355356
std::vector<cv::gapi::onnx::ep::EP> execution_providers;
356357
bool disable_mem_pattern;
358+
cv::util::optional<int> opt_level;
357359
};
358360
} // namespace detail
359361

@@ -648,6 +650,17 @@ template<typename Net> class Params {
648650
return *this;
649651
}
650652

653+
/** @brief Configures optimization level for ONNX Runtime.
654+
655+
@param opt_level [optimization level]: Valid values are 0 (disable), 1 (basic), 2 (extended), 99 (all).
656+
Please see onnxruntime_c_api.h (enum GraphOptimizationLevel) for the full list of all optimization levels.
657+
@return the reference on modified object.
658+
*/
659+
Params<Net>& cfgOptLevel(const int opt_level) {
660+
desc.opt_level = cv::util::make_optional(opt_level);
661+
return *this;
662+
}
663+
651664
// BEGIN(G-API's network parametrization API)
652665
GBackend backend() const { return cv::gapi::onnx::backend(); }
653666
std::string tag() const { return Net::tag(); }
@@ -675,7 +688,7 @@ class Params<cv::gapi::Generic> {
675688
@param model_path path to model file (.onnx file).
676689
*/
677690
Params(const std::string& tag, const std::string& model_path)
678-
: desc{model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false}, m_tag(tag) {}
691+
: desc{ model_path, 0u, 0u, {}, {}, {}, {}, {}, {}, {}, {}, {}, true, {}, {}, {}, {}, false, {} }, m_tag(tag) {}
679692

680693
/** @see onnx::Params::cfgMeanStdDev. */
681694
void cfgMeanStdDev(const std::string &layer,
@@ -724,6 +737,11 @@ class Params<cv::gapi::Generic> {
724737
desc.session_options.insert(options.begin(), options.end());
725738
}
726739

740+
/** @see onnx::Params::cfgOptLevel. */
741+
void cfgOptLevel(const int opt_level) {
742+
desc.opt_level = cv::util::make_optional(opt_level);
743+
}
744+
727745
// BEGIN(G-API's network parametrization API)
728746
GBackend backend() const { return cv::gapi::onnx::backend(); }
729747
std::string tag() const { return m_tag; }

modules/gapi/src/backends/onnx/bindings_onnx.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ cv::gapi::onnx::PyParams::cfgSessionOptions(const std::map<std::string, std::str
6363
return *this;
6464
}
6565

66+
cv::gapi::onnx::PyParams&
67+
cv::gapi::onnx::PyParams::cfgOptLevel(const int opt_level) {
68+
m_priv->cfgOptLevel(opt_level);
69+
return *this;
70+
}
71+
6672
cv::gapi::GBackend cv::gapi::onnx::PyParams::backend() const {
6773
return m_priv->backend();
6874
}

modules/gapi/src/backends/onnx/gonnxbackend.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,26 @@ namespace cv {
701701
namespace gimpl {
702702
namespace onnx {
703703

704+
static GraphOptimizationLevel convertToGraphOptimizationLevel(const int opt_level) {
705+
switch (opt_level) {
706+
case ORT_DISABLE_ALL:
707+
return ORT_DISABLE_ALL;
708+
case ORT_ENABLE_BASIC:
709+
return ORT_ENABLE_BASIC;
710+
case ORT_ENABLE_EXTENDED:
711+
return ORT_ENABLE_EXTENDED;
712+
case ORT_ENABLE_ALL:
713+
return ORT_ENABLE_ALL;
714+
default:
715+
if (opt_level > ORT_ENABLE_ALL) { // relax constraint
716+
return ORT_ENABLE_ALL;
717+
}
718+
else {
719+
cv::util::throw_error(std::invalid_argument("Invalid argument opt_level = " + std::to_string(opt_level)));
720+
}
721+
}
722+
}
723+
704724
ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
705725
: params(pp) {
706726
// Validate input parameters before allocating any resources
@@ -726,6 +746,10 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
726746
if (pp.disable_mem_pattern) {
727747
session_options.DisableMemPattern();
728748
}
749+
750+
if (pp.opt_level.has_value()) {
751+
session_options.SetGraphOptimizationLevel(convertToGraphOptimizationLevel(pp.opt_level.value()));
752+
}
729753
this_env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "");
730754
#ifndef _WIN32
731755
this_session = Ort::Session(this_env, params.model_path.data(), session_options);

0 commit comments

Comments
 (0)