Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G-API: Get input model layout from the IR if possible in OV 2.0 backend #24658

Merged
merged 3 commits into from Dec 13, 2023

Conversation

smirnov-alexey
Copy link
Contributor

@smirnov-alexey smirnov-alexey commented Dec 6, 2023

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

@smirnov-alexey
Copy link
Contributor Author

This PR fixes smart classroom G-API demo in OMZ: openvinotoolkit/open_model_zoo#3881


auto f = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data1}, "function_name");

ov::serialize(f, model_path, weights_path, ov::pass::Serialize::Version::IR_V11);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably there's some general approach to producing temporary files in tests in OpenCV.. Like some specific folder or naming convention?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw maybe we in the future we need to extend our API to accept objects of ov::Model directly instead of xml/bin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw maybe we in the future we need to extend our API to accept objects of ov::Model directly instead of xml/bin

In that case it won't be ov::Model because headers will depend on OpenVINO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dude we did this with PlaidML already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plaidml.hpp looks clear from framework details

@dmatveev dmatveev self-assigned this Dec 7, 2023
@dmatveev dmatveev added this to the 4.9.0 milestone Dec 7, 2023
@dmatveev
Copy link
Contributor

dmatveev commented Dec 7, 2023

@TolyaTalamanov do you have any comments here?

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()) {
input_info.model().set_layout(input_layout);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input_info.model() and m_model should refer to the same object, I'm wondering why this line is even needed.

Looks even though model has model layout it must be specified to PrePostProcessor as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth a comment at least, since it's not clear why we need to specify model layout for the preprocessor created from exactly the same model.

PrePostProcessor ppp(model);
audo layout = ov::layout::get::layout(model->input(input_name));
if (!layout.empty()) {
    ppp.input(input_name).model().set_layout(layout);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looks we figured out it locally. Need to force model layout only if model doesn't have one otherwise keep it as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works without this line. OV works fine

ov::layout::set_layout(data1, ov::Layout("NHWC"));

auto sin = std::make_shared<ov::opset8::Sin>(data1);
sin->output(0).set_names({"sin_t"}); // tensor name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessarily needed to specify name there


auto f = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data1}, "function_name");

ov::serialize(f, model_path, weights_path, ov::pass::Serialize::Version::IR_V11);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw maybe we in the future we need to extend our API to accept objects of ov::Model directly instead of xml/bin

In that case it won't be ov::Model because headers will depend on OpenVINO

model_path,
weights_path,
device_id);
EXPECT_NO_THROW(comp.apply(cv::gin(in_mat1), cv::gout(gapi_mat),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I didn't get the purpose of this test, the model has NHWC layout but you pass NCHW mat, so it won't throw any exceptions because ov backend doesn't validate either layouts match or not.

The test should be something like that:

  1. Model has NHWC layout
  2. Provide 4D mat and specify only tensor layout (e.g cfgInputTensorLayout('NCHW'))
  3. Check if there is NHWC->NCHW conversion is added to the model. (it won't be added without your changes, since model layout not specified therefore won't be propagated into PrePostProcessor)

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()) {
input_info.model().set_layout(input_layout);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth a comment at least, since it's not clear why we need to specify model layout for the preprocessor created from exactly the same model.

PrePostProcessor ppp(model);
audo layout = ov::layout::get::layout(model->input(input_name));
if (!layout.empty()) {
    ppp.input(input_name).model().set_layout(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()) {
input_info.model().set_layout(input_layout);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it on the second day, is this a valid change at all? Why we're setting whole model's layout based on some input layout?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not for the whole model, it's set to input_info which refers to the particular input layer.

input_info = m_ppp.input(input_name);
...
input_info.model().set_layout(input_layout);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will be removed, since OV does it in their PrePostProc

@dmatveev
Copy link
Contributor

dmatveev commented Dec 8, 2023

Please explain what is the issue and what is the change here.

@smirnov-alexey
Copy link
Contributor Author

Please explain what is the issue and what is the change here.

The issue is that user might not want to configure input model layout which is available in the network. So, they don't set cfgLayouts(). In this case OV backend assumes by default it's always NCHW which is not the case for some models (e.g. in OMZ smart classroom gapi demo). I'll check if OV already knows proper input layout and if it's there, we do nothing (instead of setting NCHW)

Copy link
Contributor

@TolyaTalamanov TolyaTalamanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@smirnov-alexey
Copy link
Contributor Author

@asmorkalov @mshabunin can we merge this please?

@smirnov-alexey
Copy link
Contributor Author

@asmorkalov @mshabunin kind reminder

@smirnov-alexey
Copy link
Contributor Author

@asmorkalov @mshabunin can we merge it this week pretty please?

Copy link
Contributor

@mshabunin mshabunin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@asmorkalov asmorkalov merged commit 14688e9 into opencv:4.x Dec 13, 2023
26 checks passed
@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants