diff --git a/modules/gapi/include/opencv2/gapi/infer/ov.hpp b/modules/gapi/include/opencv2/gapi/infer/ov.hpp index 782792489b..9515744cac 100644 --- a/modules/gapi/include/opencv2/gapi/infer/ov.hpp +++ b/modules/gapi/include/opencv2/gapi/infer/ov.hpp @@ -99,6 +99,8 @@ struct ParamDesc { PluginConfigT config; size_t nireq = 1; + + bool ensure_named_tensors = false; }; // NB: Just helper to avoid code duplication. @@ -205,6 +207,24 @@ public: return *this; } + /** @brief Ensures the model has named tensors. + + This function is used to ensure that all tensors in the model have names. + It goes through all input and output nodes of the model and sets the names + if they are not set. This is neccessary for models with nameless tensors. + + If a tensor does not have a name, it will be assigned a default name + based on the producer node's friendly name. If the producer node has multiple + outputs, the name will be in the form "node_name:N", where N is the output index. + + @param flag If true, then it guarantees that all tensors will have names. + @return reference to this parameter structure. + */ + Params& cfgEnsureNamedTensors(bool flag = true) { + m_desc.ensure_named_tensors = flag; + return *this; + } + /** @brief Specifies tensor layout for an input layer. The function is used to set tensor layout for an input layer. @@ -524,6 +544,12 @@ public: return *this; } + /** @see ov::Params::cfgEnsureNamedTensors. */ + Params& cfgEnsureNamedTensors(bool flag = true) { + m_desc.ensure_named_tensors = flag; + return *this; + } + /** @see ov::Params::cfgInputTensorLayout. */ Params& cfgInputTensorLayout(std::string layout) { detail::getModelToSetAttrOrThrow(m_desc.kind, "input tensor layout") diff --git a/modules/gapi/src/backends/ov/govbackend.cpp b/modules/gapi/src/backends/ov/govbackend.cpp index ed2b6fd94e..dbaba382db 100644 --- a/modules/gapi/src/backends/ov/govbackend.cpp +++ b/modules/gapi/src/backends/ov/govbackend.cpp @@ -69,6 +69,27 @@ ov::Core cv::gapi::ov::wrap::getCore() { ? create_OV_Core_pointer() : create_OV_Core_instance(); } +static std::string make_default_tensor_name(const ov::Output& output) { + auto default_name = output.get_node()->get_friendly_name(); + if (output.get_node()->get_output_size() > 1) { + default_name += ':' + std::to_string(output.get_index()); + } + return default_name; +} + +static void ensureNamedTensors(std::shared_ptr model) { + for (auto& input : model->inputs()) { + if (input.get_names().empty()) { + input.set_names({make_default_tensor_name(input)}); + } + } + for (auto& output : model->outputs()) { + if (output.get_names().empty()) { + output.set_names({make_default_tensor_name(output)}); + } + } +} + static ov::AnyMap toOV(const ParamDesc::PluginConfigT &config) { return {config.begin(), config.end()}; } @@ -236,6 +257,10 @@ struct OVUnit { .read_model(desc.model_path, desc.bin_path); GAPI_Assert(model); + if (params.ensure_named_tensors) { + ensureNamedTensors(model); + } + if (params.num_in == 1u && params.input_names.empty()) { params.input_names = { model->inputs().begin()->get_any_name() }; }