Skip to content

Fix annotation of draw_segmentation_masks #4527

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 16 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def test_draw_invalid_boxes():

@pytest.mark.parametrize('colors', [
None,
'blue',
'#FF00FF',
(1, 34, 122),
['red', 'blue'],
['#FF00FF', (1, 34, 122)],
])
Expand Down Expand Up @@ -196,6 +199,8 @@ def test_draw_segmentation_masks(colors, alpha):

if colors is None:
colors = utils._generate_color_palette(num_masks)
elif isinstance(colors, str) or isinstance(colors, tuple):
colors = [colors]

# Make sure each mask draws with its own color
for mask, color in zip(masks, colors):
Expand Down
25 changes: 12 additions & 13 deletions torchvision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def draw_bounding_boxes(
the boxes are absolute coordinates with respect to the image. In other words: `0 <= xmin < xmax < W` and
`0 <= ymin < ymax < H`.
labels (List[str]): List containing the labels of bounding boxes.
colors (Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]): List containing the colors
or a single color for all of the bounding boxes. The colors can be represented as `str` or
`Tuple[int, int, int]`.
colors: (color or list of colors, optional): List containing the colors
of the boxes or single color for all boxes. The color can be represented as
PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``.
fill (bool): If `True` fills the bounding box with specified color.
width (int): Width of bounding box.
font (str): A filename containing a TrueType font. If the file is not found in this filename, the loader may
Expand Down Expand Up @@ -230,7 +230,7 @@ def draw_segmentation_masks(
image: torch.Tensor,
masks: torch.Tensor,
alpha: float = 0.8,
colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None,
colors: Optional[Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]] = None,
) -> torch.Tensor:

"""
Expand All @@ -242,10 +242,10 @@ def draw_segmentation_masks(
masks (Tensor): Tensor of shape (num_masks, H, W) or (H, W) and dtype bool.
alpha (float): Float number between 0 and 1 denoting the transparency of the masks.
0 means full transparency, 1 means no transparency.
colors (list or None): List containing the colors of the 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
with one element. By default, random colors are generated for each mask.
colors: (color or list of colors, optional): List containing the colors
of the masks or single color for all masks. The color can be represented as
PIL strings e.g. "red" or "#FF00FF", or as RGB tuples e.g. ``(240, 10, 157)``.
By default, random colors are generated for each mask.

Returns:
img (Tensor[C, H, W]): Image Tensor, with segmentation masks drawn on top.
Expand Down Expand Up @@ -288,18 +288,17 @@ def draw_segmentation_masks(
for color in colors:
if isinstance(color, str):
color = ImageColor.getrgb(color)
color = torch.tensor(color, dtype=out_dtype)
colors_.append(color)
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


out = image * (1 - alpha) + img_to_draw * alpha
return out.to(out_dtype)


def _generate_color_palette(num_masks):
def _generate_color_palette(num_masks: int):
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
return [tuple((i * palette) % 255) for i in range(num_masks)]