Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Support Binary Mask with transparent SementationMask interface (#473)
Browse files Browse the repository at this point in the history
* support RLE and binary mask

* do not convert to numpy

* be consistent with Detectron

* delete wrong comment

* [WIP] add tests for segmentation_mask

* update tests

* minor change

* Refactored segmentation_mask.py

* Add unit test for segmentation_mask.py

* Add RLE support for BinaryMaskList

* PEP8 black formatting

* Minor patch

* Use internal  that handles 0 channels

* Fix polygon slicing
  • Loading branch information
botcs authored and fmassa committed Apr 9, 2019
1 parent f917a55 commit b4d5465
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 86 deletions.
2 changes: 1 addition & 1 deletion maskrcnn_benchmark/data/datasets/coco.py
Expand Up @@ -80,7 +80,7 @@ def __getitem__(self, idx):
target.add_field("labels", classes)

masks = [obj["segmentation"] for obj in anno]
masks = SegmentationMask(masks, img.size)
masks = SegmentationMask(masks, img.size, mode='poly')
target.add_field("masks", masks)

if anno and "keypoints" in anno[0]:
Expand Down
10 changes: 4 additions & 6 deletions maskrcnn_benchmark/modeling/roi_heads/mask_head/loss.py
Expand Up @@ -27,17 +27,15 @@ def project_masks_on_boxes(segmentation_masks, proposals, discretization_size):
assert segmentation_masks.size == proposals.size, "{}, {}".format(
segmentation_masks, proposals
)
# TODO put the proposals on the CPU, as the representation for the
# masks is not efficient GPU-wise (possibly several small tensors for
# representing a single instance mask)

# FIXME: CPU computation bottleneck, this should be parallelized
proposals = proposals.bbox.to(torch.device("cpu"))
for segmentation_mask, proposal in zip(segmentation_masks, proposals):
# crop the masks, resize them to the desired resolution and
# then convert them to the tensor representation,
# instead of the list representation that was used
# then convert them to the tensor representation.
cropped_mask = segmentation_mask.crop(proposal)
scaled_mask = cropped_mask.resize((M, M))
mask = scaled_mask.convert(mode="mask")
mask = scaled_mask.get_mask_tensor()
masks.append(mask)
if len(masks) == 0:
return torch.empty(0, dtype=torch.float32, device=device)
Expand Down

2 comments on commit b4d5465

@zimenglan-sysu-512
Copy link
Contributor

Choose a reason for hiding this comment

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

hi @botcs
will this modification slow down the speed for training and testing?

@botcs
Copy link
Contributor Author

@botcs botcs commented on b4d5465 Apr 18, 2019

Choose a reason for hiding this comment

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

Hi @zimenglan-sysu-512
My short answer is: no, it will not slow down training.

If you have managed to feed polygons before into your training script than it will be completely the same (apart from the few assertions for sanity checking the input and operations on your polygons but again, if your original polygon script was working this will work as well).

In general it depends on how are you going to use it: if you are using binary mask representation, than probably the training speed will decrease significantly because in the segmentation loss all the instances are cropped and resized sequentially. You can use the updated interface to convert masks to polygons but be careful because it may distort the mask.

Please sign in to comment.