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

use_pycocotools_for_segment_mask #8640

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
22 changes: 2 additions & 20 deletions torchvision/tv_tensors/_dataset_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,6 @@ def coco_dectection_wrapper_factory(dataset, target_keys):
default={"image_id", "boxes", "labels"},
)

def segmentation_to_mask(segmentation, *, canvas_size):
from pycocotools import mask

if isinstance(segmentation, dict):
# if counts is a string, it is already an encoded RLE mask
if not isinstance(segmentation["counts"], str):
segmentation = mask.frPyObjects(segmentation, *canvas_size)
elif isinstance(segmentation, list):
segmentation = mask.merge(mask.frPyObjects(segmentation, *canvas_size))
else:
raise ValueError(f"COCO segmentation expected to be a dict or a list, got {type(segmentation)}")
return torch.from_numpy(mask.decode(segmentation))

def wrapper(idx, sample):
image_id = dataset.ids[idx]

Expand All @@ -394,15 +381,10 @@ def wrapper(idx, sample):
),
new_format=tv_tensors.BoundingBoxFormat.XYXY,
)

coco_ann = dataset.coco.imgToAnns[image_id]
if "masks" in target_keys:
target["masks"] = tv_tensors.Mask(
torch.stack(
[
segmentation_to_mask(segmentation, canvas_size=canvas_size)
for segmentation in batched_target["segmentation"]
]
),
torch.stack([torch.from_numpy(dataset.coco.annToMask(ann)) for ann in coco_ann]),
)

if "labels" in target_keys:
Expand Down
Loading