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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster implementation of torchvision.ops.boxes::masks_to_boxes #8194

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions torchvision/ops/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,15 @@ def masks_to_boxes(masks: torch.Tensor) -> torch.Tensor:
if masks.numel() == 0:
return torch.zeros((0, 4), device=masks.device, dtype=torch.float)

n = masks.shape[0]
non_zero_ys = torch.any(masks, axis=1).float()
non_zero_xs = torch.any(masks, axis=2).float()

bounding_boxes = torch.zeros((n, 4), device=masks.device, dtype=torch.float)
y1 = non_zero_ys.argmax(dim=1)
x1 = non_zero_xs.argmax(dim=1)

for index, mask in enumerate(masks):
y, x = torch.where(mask != 0)
y2 = (masks.shape[1] - 1) - non_zero_ys.flip(dims=[1]).argmax(dim=1)
x2 = (masks.shape[2] - 1) - non_zero_xs.flip(dims=[1]).argmax(dim=1)

bounding_boxes[index, 0] = torch.min(x)
bounding_boxes[index, 1] = torch.min(y)
bounding_boxes[index, 2] = torch.max(x)
bounding_boxes[index, 3] = torch.max(y)
bounding_boxes = torch.stack((x1, y1, x2, y2), dim=1).float()

return bounding_boxes