Skip to content

G-API: Support vaargs for cv.compile_args#20196

Merged
alalek merged 4 commits intoopencv:masterfrom
TolyaTalamanov:at/support-vaargs-compile-args
Jun 18, 2021
Merged

G-API: Support vaargs for cv.compile_args#20196
alalek merged 4 commits intoopencv:masterfrom
TolyaTalamanov:at/support-vaargs-compile-args

Conversation

@TolyaTalamanov
Copy link
Copy Markdown
Contributor

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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

Build configuration

force_builders=Custom,Custom Win,Custom Mac
build_gapi_standalone:Linux x64=ade-0.1.1f
build_gapi_standalone:Win64=ade-0.1.1f
build_gapi_standalone:Mac=ade-0.1.1f
build_gapi_standalone:Linux x64 Debug=ade-0.1.1f

Xbuild_image:Custom=centos:7
Xbuildworker:Custom=linux-1
build_gapi_standalone:Custom=ade-0.1.1f

build_image:Custom=ubuntu-openvino-2021.3.0:20.04
build_image:Custom Win=openvino-2021.2.0
build_image:Custom Mac=openvino-2021.2.0

test_modules:Custom=gapi,python2,python3,java
test_modules:Custom Win=gapi,python2,python3,java
test_modules:Custom Mac=gapi,python2,python3,java

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@dmatveev @mpashchenkov @sivanov-work Folks, have a look, please

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

TolyaTalamanov commented Jun 2, 2021

@alalek Compatibility with python2 seems to be broken, can we exclude python2 since it's already deprecated ?

GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GNetPackage pkg);
GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GKernelPackage kernels, gapi::GNetPackage nets);
struct GAPI_EXPORTS_W_SIMPLE GCompileArg {
GAPI_WRAP GCompileArg(gapi::GKernelPackage pkg);
Copy link
Copy Markdown
Contributor Author

@TolyaTalamanov TolyaTalamanov Jun 2, 2021

Choose a reason for hiding this comment

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

To extend number of possible types from another module need to do:

// shadow_somemodule.hpp
struct GCompileArg {
    GAPI_WRAP GCompileArg(module::SomeArg arg);
};

Copy link
Copy Markdown
Contributor Author

@TolyaTalamanov TolyaTalamanov Jun 2, 2021

Choose a reason for hiding this comment

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

This code adds new overload to GCompileArg ctor

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But how did you solve variadic arguments passing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's solved indirectly through python's compile_args (which probably requires a better PR name since it's confusing - vaargs is a thing in C++ as well).

map() calls a function for each element of a range. given that the function is a GCompileArg ctor, we convert "raw" args into a range containing cv2.GCompileArg(arg) elements (by the way, does ctor qualify as a function?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GNetPackage pkg);
GAPI_EXPORTS_W GCompileArgs compile_args(gapi::GKernelPackage kernels, gapi::GNetPackage nets);
struct GAPI_EXPORTS_W_SIMPLE GCompileArg {
GAPI_WRAP GCompileArg(gapi::GKernelPackage pkg);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's solved indirectly through python's compile_args (which probably requires a better PR name since it's confusing - vaargs is a thing in C++ as well).

map() calls a function for each element of a range. given that the function is a GCompileArg ctor, we convert "raw" args into a range containing cv2.GCompileArg(arg) elements (by the way, does ctor qualify as a function?)

struct GAPI_EXPORTS_W_SIMPLE GCompileArg {
GAPI_WRAP GCompileArg(gapi::GKernelPackage pkg);
GAPI_WRAP GCompileArg(gapi::GNetPackage pkg);
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

while I like the idea, this API is not quite equivalent. what if, as a user, I have my own gapi::GMyOwnPackage - how can I use compile_args machinery to wrap this external-to-G-API package?
it seems remotely possible with the old API (just add a new overload of compile_args) - albeit, I'm not sure if python has anything like function overload resolution/adl (wondering how this old API works) - if this is out of scope for this (e.g. G-API only supports packages that it provides itself via compile_args and users have to do something else), then feel free to ignore this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Wow, glad to see u :)

As for gapi::GMyOwnPackage what do u mean ? Do u want to use your own compile argument ?
From python user can't define new compile argument.

Copy link
Copy Markdown
Member

@andrey-golubev andrey-golubev Jun 6, 2021

Choose a reason for hiding this comment

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

Wow, glad to see u :)

Just occasionally looking at things here, nothing too serious 😉

As for gapi::GMyOwnPackage what do u mean ? Do u want to use your own compile argument ?

Yes, imagine such a use case (maybe someone wants their own package - it is the thing that provides the kernels/etc, right? can't remember anymore).

From python user can't define new compile argument.

Well, it should be doable from C++ and then C++ gets magically converted into python - I guess, ideally, G-API should be convenient to use from python as well, so if someone supplies their own package, it could be used from python API as well.
However, this is a purely artificial use case. I wrote this comment in the meaning of:
you change the API and this (might) limit the usability - e.g. the ability to use custom packages in python. if this was never a concern, then this change is perfectly fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(glad to see you as well!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, imagine such a use case (maybe someone wants their own package - it is the thing that provides the kernels/etc, right? can't remember anymore).

Again, what's the purpose of the gapi::GMyOwnPackage ? First of all it won't be recognized inside G-API, right ? Secondly if the user want to pass his own set of kernels it can be easily done by using cv.gapi.kernels(....) which returns gapi::GKernelPackage

I mean that user defined compile arguments are useless because there is no support inside G-API to handle them.

Copy link
Copy Markdown
Member

@andrey-golubev andrey-golubev Jun 8, 2021

Choose a reason for hiding this comment

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

ah, I get it now. no objections then.

Comment on lines +160 to +162
self.assertEqual(centers.shape[1], sz[1]);
self.assertEqual(centers.shape[0], K);
self.assertTrue(centers.size != 0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@TolyaTalamanov TolyaTalamanov force-pushed the at/support-vaargs-compile-args branch from 6f8d6e7 to ca3787d Compare June 15, 2021 15:07
@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@alalek Can it be merged ?



@register('cv2')
def compile_args(*args):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cv2 => cv2.gapi ?

(as there is no "G" prefix)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

but in c++ it's a part of cv namespace, not cv::gapi

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It will be a bit weird to keep it in cv.gapi package in that case. Are u Ok to leave it as is ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@dmatveev Please confirm this usage.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@alalek yes somehow we have cv::compile_args but not cv::gapi::compile_args in our C++ API.

It is definitely worth aligning in 5.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then it makes sense to keep it in cv.gapi for python.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@alalek

@alalek alalek merged commit 53eca2f into opencv:master Jun 18, 2021
@alalek alalek mentioned this pull request Oct 15, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
…-compile-args

G-API: Support vaargs for cv.compile_args

* Support cv.compile_args to work with variadic number of inputs

* Disable python2.x G-API

* Move compile_args to gapi pkg
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.

6 participants