CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
Add python bindings for G-API onnx #22017
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
Conversation
You can clone opencv_extra and run this script to get the models for testing. |
@fengyuentau |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xiong-jie-y Thank you for the contribution!
@dmatveev Could you please take a look on this proposal?
Does someone know how to fix this CI error? |
class test_gapi_infer(NewOpenCVTests): | ||
def test_onnx_classification(self): | ||
model_path = self.find_file( | ||
"vision/classification/squeezenet/model/squeezenet1.0-9.onnx", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DNN models are optional (available if OPENCV_DNN_TEST_DATA_PATH
is specified).
Test should "skip" if file is not available.
Check this helper function: https://github.com/opencv/opencv/blob/4.5.5/modules/dnn/misc/python/test/test_dnn.py#L105-L110
There is missing onnx_models/
prefix also
model_path = self.find_file(
"onnx_models/vision/classification/squeezenet/model/squeezenet1.0-9.onnx",
[os.environ.get('OPENCV_DNN_TEST_DATA_PATH', os.getcwd()),
os.environ['OPENCV_TEST_DATA_PATH']],
required=False)
if model_path is None:
raise unittest.SkipTest("Missing DNN test file")
Finally, test should not fail with message G-API has been compiled without ONNX support
.
Test should catch this cv.error
, check its message and "skip" as unsupported configuration.
BTW, download .py file from "gapi" is not accurate. It doesn't support launching from opencv_extra. It must be launched from location pointed by OPENCV_DNN_TEST_DATA_PATH
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Done! 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alalek, is behavior of download gapi .py incorrect? What is expected?
I’m out of context, sorry.
Hi @alalek, Thank you for the previous review comment! This PR is not very emergent, but I want to merge this PR soon because I have more PRs that depends on this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for contribution!
@@ -67,6 +68,8 @@ struct ParamDesc { | |||
std::vector<bool> normalize; //!< Vector of bool values that enabled or disabled normalize of input data. | |||
|
|||
std::vector<std::string> names_to_remap; //!< Names of output layers that will be processed in PostProc function. | |||
|
|||
bool is_generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add documentation for this flag.
Does it really needed? (I see only false
initial value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it may be related to "generic infer" (with no network type defined), but let's @TolyaTalamanov to confirm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's needed, because only generic
infer can be exposed to python
@TolyaTalamanov @mpashchenkov do you have any comments here? |
There was a problem hiding this 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. Thanks @xiong-jie-y for your work!
@@ -67,6 +68,8 @@ struct ParamDesc { | |||
std::vector<bool> normalize; //!< Vector of bool values that enabled or disabled normalize of input data. | |||
|
|||
std::vector<std::string> names_to_remap; //!< Names of output layers that will be processed in PostProc function. | |||
|
|||
bool is_generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it may be related to "generic infer" (with no network type defined), but let's @TolyaTalamanov to confirm
@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}, m_tag(tag) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alalek here is where is_generic
is set to true
(in the tail)
Sorry for delay @xiong-jie-y , we're all on sort-of-leave now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically implementation is correct, but I'd ask @mpashchenkov to check if generic
onnx infer is handled properly.
|
||
GModel::Graph model(gr); | ||
auto& op = model.metadata(nh).get<Op>(); | ||
if (pp.is_generic) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpashchenkov Could review this part, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like true (analogue of IE generic). But I think that tests are required.
|
||
testdata_required = bool(os.environ.get('OPENCV_DNN_TEST_REQUIRE_TESTDATA', False)) | ||
|
||
class test_gapi_infer(NewOpenCVTests): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_gapi_infer
already exists, I'd change to test_gapi_onnx_infer
* @see struct Generic | ||
*/ | ||
template<> | ||
class Params<cv::gapi::Generic> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great to add test for generic
onnx inference because it becomes core functionality for c++ users.
@@ -67,6 +68,8 @@ struct ParamDesc { | |||
std::vector<bool> normalize; //!< Vector of bool values that enabled or disabled normalize of input data. | |||
|
|||
std::vector<std::string> names_to_remap; //!< Names of output layers that will be processed in PostProc function. | |||
|
|||
bool is_generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's needed, because only generic
infer can be exposed to python
@@ -735,7 +735,8 @@ void ONNXCompiled::extractMat(ONNXCallContext &ctx, const size_t in_idx, Views& | |||
} | |||
} | |||
|
|||
void ONNXCompiled::setOutput(int i, cv::Mat &m) { | |||
void ONNXCompiled::setOutput(int i, cv::Mat &m) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary change.
} | ||
// Adding const input is necessary because the definition of input_names | ||
// includes const input. | ||
for (const auto& a : pp.const_inputs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to add test for const input (generic case)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ONNXbackend doesn’t have tests for generic infer.
class test_gapi_infer(NewOpenCVTests): | ||
def test_onnx_classification(self): | ||
model_path = self.find_file( | ||
"vision/classification/squeezenet/model/squeezenet1.0-9.onnx", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alalek, is behavior of download gapi .py incorrect? What is expected?
I’m out of context, sorry.
if (pp.is_generic) { | ||
auto& info = cv::util::any_cast<cv::detail::InOutInfo>(op.params); | ||
|
||
for (const auto& a : info.in_names) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use loops for assignment?
|
||
GModel::Graph model(gr); | ||
auto& op = model.metadata(nh).get<Op>(); | ||
if (pp.is_generic) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like true (analogue of IE generic). But I think that tests are required.
@xiong-jie-y @mvskg Friendly reminder |
ONNX part looks like true but for "possible unexpected cases in the future" tests for generic part need to be added. |
Is there anything else blocking this PR from merge? |
I guess only lack of tests. This PR introduces |
I believe if there is python tests, that should be enough. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! 👍
Motivation
I think that the feature to run modle on onnxruntime is worth exposing to python G-API considering that
This PR is the first step to expose onnxruntime features to python and support of execution providers will follow later.
This PR is draft, so let's have a discussion if necessary 🙂
Remaining tasks
-> I will not implement this because it seems that the implementation in the onnx seems right from this line.
Questions
Example Code
https://github.com/xiong-jie-y/g_api_examples/blob/main/low_light_enhancement_onnx/enhance_low_light.py
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
There's no bug report.
Patch to opencv_extra has the same branch name.
I don't thik performance test is necessary this time, because it's just a binding.