Skip to content

Add typing Annotations to detection/utils #4583

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 12 commits into from
Oct 12, 2021
Merged

Conversation

oke-aditya
Copy link
Contributor

@oke-aditya oke-aditya commented Oct 10, 2021

Let's begin #4582

@khushi-411 do you want to take this up? I think you can push to this branch on my fork.
You can clone my forked repo and push changes. They should reflect here.

cc @datumbox

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.

The PR isn't complete. A small headstart for @khushi-411 😃

@khushi-411
Copy link
Contributor

Definitely @oke-aditya, I'd love to start up with this PR! The explanations you provided are really helpful.
I'll ping you once it is complete or if I have some doubts.

@oke-aditya
Copy link
Contributor Author

It worked. Your commit appeared here.

@khushi-411
Copy link
Contributor

Yep, Cheers!

@oke-aditya
Copy link
Contributor Author

@khushi-411 you would need to use pre-commit hooks to format your code.

See the contributing guide

https://github.com/pytorch/vision/blob/main/CONTRIBUTING.md#pre-commit-hooks

It will automatically format your code to pass python lint CI.

I will have a look at PR before end of day, don't worry it's very natural to get a Red from CI, little bit of debugging can fix it.

@@ -290,7 +285,7 @@ def __call__(self, match_quality_matrix):
if self.allow_low_quality_matches:
all_matches = matches.clone()
else:
all_matches = None
all_matches = None # type: ignore[assignment]
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 should ignore this error, as we are directly assigning None to a Tensor object.

Copy link
Contributor

@khushi-411 khushi-411 Oct 11, 2021

Choose a reason for hiding this comment

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

Ah, I was stuck at this point. Is there any other method of adding types here?
I read one issue (#40867). That's quite relatable.

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'm not sure if we have a better way as we are assigning all_matches on the fly to be either Tensor or None.
Where could we declare all_matches to be Optional[Tensor] ?

Previously too we had to use such workaround see https://github.com/pytorch/vision/blob/main/torchvision/models/quantization/googlenet.py#L80

cc @pmeier

Copy link
Collaborator

Choose a reason for hiding this comment

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

If Optional[Tensor] is possible, than we should definitively go for that. Otherwise probably Tensor and ignore the error 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.

Let me have a quick try at that. I will get back in an hour.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't work well, see the below JIT error.

@oke-aditya oke-aditya marked this pull request as ready for review October 11, 2021 19:17
@oke-aditya
Copy link
Contributor Author

Apart from above comment everything looks good.

cc @pmeier @NicolasHug

@datumbox
Copy link
Contributor

Had a look and the first impression is that all types are correct. I can have a look again if you want tomorrow.

@khushi-411
Copy link
Contributor

Yes definitely, thanks a lot @datumbox!

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.

LGTM after #4583 (comment) is resolved and CI is green. Thanks a lot @oke-aditya and @khushi-411!

@khushi-411
Copy link
Contributor

Thanks a lot @pmeier!

@oke-aditya
Copy link
Contributor Author

oke-aditya commented Oct 11, 2021

Mypy was happy locally Hopefully CI will be all green.

Edit: I'm amazed that people in 3 different time zones synced so fast. Hats off to @pmeier @datumbox @khushi-411 !

@oke-aditya
Copy link
Contributor Author

oke-aditya commented Oct 11, 2021

Ok so mpy was happy and JIT wasn't.

https://app.circleci.com/pipelines/github/pytorch/vision/11222/workflows/bf1a9029-353d-4d55-ad61-2c8f98a2b943/jobs/866525

torch.jit.frontend.UnsupportedNodeError: annotated assignments without assigned value aren't supported:
  File "/root/project/torchvision/models/detection/_utils.py", line 285
        # Max over gt elements (dim 0) to find best gt candidate for each prediction
        matched_vals, matches = match_quality_matrix.max(dim=0)
        all_matches: Optional[Tensor]
        ~ <--- HERE
        if self.allow_low_quality_matches:
            all_matches = matches.clone()

So we have no choice but to ignore ☹️

@oke-aditya
Copy link
Contributor Author

This should be good to go now @datumbox @pmeier 😃

@pmeier
Copy link
Collaborator

pmeier commented Oct 11, 2021

I'm amazed that people in 3 different time zones synced so fast.

Well for Vasilis and me the time is at least somewhat reasonable whereas for you folks is the middle of the night 😛

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, thanks!

@datumbox datumbox merged commit c790216 into pytorch:main Oct 12, 2021
@oke-aditya oke-aditya deleted the type_utils branch October 12, 2021 10:39
facebook-github-bot pushed a commit that referenced this pull request Oct 14, 2021
Summary:
* Start annotating utils

* checking

* Add annotations at _utils.py

* Remove unnecessary comments.

* re-checked typings

* Update typing

* Ignore small error

* Use optional tensor

* Ignore for JIT

Reviewed By: fmassa

Differential Revision: D31649971

fbshipit-source-id: 687cd2f5511b0d8cad015b74b60f3af0c108c2b0

Co-authored-by: Khushi Agrawal <[email protected]>
Co-authored-by: Vasilis Vryniotis <[email protected]>
mszhanyi pushed a commit to mszhanyi/vision that referenced this pull request Oct 19, 2021
* Start annotating utils

* checking

* Add annotations at _utils.py

* Remove unnecessary comments.

* re-checked typings

* Update typing

* Ignore small error

* Use optional tensor

* Ignore for JIT

Co-authored-by: Khushi Agrawal <[email protected]>
Co-authored-by: Vasilis Vryniotis <[email protected]>
cyyever pushed a commit to cyyever/vision that referenced this pull request Nov 16, 2021
* Start annotating utils

* checking

* Add annotations at _utils.py

* Remove unnecessary comments.

* re-checked typings

* Update typing

* Ignore small error

* Use optional tensor

* Ignore for JIT

Co-authored-by: Khushi Agrawal <[email protected]>
Co-authored-by: Vasilis Vryniotis <[email protected]>
@frgfm frgfm mentioned this pull request Oct 27, 2022
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