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

Add GPU support for draw_segmentation_masks #7684

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 14 additions & 11 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import torch
import torchvision.transforms.functional as F
import torchvision.utils as utils
from common_utils import assert_equal
from common_utils import assert_equal, cpu_and_cuda
from PIL import __version__ as PILLOW_VERSION, Image, ImageColor


Expand Down Expand Up @@ -203,12 +203,13 @@ def test_draw_no_boxes():
],
)
@pytest.mark.parametrize("alpha", (0, 0.5, 0.7, 1))
def test_draw_segmentation_masks(colors, alpha):
@pytest.mark.parametrize("device", cpu_and_cuda())
def test_draw_segmentation_masks(colors, alpha, device):
"""This test makes sure that masks draw their corresponding color where they should"""
num_masks, h, w = 2, 100, 100
dtype = torch.uint8
img = torch.randint(0, 256, size=(3, h, w), dtype=dtype)
masks = torch.randint(0, 2, (num_masks, h, w), dtype=torch.bool)
img = torch.randint(0, 256, size=(3, h, w), dtype=dtype, device=device)
masks = torch.randint(0, 2, (num_masks, h, w), dtype=torch.bool, device=device)

# For testing we enforce that there's no overlap between the masks. The
# current behaviour is that the last mask's color will take priority when
Expand All @@ -234,7 +235,7 @@ def test_draw_segmentation_masks(colors, alpha):
for mask, color in zip(masks, colors):
if isinstance(color, str):
color = ImageColor.getrgb(color)
color = torch.tensor(color, dtype=dtype)
color = torch.tensor(color, dtype=dtype, device=device)

if alpha == 1:
assert (out[:, mask] == color[:, None]).all()
Expand All @@ -245,11 +246,12 @@ def test_draw_segmentation_masks(colors, alpha):
torch.testing.assert_close(out[:, mask], interpolated_color, rtol=0.0, atol=1.0)


def test_draw_segmentation_masks_errors():
@pytest.mark.parametrize("device", cpu_and_cuda())
def test_draw_segmentation_masks_errors(device):
Copy link
Member

Choose a reason for hiding this comment

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

I'll leave this here to avoid churn but we typically don't need to run those tests on cuda, as there's nothing cuda-spcecific there. Same for test_draw_no_segmention_mask

h, w = 10, 10

masks = torch.randint(0, 2, size=(h, w), dtype=torch.bool)
img = torch.randint(0, 256, size=(3, h, w), dtype=torch.uint8)
masks = torch.randint(0, 2, size=(h, w), dtype=torch.bool, device=device)
img = torch.randint(0, 256, size=(3, h, w), dtype=torch.uint8, device=device)

with pytest.raises(TypeError, match="The image must be a tensor"):
utils.draw_segmentation_masks(image="Not A Tensor Image", masks=masks)
Expand Down Expand Up @@ -281,9 +283,10 @@ def test_draw_segmentation_masks_errors():
utils.draw_segmentation_masks(image=img, masks=masks, colors=bad_colors)


def test_draw_no_segmention_mask():
img = torch.full((3, 100, 100), 0, dtype=torch.uint8)
masks = torch.full((0, 100, 100), 0, dtype=torch.bool)
@pytest.mark.parametrize("device", cpu_and_cuda())
def test_draw_no_segmention_mask(device):
img = torch.full((3, 100, 100), 0, dtype=torch.uint8, device=device)
masks = torch.full((0, 100, 100), 0, dtype=torch.bool, device=device)
with pytest.warns(UserWarning, match=re.escape("masks doesn't contain any mask. No mask was drawn")):
res = utils.draw_segmentation_masks(img, masks)
# Check that the function didn't change the image
Expand Down
2 changes: 1 addition & 1 deletion torchvision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def draw_segmentation_masks(
return image

out_dtype = torch.uint8
colors = [torch.tensor(color, dtype=out_dtype) for color in _parse_colors(colors, num_objects=num_masks)]
colors = [torch.tensor(color, dtype=out_dtype, device=image.device) for color in _parse_colors(colors, num_objects=num_masks)]

img_to_draw = image.detach().clone()
# TODO: There might be a way to vectorize this
Expand Down