-
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
Unified Pad and F.pad opertion for PIL and Tensor inputs #2345
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.
Looks great, thanks a lot!
I have some minor comments, could you open a new issue so that we can track supporting other types of padding_mode
in the future for Tensor?
|
||
Args: | ||
img (Tensor): Image to be padded. | ||
padding (int or tuple or list): Padding on each border. If a single int is provided this |
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.
doc should be updated, as we only support List[int]
in torchscript
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.
Actually, we can support both: tuple and list as tuple is casted to list below (L361). Annotation is kept as List
for torchscript to pass. In the tests test_pad
I check both types: [4, 4]
and (2, 2, 2, 2)
.
Do you want me add some note about that ?
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.
yeah, I think we should be careful with how we describe our support.
If we say we support tuple
but it fails in torchscript, that's a bad user experience.
I would say it might be better to say we only support List
, and as a bonus we support Tuple
in non-torchscript mode.
Thoughts?
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, we need to provide correct information about what is supported and when. In case of pad in torchscript or non-torchscipt mode, both types (tuple, list) are supported :
>>> from torchvision.transforms.functional_tensor import pad
>>> import torch
>>> tensor = torch.randint(0, 255, (3, 10, 12), dtype=torch.uint8)
>>> r1 = pad(tensor, (2, 2), fill=0, padding_mode="constant")
>>> r2 = pad(tensor, [2, 2], fill=0, padding_mode="constant")
>>> r1.eq(r2).all()
tensor(True)
>>> script_pad = torch.jit.script(pad)
>>> r1 = script_pad(tensor, (2, 2), fill=0, padding_mode="constant")
>>> r2 = script_pad(tensor, [2, 2], fill=0, padding_mode="constant")
>>> r1.eq(r2).all()
tensor(True)
Anyway, I'll update the docstring to make it crystal clear :)
@fmassa I updated 3 of 4 comments you mentioned.
It is for for another PR, right ? |
Yes, doing it in another PR is fine, but it would be good to have an issue open to track it down so that we don't forget (and if you could work on it it would be even better :-)) |
Yep, done here : #2350 |
- added compatibility support for padding as [value, ] for functional_pil.pad
@fmassa I updated docstring of F.pad(x, padding=[1, ])
F_pil.pad(x, padding=[1, ])
F_t.pad(x, padding=[1, ])
fn = torch.jit.script(F.pad)
fn(x, padding=[1, ]) without checking if x is PIL Image or Tensor. |
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 great, thanks a lot @vfdev-5 !
* [WIP] Add Tensor implementation for pad * Unified Pad and F.pad opertion for PIL and Tensor inputs * Added another test and improved docstring * Updates according to the review * Cosmetics and replaced f-string by "".format * Updated docstring - added compatibility support for padding as [value, ] for functional_pil.pad Co-authored-by: Francisco Massa <fvsmassa@gmail.com>
Addresses #2292
Description:
Pad
andF.pad
methods to accept PIL Image and torch Tensors.F.pad
acceptspadding
as tuple or list and is casted internally to required type.