Skip to content

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

Merged
merged 21 commits into from
Aug 18, 2021

Conversation

oke-aditya
Copy link
Contributor

@oke-aditya oke-aditya commented Jul 30, 2021

Following up on #2025, this PR adds missing typing annotations in functional_pil.py

Any feedback is welcome!

cc @frgfm @pmeier

Copy link
Contributor Author

@oke-aditya oke-aditya left a 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

@frgfm frgfm mentioned this pull request Jul 31, 2021
Copy link
Collaborator

@pmeier pmeier left a 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.

Copy link
Contributor Author

@oke-aditya oke-aditya left a 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 😄

@oke-aditya oke-aditya requested review from datumbox and pmeier August 16, 2021 19:06
Copy link
Contributor Author

@oke-aditya oke-aditya left a 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

@oke-aditya
Copy link
Contributor Author

oke-aditya commented Aug 16, 2021

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 as padding=pad.

Also another point I noticed that.

The functional tensor has similar padding code, and I think that it accepts int as well as List and Tuples of them.
(Basically same as Union[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.

Copy link
Collaborator

@pmeier pmeier left a 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 thinks int is not a number and hence we cannot assign 0 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 as padding=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 as Union[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?

@oke-aditya
Copy link
Contributor Author

oke-aditya commented Aug 17, 2021

@datumbox @pmeier
I think so CI is green enough to merge this. Probably all the failures are unrelated.

Hopefully, Good to go!

Edit:

Leaving leaving your comment as a new issue.

Copy link
Contributor

@datumbox datumbox left a 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,
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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],
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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,
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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,
Copy link
Contributor

@datumbox datumbox Aug 17, 2021

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?

Copy link
Contributor Author

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?)

Copy link
Contributor

@datumbox datumbox Aug 17, 2021

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).

Copy link
Contributor Author

@oke-aditya oke-aditya left a comment

Choose a reason for hiding this comment

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

Hey @datumbox ! Here are my thoughts.

There are probably multiple places where we have misaligned with functional_tensor.

My idea was to follow-up this with a new PR. (#4282 ).

Let me know what you think!

def pad(
img: Image.Image,
padding: Union[int, List[int], Tuple[int, ...]],
fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0,
Copy link
Contributor Author

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],
Copy link
Contributor Author

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,
Copy link
Contributor Author

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,
Copy link
Contributor Author

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.

@oke-aditya oke-aditya requested a review from datumbox August 17, 2021 17:27
@oke-aditya
Copy link
Contributor Author

I think this should be do it! @datumbox can you just cross check if I have kept the modes BILINEAR CUBIC and NEAREST correct as per old definitions?

Copy link
Contributor

@datumbox datumbox left a 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.

Copy link
Collaborator

@pmeier pmeier left a 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 and NEAREST 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],
Copy link
Collaborator

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.

@datumbox datumbox merged commit 759c5b6 into pytorch:master Aug 18, 2021
@github-actions
Copy link

Hey @datumbox!

You merged this PR, but no labels were added.

facebook-github-bot pushed a commit that referenced this pull request Aug 20, 2021
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]>
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.

4 participants