-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Added typing annotations to transforms/functional_pil #4234
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
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.
Little help would be nice! I'm not sure about few types.
Edit: CI appears green
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.
Hey @oke-aditya and sorry for the delay. I have some comments inline.
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.
I think I have fixed the bugs.
Should we unblock this file from mypy.ini ?
Edit: Nope. I think it's not yet all green.
We have stumbled upon mypy/#3186 issue because of which mypy still complains.
It thinks int
is not a number and hence we cannot assign 0
as default arg.
Edit2: A small workaround is to use
Optional[Union[int, float, List[int], List[float], Tuple[int, ...], Tuple[float, ...]]]
It sounds slightly more verbose. Probably you know a better a way 😄
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.
Just marking the lines where error occured
Hmmm tests seem to be failing on Also another point I noticed that. The functional tensor has similar padding code, and I think that it accepts But the typing seems to be only I think that the typing for Functional PIL and Functional Tensor should be same. Since that's how we test them as well. |
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.
We have stumbled upon mypy/#3186 issue because of which mypy still complains.
It thinksint
is not a number and hence we cannot assign0
as default arg.
Good catch. I've added a fix inline.
Hmmm tests seem to be failing on
.pad()
function.
Not very sure why it occured, but I think that has easy fix by passing args aspadding=pad
.
Fix inline.
The functional tensor has similar padding code, and I think that it accepts
int
as well as List and Tuples of them.
(Basically same asUnion[int, List[int], Tuple[int, ...]],
)But the typing seems to be only
List[int]
.I think that the typing for Functional PIL and Functional Tensor should be same. Since that's how we test them as well.
Yes the signatures and in turn the annotations should be the same. Would you send a follow-up PR after this one is merged?
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.
@oke-aditya I checked this again. Left a couple of comments to get your input. It's mainly around whether we want to align the API between PIL and Tensor or if we want to keep the two separate. No strong opinions here. @pmeier Let me also know what you think please.
I know this PR turned out to be lengthy so if you had enough we can address them later. Cheers! :)
def pad( | ||
img: Image.Image, | ||
padding: Union[int, List[int], Tuple[int, ...]], | ||
fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, |
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.
This is defined as int on the tensor equivalent method, though it supports both. Do we want them aligned?
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.
I think we want float in tensor equivalent method to align 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.
no strong opinion here. I leave it up to you.
def resize(img, size, interpolation=Image.BILINEAR, max_size=None): | ||
def resize( | ||
img: Image.Image, | ||
size: Union[Sequence[int], int], |
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.
The tensor API uses List instead of Sequence. Do we want them aligned?
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, I think we need to have Sequence in tensor, to align. But probably JIT does not support it.
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, torch.jit
does not support Sequence
. Since we also have a Union
here, we can't align them anyway. Thus, I think it is ok to go with Sequence
.
img: Image.Image, | ||
matrix: List[float], | ||
interpolation: int = 0, | ||
fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, |
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.
FYI strangely the tensor API defines this as float as well.
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 the tensor API defines it to be only List[float]
and not all the three, again something which isn't aligned or probably has an issue.
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.
It's the union that is not supported. I just highlight that here the type (float vs int) is actually aligned. Which is weird cause we don't seem to keep things aligned. I probably agree that it's the tensor that needs changing though.
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.
I just verified Sequence is not supported by JIT. And yes Union too is not supported.
https://pytorch.org/docs/stable/jit_language_reference.html#supported-type
def affine( | ||
img: Image.Image, | ||
matrix: List[float], | ||
interpolation: int = 0, |
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.
Perhaps we should use Image.NEAREST instead of the numeric here?
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.
Hmm, something stranger is that in the tensor API we pass a string.
I'm not sure how would we align it. (again probably JIT?)
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.
I mean something different. See the next comment (enum-style VS integer for the default 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.
def pad( | ||
img: Image.Image, | ||
padding: Union[int, List[int], Tuple[int, ...]], | ||
fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, |
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.
I think we want float in tensor equivalent method to align this.
def resize(img, size, interpolation=Image.BILINEAR, max_size=None): | ||
def resize( | ||
img: Image.Image, | ||
size: Union[Sequence[int], int], |
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, I think we need to have Sequence in tensor, to align. But probably JIT does not support it.
def affine( | ||
img: Image.Image, | ||
matrix: List[float], | ||
interpolation: int = 0, |
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.
Hmm, something stranger is that in the tensor API we pass a string.
I'm not sure how would we align it. (again probably JIT?)
img: Image.Image, | ||
matrix: List[float], | ||
interpolation: int = 0, | ||
fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0, |
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 the tensor API defines it to be only List[float]
and not all the three, again something which isn't aligned or probably has an issue.
I think this should be do it! @datumbox can you just cross check if I have kept the modes |
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.
LGTM, thank you @oke-aditya for your contribution and for your patience. :)
I checked the modes and they look fine to me.
@pmeier Do you mind also having a look that we are good to go? Once I get your stamp we can merge.
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.
can you just cross check if I have kept the modes
BILINEAR
CUBIC
andNEAREST
correct as per old definitions?
Seems to be good, even replacing an old 0
with NEAREST
👍
LGTM, thanks a lot @oke-aditya!
def resize(img, size, interpolation=Image.BILINEAR, max_size=None): | ||
def resize( | ||
img: Image.Image, | ||
size: Union[Sequence[int], int], |
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, torch.jit
does not support Sequence
. Since we also have a Union
here, we can't align them anyway. Thus, I think it is ok to go with Sequence
.
Hey @datumbox! You merged this PR, but no labels were added. |
Summary: * fix * add functional PIL typings * fix types * fix types * fix a small one * small fix * fix type * fix interpolation types Reviewed By: NicolasHug Differential Revision: D30417195 fbshipit-source-id: 5e09a14011e5cca76d87c2a3dfc2872303f40b2c Co-authored-by: Vasilis Vryniotis <[email protected]>
Following up on #2025, this PR adds missing typing annotations in functional_pil.py
Any feedback is welcome!
cc @frgfm @pmeier