Skip to content

Add typing annotations to detection/generalized_rcnn #4631

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 7 commits into from
Nov 2, 2021

Conversation

oke-aditya
Copy link
Contributor

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

Helps #4582

This is easier to tackle. I think this should be good to go with less friction.

cc @datumbox

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

torchvision/models/detection/generalized_rcnn.py:106: error: "Tensor" not
callable  [operator]
            detections = self.transform.postprocess(detections, images.ima..

This is the only mypy error. I think it should be safe to ignore.

@oke-aditya
Copy link
Contributor Author

Hi @datumbox @NicolasHug can you guys have a review? I think this should be good to go.

@oke-aditya
Copy link
Contributor Author

A Friendly ping to @NicolasHug and @datumbox for a review :)

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!

@@ -97,7 +104,7 @@ def forward(self, images, targets=None):
features = OrderedDict([("0", features)])
proposals, proposal_losses = self.rpn(images, features, targets)
detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes)
detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes) # type: ignore[operator]
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, what's up with that warning?

Copy link
Contributor Author

@oke-aditya oke-aditya Nov 2, 2021

Choose a reason for hiding this comment

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

Here is the error.

torchvision/models/detection/generalized_rcnn.py:106: error: "Tensor" not
callable  [operator]
            detections = self.transform.postprocess(detections, images.ima...
                         ^
Found 1 error in 1 file (checked 143 source files)

Exited with code exit status 1

We pass transform as nn.Module to generalized_rcnn. While calling this function we use the GeneralizedRCNNTransform.

TheGeneralizedRCNNTransform class in other file has a method called postprocess but generalized_rcnn does not know that. So it thinks it's a Tensor and not a function, hence the error.

I don't know easy way to fix this, maybe we should type transform as GeneralizedRCNNTransform ?

@datumbox datumbox merged commit 5906d59 into pytorch:main Nov 2, 2021
@github-actions
Copy link

github-actions bot commented Nov 2, 2021

Hey @datumbox!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

self,
images: List[Tensor],
targets: Optional[List[Dict[str, Tensor]]] = None,
) -> Union[Tuple[Dict[str, Tensor], List[Dict[str, Tensor]]], Dict[str, Tensor], List[Dict[str, Tensor]]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

@datumbox This change causes failure in internal tests, check D32216673.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI
This change was done as Union is supported by Torchscript. There isn't a CI failure on main branch. (Probably you are referring to fbcode)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah.

RuntimeError: 
Union[Tuple[Dict[str, Tensor], List[Dict[str, Tensor]]], List[Dict[str, Tensor]], Dict[str, Tensor]] cannot be used as a tuple:
  File "/data/sandcastle/boxes/eden-trunk-hg-fbcode-fbsource/fbcode/buck-out/dev/gen/smart/smart_catalog_filter/inference/handler_test#binary,link-tree/smart_catalog_filters/embedding.py", line 144
    def forward(self, images: List[Tensor]):
        _, results = self.rcnn(images)
                     ~~~~~~~~~~~~~~~~ <--- HERE
        return [
            (self.get_emb(result["features"]), result["boxes"]) for result in results


----------------------------------------------------------------------

Copy link
Contributor

Choose a reason for hiding this comment

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

@prabhat00155 Thanks for flagging, I see it. This is something we should keep in mind for future Annotations and PRs.

Union of a tuple with a non-tuple is likely to break code because of expressions like:
a, b = result

My proposal is to amend the diff on FBcode to either remove the annotation or remove the union (whichever passes all tests) and then sync the change that you will do on FBcode to GH on a cherrypick OR on a new PR (Which ever easier).

Let me know your thoughts on the above and if I can do something to help.

Copy link
Contributor

@prabhat00155 prabhat00155 Nov 8, 2021

Choose a reason for hiding this comment

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

Thanks, I'll amend the diff(removing the type annotation).

facebook-github-bot pushed a commit that referenced this pull request Nov 8, 2021
Summary:
* Update typing

* Fix bug

* Unblock mypy

* Ignore small error

Reviewed By: kazhang

Differential Revision: D32216673

fbshipit-source-id: cc73b84b830c1e0c31502a2cb6cd7408a899d6ba

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

* Fix bug

* Unblock mypy

* Ignore small error

Co-authored-by: Vasilis Vryniotis <[email protected]>
@mpearce25 mpearce25 mentioned this pull request Feb 11, 2023
2 tasks
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