-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Conversation
I think that we should generate a color palette for |
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.
Thanks @oke-aditya , this looks good, I made a few comments below
torchvision/utils.py
Outdated
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] |
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.
In order to preserve git blame I would suggest to revert these changes, as I don't think they significantly improve readability either
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.
Problem is mypy. To satisfy mypy I had to change the name.
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.
what is mypy complaining about?
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.
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.
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.
Ok, let's leave it as-is then.
@pmeier what is your take on 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.
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.
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.
done #4531
torchvision/utils.py
Outdated
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 |
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.
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
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.
Thanks @oke-aditya , LGTM with a last comment
Co-authored-by: Nicolas Hug <[email protected]>
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.
thanks, I'll merge when green
@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! |
Small docstring fix
@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? |
Yeah works fine 😃 |
Done! Thanks @oke-aditya |
Hey @NicolasHug! You merged this PR, but no labels were added. |
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]>
* 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]>
* 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]>
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