From 14688e95ea7c1779ec90a0035dee1ed72093ab32 Mon Sep 17 00:00:00 2001 From: Alexey Smirnov Date: Wed, 13 Dec 2023 15:40:08 +0000 Subject: [PATCH] Merge pull request #24658 from smirnov-alexey:as/gapi_ov_get_model_layout G-API: Get input model layout from the IR if possible in OV 2.0 backend #24658 ### 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 - [x] 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 --- modules/gapi/src/backends/ov/govbackend.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/gapi/src/backends/ov/govbackend.cpp b/modules/gapi/src/backends/ov/govbackend.cpp index a139982e48..7e99af4f77 100644 --- a/modules/gapi/src/backends/ov/govbackend.cpp +++ b/modules/gapi/src/backends/ov/govbackend.cpp @@ -774,14 +774,19 @@ public: if (explicit_in_model_layout) { input_info.model().set_layout(::ov::Layout(*explicit_in_model_layout)); } else if (m_model->input(input_name).get_shape().size() == 4u) { - // NB: Back compatibility with IR's without any layout information. - // Note that default is only applicable for 4D inputs in order to - // support auto resize for image use cases. - GAPI_LOG_WARNING(NULL, "Failed to find layout for input layer \"" - << input_name << "\" - NCHW is set by default"); - const std::string default_layout = "NCHW"; - input_info.model().set_layout(::ov::Layout(default_layout)); - m_input_model_layout.emplace(input_name, default_layout); + const auto& input_layout = ::ov::layout::get_layout(m_model->input(input_name)); + if (!input_layout.empty()) { + GAPI_LOG_INFO(NULL, "Model input layout " << input_name << " found: " << input_layout.to_string() << "."); + } else { + // NB: Back compatibility with IR's without any layout information. + // Note that default is only applicable for 4D inputs in order to + // support auto resize for image use cases. + GAPI_LOG_WARNING(NULL, "Failed to find layout for input layer \"" + << input_name << "\" - NCHW is set by default"); + const std::string default_layout = "NCHW"; + input_info.model().set_layout(::ov::Layout(default_layout)); + m_input_model_layout.emplace(input_name, default_layout); + } } const auto explicit_in_tensor_layout = lookUp(m_input_tensor_layout, input_name); if (explicit_in_tensor_layout) {