Add python bindings for G-API onnx#22017
Add python bindings for G-API onnx#22017asmorkalov merged 1 commit intoopencv:4.xfrom xiong-jie-y:py_onnx
Conversation
You can clone opencv_extra and run this script to get the models for testing. |
|
@fengyuentau |
alalek
left a comment
There was a problem hiding this comment.
@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.
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.
Thank you! Done! 🙂
There was a problem hiding this comment.
@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. |
|
|
||
| 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.
Please add documentation for this flag.
Does it really needed? (I see only false initial value)
There was a problem hiding this comment.
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.
Yes, it's needed, because only generic infer can be exposed to python
|
@TolyaTalamanov @mpashchenkov do you have any comments here? |
dmatveev
left a comment
There was a problem hiding this comment.
Looks good to me. Thanks @xiong-jie-y for your work!
|
|
||
| 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.
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.
@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 |
TolyaTalamanov
left a comment
There was a problem hiding this comment.
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.
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.
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.
It'd be great to add test for generic onnx inference because it becomes core functionality for c++ users.
|
|
||
| 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.
Yes, it's needed, because only generic infer can be exposed to python
| } | ||
|
|
||
| void ONNXCompiled::setOutput(int i, cv::Mat &m) { | ||
| void ONNXCompiled::setOutput(int i, cv::Mat &m) |
| } | ||
| // 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.
Is it necessary to add test for const input (generic case)?
There was a problem hiding this comment.
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.
@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) |
|
|
||
| GModel::Graph model(gr); | ||
| auto& op = model.metadata(nh).get<Op>(); | ||
| if (pp.is_generic) { |
There was a problem hiding this comment.
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. |
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.