Skip to content

[feature request] Derive all transforms classes from nn.Module? #5236

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

Closed
vadimkantorov opened this issue Jan 20, 2022 · 9 comments
Closed

[feature request] Derive all transforms classes from nn.Module? #5236

vadimkantorov opened this issue Jan 20, 2022 · 9 comments

Comments

@vadimkantorov
Copy link

vadimkantorov commented Jan 20, 2022

🚀 The feature

Currently it's already the case for most transform classes, except T.Compose / T.RandomChoice (these could probably be derived from nn.Sequential / nn.ModuleList). This also would not break any compat, and improve consistency and composability (sometimes only nn.Modules are accepted as parts of other existing models)

Motivation, pitch

N/A

Alternatives

No response

Additional context

No response

cc @vfdev-5 @datumbox @bjuncek

@datumbox
Copy link
Contributor

@vadimkantorov You are right to say that. There is a handful of them that don't derive from it and the reason is mainly that they might not be JIT-scriptable and that might break BC. I think there is a lot of merit to make all classes inherit from nn.Module. The new Transforms API is the place to address it (see here).

@vadimkantorov
Copy link
Author

vadimkantorov commented Jan 20, 2022

T.Compose probably is scriptable, the rest probably too if we migrate from random. to using torch random functions? But yeah, that'd be a switch to a different random algo

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jan 21, 2022

@vadimkantorov have you seen discussion from this PR (#2645), why classes like T.Compose were not derived from nn.Module etc ? TLDR, keep BC with previous versions and support lambda functions.

@vadimkantorov
Copy link
Author

vadimkantorov commented Jan 21, 2022

Thanks for the reference! I tried to understand why deriving from nn.Sequential would existing some code, but I couldn't since the discussion is quite dispersed there.

What are concrete examples you had in mind in that PR? Is it because you worry of the code that's directly modifying internal transforms field? I didn't see a lot of code like this in the wild. Some solutions I can see:

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jan 21, 2022

Thanks for the reference! I tried to understand why deriving from nn.Sequential would existing some code, but I couldn't since the discussion is quite dispersed there.

First point is that we can not do basic thing like: T.Compose([T.Resize(200), lambda x: x * 0.5, my_transform_plain_function]) as lambda functions are not nn.Module and then T.Compose being inherited from nn.Module or nn.Sequential wont accept that.
To solve this we have to wrap all callables non-nn-modules with something from torchvision.

Next, if we wrap these custom functions by something then user can lose the access to its possible attributes etc. Imagine,
t = T.Compose([T.Resize(200), MyCustomTransform(*args, **kwargs)]) and t.transforms[1].call_custom_method(...). This wont be possible if we do not do some __getattr__ magic...

@vadimkantorov
Copy link
Author

vadimkantorov commented Jan 21, 2022

Thanks for explaining! Now I see!

My proposal would be:

@vadimkantorov
Copy link
Author

Maybe RandomChoice is more safe for conversion to nn.ModuleList, as it's not as old class

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Aug 19, 2022

Proto transforms now all derive from nn.Module

@vfdev-5 vfdev-5 closed this as completed Aug 19, 2022
@vadimkantorov
Copy link
Author

Is there now a nn.Sequential/nn.ModuleList-derived TupleSequential? Or what is the format of instances in proto? Are they now all dicts? Is there now also a nn.Module-derived T.Lambda wrapper?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants