Merge pull request #25662 from ujjayant-kadian:uk/port-gapi-onnxrt-backend-into-v2-api

Port G-API ONNXRT backend into V2 API #25662

### 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:
Ujjayant Kadian 2024-05-31 15:01:46 +01:00 committed by GitHub
parent d7f04a9d33
commit dcce2b8b24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 9 deletions

View File

@ -11,6 +11,7 @@
#include <string>
#include <array>
#include <tuple> // tuple, tuple_size
#include <map>
#include <opencv2/gapi/opencv_includes.hpp>
#include <opencv2/gapi/util/any.hpp>
@ -156,13 +157,24 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
Constructs OpenVINO parameters based on device type information.
@param dev_type Target device type to use. ("CPU_FP32", "GPU_FP16", etc)
@param dev_type Target device type to use. ("CPU", "GPU", "GPU.0" etc)
*/
GAPI_WRAP
explicit OpenVINO(const std::string &dev_type)
: device_type(dev_type) {
}
/** @brief Class constructor.
Constructs OpenVINO parameters based on map of options passed.
* @param params A map of parameter names and their corresponding string values.
*/
GAPI_WRAP
explicit OpenVINO(const std::map<std::string, std::string>& params)
: params_map(params) {
}
/** @brief Specifies OpenVINO Execution Provider cache dir.
This function is used to explicitly specify the path to save and load
@ -173,6 +185,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
*/
GAPI_WRAP
OpenVINO& cfgCacheDir(const std::string &dir) {
if (!params_map.empty()) {
cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if"
"created from the parameters map."));
}
cache_dir = dir;
return *this;
}
@ -187,6 +203,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
*/
GAPI_WRAP
OpenVINO& cfgNumThreads(size_t nthreads) {
if (!params_map.empty()) {
cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if"
"created from the parameters map."));
}
num_of_threads = nthreads;
return *this;
}
@ -200,6 +220,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
*/
GAPI_WRAP
OpenVINO& cfgEnableOpenCLThrottling() {
if (!params_map.empty()) {
cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if"
"created from the parameters map."));
}
enable_opencl_throttling = true;
return *this;
}
@ -216,6 +240,10 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
*/
GAPI_WRAP
OpenVINO& cfgEnableDynamicShapes() {
if (!params_map.empty()) {
cv::util::throw_error(std::logic_error("ep::OpenVINO cannot be changed if"
"created from the parameters map."));
}
enable_dynamic_shapes = true;
return *this;
}
@ -225,6 +253,7 @@ struct GAPI_EXPORTS_W_SIMPLE OpenVINO {
size_t num_of_threads = 0;
bool enable_opencl_throttling = false;
bool enable_dynamic_shapes = false;
std::map<std::string, std::string> params_map;
};
/**

View File

@ -178,16 +178,26 @@ static void addTensorRTExecutionProvider(Ort::SessionOptions *session_options,
static void addOpenVINOExecutionProvider(Ort::SessionOptions *session_options,
const cv::gapi::onnx::ep::OpenVINO &ov_ep) {
OrtOpenVINOProviderOptions options{};
options.device_type = ov_ep.device_type.c_str();
options.cache_dir = ov_ep.cache_dir.c_str();
options.num_of_threads = ov_ep.num_of_threads;
options.enable_opencl_throttling = ov_ep.enable_opencl_throttling;
options.enable_dynamic_shapes = ov_ep.enable_dynamic_shapes;
options.context = nullptr;
std::unordered_map<std::string, std::string> options;
try {
session_options->AppendExecutionProvider_OpenVINO(options);
// If the OpenVINO Execution Provider object was initialized with a parameters map,
// those parameters are used directly.
// Otherwise, the function constructs the options map from the individual member
// variables of the OpenVINO object.
if (ov_ep.params_map.empty()) {
options = {
{"device_type", ov_ep.device_type},
{"cache_dir", ov_ep.cache_dir},
{"num_of_threads", ov_ep.num_of_threads > 0 ? std::to_string(ov_ep.num_of_threads) : ""},
{"enable_opencl_throttling", ov_ep.enable_opencl_throttling ? "True" : "False"},
{"enable_dynamic_shapes", ov_ep.enable_dynamic_shapes ? "True" : "False"},
};
} else {
options.insert(ov_ep.params_map.begin(), ov_ep.params_map.end());
}
// AppendExecutionProvider function expects a const std::unordered_map as its second argument
session_options->AppendExecutionProvider("OpenVINO", options);
} catch (const std::exception &e) {
std::stringstream ss;
ss << "ONNX Backend: Failed to enable OpenVINO"