-
Notifications
You must be signed in to change notification settings - Fork 7k
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
[PoC] reinstate get_params
#7095
Conversation
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.
Let's discuss this design based on RandomErasing
. I don't expect any large differences for the other transforms. I'll add them when we are happy with the design.
ratio: Tuple[float, float], | ||
value: Optional[List[float]] = None, | ||
) -> Tuple[int, int, int, int, torch.Tensor]: | ||
img_c, img_h, img_w = get_dimensions(image) |
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.
In v2, we use the query_chw
idiom, since in general the transform should not be bound to images and we can extract this information from multiple types. Since we already have an image here, we can extract directly and pass that.
) -> Tuple[int, int, int, int, torch.Tensor]: | ||
img_c, img_h, img_w = get_dimensions(image) | ||
i, j, h, w, v = RandomErasing._get_params_internal( | ||
img_c, img_h, img_w, scale, torch.log(torch.tensor(ratio)), 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.
Instead of passing ratio
directly, v2 stores the log ratio as instance attribute to avoid the re-computation over and over again.
if v is None: | ||
v = image |
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.
In v2, this whole transformation is a no-op in case the value is None
for performance. In v1 we returned the input image instead and performed an "erasing", by replacing the image with itself.
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.
See #7092 (comment) for a detailed explanation.
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.
Thanks Philip, I took a brief look at it
Instead of having a 3rd _get_params_internal()
function and 2 shallow wrappers, could we eliminate one of them?
i.e. just let _get_params()
do 99% of the work and massage what needs to be in the public get_params()
? This way when/if we remove get_params()
eventually, we won't have to change the other methods.
Of course we could also create a dummy object that represents As a final note, I don't know if we supported or enforced JIT scriptability on |
I did a little digging. vision/torchvision/transforms.py Lines 297 to 303 in 4390b55
We have similar behavior in v2, although not for At this point, JIT was non-existent and thus also not part of the design. That changed with #2292, which mostly dealt with unifying the PIL and tensor backend, but also required JIT scriptability. The original plan only comprised deterministic transforms, which makes sense. However, later on the list was extended to also include random transforms. JIT scriptability is useful for inference, but I can't think of a use case of using random transforms for that. Unfortunately, it is unclear from the context whether this was done to achieve full scriptability of the transforms API or if it was backed by an actual use case. cc @vfdev-5 @fmassa In any case, JIT scriptability of vision/test/test_transforms_tensor.py Lines 66 to 71 in c06d52b
vision/test/test_transforms_tensor.py Lines 143 to 144 in c06d52b
(This is just exemplary. You can find JIT tests for all other transforms as well in the same test module) In conclusion, JIT scriptability of I think we should tie the decision here to our decision for JIT scriptability in general: if we decide to support it (temporarily or permanently) after all, the same should apply to the temporary |
In 21946f7 I've added a design that uses a fake |
Thanks a lot for the deep dive Philip
I agree, let's postpone this decision until we're clearer about the JIT requirements |
One option that has slipped my mind so far: if we only want to support class MyTransformV2(transforms.Transform):
...
@staticmethod
def get_params():
return MyTransformV1.get_params() |
A naming nitpick: should this be better called |
Yes, probably, but we're only introducing those for BC - the goal is to be backward-compatible, so we have to keep the same name. |
Superseded by #7153. |
Addresses #7092 (comment). The core idea is the following:
get_params
and_get_params_internal
(name up for discussion)_get_params_internal
_get_params
andget_params
and wrangle it in the respective format therecc @vfdev-5 @datumbox @bjuncek