Skip to content
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

Fix annotation of draw_segmentation_masks #4527

Merged
merged 16 commits into from
Oct 5, 2021

Conversation

oke-aditya
Copy link
Contributor

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

draw_segmentation_masks also supports passing single str containing color name or single str containing color hex or tuple containing RGB values as all colors for masks.

A simple example is our gallery 😃

https://pytorch.org/vision/master/auto_examples/plot_repurposing_annotations.html#sphx-glr-auto-examples-plot-repurposing-annotations-py

I have updated the tests too which all check all the above cases.

cc @NicolasHug

@oke-aditya
Copy link
Contributor Author

oke-aditya commented Oct 2, 2021

I think that we should generate a color palette for draw_bounding_boxes too instead of drawing default white color.
If people really want white color boxes (yuk!) they can pass color ="white"

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @oke-aditya , this looks good, I made a few comments below

test/test_utils.py Outdated Show resolved Hide resolved
Comment on lines 292 to 297
colors_.append(torch.tensor(color, dtype=out_dtype))

img_to_draw = image.detach().clone()
# TODO: There might be a way to vectorize this
for mask, color in zip(masks, colors_):
img_to_draw[:, mask] = color[:, None]
for mask, color_tensor in zip(masks, colors_):
img_to_draw[:, mask] = color_tensor[:, None]
Copy link
Member

Choose a reason for hiding this comment

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

In order to preserve git blame I would suggest to revert these changes, as I don't think they significantly improve readability either

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Problem is mypy. To satisfy mypy I had to change the name.

Copy link
Member

Choose a reason for hiding this comment

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

what is mypy complaining about?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mypy complains that color isn't a tensor, as we are re-using the name color.
Here is the exact error.

torchvision/utils.py:292: error: Incompatible types in assignment (expression
has type "Tensor", variable has type "Union[str, Tuple[int, int, int]]") 
[assignment]
            color = torch.tensor(color, dtype=out_dtype)
                    ^
torchvision/utils.py:298: error: Invalid index type "Tuple[slice, None]" for
"Union[str, Tuple[int, int, int]]"; expected type "Union[int, slice]"  [index]
            img_to_draw[:, mask] = color[:, None]
                                   ^
torchvision/utils.py:298: error: Invalid tuple index type (actual type
"Tuple[slice, None]", expected type "Union[int, slice]")  [misc]
            img_to_draw[:, mask] = color[:, None]
                                   ^
torchvision/utils.py:298: error: Incompatible types in assignment (expression
has type "Union[str, Any]", target has type
"Union[Tensor, Union[int, float, bool]]")  [assignment]
            img_to_draw[:, mask] = color[:, None]
                                   ^
Found 4 errors in 1 file (checked 127 source files)

Exited with code exit status 1

The easiest fix was to rename, rather than ignoring mypy errors.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's leave it as-is then.

@pmeier what is your take on this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Given that this is one of the most frustrating things when working with mypy, we could simply put allow_redefinition = True in our mypy config. I also proposed doing this in #4513, albeit only for the prototype datasets.

Copy link
Member

@NicolasHug NicolasHug Oct 4, 2021

Choose a reason for hiding this comment

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

done #4531

be represented as PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``.
colors: Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]: List containing the colors
of the masks or single color for all masks. The colors can be represented as
PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``.
When ``masks`` has a single entry of shape (H, W), you can pass a single color instead of a list
Copy link
Member

Choose a reason for hiding this comment

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

let's just remove this sentence below then, it doesn't add much to the description now

When masks has a single entry of shape (H, W), you can pass a single color instead of a list with one element

torchvision/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @oke-aditya , LGTM with a last comment

torchvision/utils.py Outdated Show resolved Hide resolved
torchvision/utils.py Outdated Show resolved Hide resolved
@oke-aditya oke-aditya requested a review from NicolasHug October 4, 2021 12:18
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

thanks, I'll merge when green

@NicolasHug
Copy link
Member

@oke-aditya we just started formatting the code with black - you'll get a few merge conflicts as you already noticed. Don't worry about it, I'll take care of it.

we'll soon publish proper instructions on how to merge these conflicts, but until then feel free to ignore them :)

sorry for the unexpected conflicts!

torchvision/utils.py Outdated Show resolved Hide resolved
@NicolasHug
Copy link
Member

@oke-aditya if that's OK I'll merge #4531 first so we can remove the renaming from this PR, and then I'll merge it. Sounds good?

torchvision/utils.py Outdated Show resolved Hide resolved
@oke-aditya
Copy link
Contributor Author

Yeah works fine 😃

@NicolasHug NicolasHug merged commit a75dc89 into pytorch:main Oct 5, 2021
@NicolasHug
Copy link
Member

Done! Thanks @oke-aditya

@oke-aditya oke-aditya deleted the fix_annot branch October 5, 2021 14:59
@github-actions
Copy link

github-actions bot commented Oct 5, 2021

Hey @NicolasHug!

You merged this PR, but no labels were added.

facebook-github-bot pushed a commit that referenced this pull request Oct 8, 2021
Summary:
* Add str param

* Update test to include str

* Fix mypy

* Remove a small bracket

* Test more robustly

* Update docstring and test:

* Apply suggestions from code review

* Update torchvision/utils.py

Small docstring fix

* Update torchvision/utils.py

* remove unnecessary renaming

Reviewed By: NicolasHug

Differential Revision: D31505551

fbshipit-source-id: 459123a3774b39ee071f43ca2b196bd50319a60a

Co-authored-by: Nicolas Hug <[email protected]>
Co-authored-by: Nicolas Hug <[email protected]>
Co-authored-by: Nicolas Hug <[email protected]>
mszhanyi pushed a commit to mszhanyi/vision that referenced this pull request Oct 19, 2021
* Add str param

* Update test to include str

* Fix mypy

* Remove a small bracket

* Test more robustly

* Update docstring and test:

* Apply suggestions from code review

Co-authored-by: Nicolas Hug <[email protected]>

* Update torchvision/utils.py

Small docstring fix

* Update torchvision/utils.py

* remove unnecessary renaming

Co-authored-by: Nicolas Hug <[email protected]>
Co-authored-by: Nicolas Hug <[email protected]>
cyyever pushed a commit to cyyever/vision that referenced this pull request Nov 16, 2021
* Add str param

* Update test to include str

* Fix mypy

* Remove a small bracket

* Test more robustly

* Update docstring and test:

* Apply suggestions from code review

Co-authored-by: Nicolas Hug <[email protected]>

* Update torchvision/utils.py

Small docstring fix

* Update torchvision/utils.py

* remove unnecessary renaming

Co-authored-by: Nicolas Hug <[email protected]>
Co-authored-by: Nicolas Hug <[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