Merge pull request #26293 from SeptimiuIoachimNeagaIntel:EISW-140103_optimization_flag

G-API: Introduce level optimization flag for ONNXRT backend #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
pull/18976/merge
Septimiu Neaga 1 month ago committed by GitHub
parent 489df18a13
commit 3919f33e21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      modules/gapi/include/opencv2/gapi/infer/bindings_onnx.hpp
  2. 20
      modules/gapi/include/opencv2/gapi/infer/onnx.hpp
  3. 6
      modules/gapi/src/backends/onnx/bindings_onnx.cpp
  4. 24
      modules/gapi/src/backends/onnx/gonnxbackend.cpp

@ -54,6 +54,9 @@ public:
GAPI_WRAP
PyParams& cfgSessionOptions(const std::map<std::string, std::string>& options);
GAPI_WRAP
PyParams& cfgOptLevel(const int opt_level);
GBackend backend() const;
std::string tag() const;
cv::util::any params() const;

@ -15,6 +15,7 @@
#include <opencv2/gapi/opencv_includes.hpp>
#include <opencv2/gapi/util/any.hpp>
#include <opencv2/gapi/util/optional.hpp>
#include <opencv2/core/cvdef.h> // GAPI_EXPORTS
#include <opencv2/gapi/gkernel.hpp> // GKernelPackage
@ -354,6 +355,7 @@ struct ParamDesc {
std::map<std::string, std::string> session_options;
std::vector<cv::gapi::onnx::ep::EP> execution_providers;
bool disable_mem_pattern;
cv::util::optional<int> opt_level;
};
} // namespace detail
@ -648,6 +650,17 @@ public:
return *this;
}
/** @brief Configures optimization level for ONNX Runtime.
@param opt_level [optimization level]: Valid values are 0 (disable), 1 (basic), 2 (extended), 99 (all).
Please see onnxruntime_c_api.h (enum GraphOptimizationLevel) for the full list of all optimization levels.
@return the reference on modified object.
*/
Params<Net>& cfgOptLevel(const int opt_level) {
desc.opt_level = cv::util::make_optional(opt_level);
return *this;
}
// BEGIN(G-API's network parametrization API)
GBackend backend() const { return cv::gapi::onnx::backend(); }
std::string tag() const { return Net::tag(); }
@ -675,7 +688,7 @@ public:
@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,
@ -724,6 +737,11 @@ public:
desc.session_options.insert(options.begin(), options.end());
}
/** @see onnx::Params::cfgOptLevel. */
void cfgOptLevel(const int opt_level) {
desc.opt_level = cv::util::make_optional(opt_level);
}
// BEGIN(G-API's network parametrization API)
GBackend backend() const { return cv::gapi::onnx::backend(); }
std::string tag() const { return m_tag; }

@ -63,6 +63,12 @@ cv::gapi::onnx::PyParams::cfgSessionOptions(const std::map<std::string, std::str
return *this;
}
cv::gapi::onnx::PyParams&
cv::gapi::onnx::PyParams::cfgOptLevel(const int opt_level) {
m_priv->cfgOptLevel(opt_level);
return *this;
}
cv::gapi::GBackend cv::gapi::onnx::PyParams::backend() const {
return m_priv->backend();
}

@ -701,6 +701,26 @@ namespace cv {
namespace gimpl {
namespace onnx {
static GraphOptimizationLevel convertToGraphOptimizationLevel(const int opt_level) {
switch (opt_level) {
case ORT_DISABLE_ALL:
return ORT_DISABLE_ALL;
case ORT_ENABLE_BASIC:
return ORT_ENABLE_BASIC;
case ORT_ENABLE_EXTENDED:
return ORT_ENABLE_EXTENDED;
case ORT_ENABLE_ALL:
return ORT_ENABLE_ALL;
default:
if (opt_level > ORT_ENABLE_ALL) { // relax constraint
return ORT_ENABLE_ALL;
}
else {
cv::util::throw_error(std::invalid_argument("Invalid argument opt_level = " + std::to_string(opt_level)));
}
}
}
ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
: params(pp) {
// Validate input parameters before allocating any resources
@ -726,6 +746,10 @@ ONNXCompiled::ONNXCompiled(const gapi::onnx::detail::ParamDesc &pp)
if (pp.disable_mem_pattern) {
session_options.DisableMemPattern();
}
if (pp.opt_level.has_value()) {
session_options.SetGraphOptimizationLevel(convertToGraphOptimizationLevel(pp.opt_level.value()));
}
this_env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "");
#ifndef _WIN32
this_session = Ort::Session(this_env, params.model_path.data(), session_options);

Loading…
Cancel
Save