Skip to content

Commit

Permalink
support empty BinaryMaskList initialization (facebookresearch#780)
Browse files Browse the repository at this point in the history
* fix the bug in segmentation_mask when initializing BinaryMaskList with an empty list

* Add empty binary mask handling

* Strict indexing in BinaryMaskList - raise error for empty indexing
  • Loading branch information
botcs authored and fmassa committed May 24, 2019
1 parent 7a9b185 commit cd63feb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions maskrcnn_benchmark/structures/segmentation_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def __init__(self, masks, size):
# The raw data representation is passed as argument
masks = masks.clone()
elif isinstance(masks, (list, tuple)):
if isinstance(masks[0], torch.Tensor):
if len(masks) == 0:
masks = torch.empty([0, size[1], size[0]]) # num_instances = 0!
elif isinstance(masks[0], torch.Tensor):
masks = torch.stack(masks, dim=2).clone()
elif isinstance(masks[0], dict) and "counts" in masks[0]:
# RLE interpretation
Expand Down Expand Up @@ -122,7 +124,7 @@ def resize(self, size):
assert height > 0

# Height comes first here!
resized_masks = torch.nn.functional.interpolate(
resized_masks = interpolate(
input=self.masks[None].float(),
size=(height, width),
mode="bilinear",
Expand All @@ -132,6 +134,9 @@ def resize(self, size):
return BinaryMaskList(resized_masks, resized_size)

def convert_to_polygon(self):
if self.masks.numel() == 0:
return PolygonList([], self.size)

contours = self._findContours()
return PolygonList(contours, self.size)

Expand Down Expand Up @@ -159,10 +164,9 @@ def __len__(self):
return len(self.masks)

def __getitem__(self, index):
# Probably it can cause some overhead
# but preserves consistency
masks = self.masks[index].clone()
return BinaryMaskList(masks, self.size)
if self.masks.numel() == 0:
raise RuntimeError("Indexing empty BinaryMaskList")
return BinaryMaskList(self.masks[index], self.size)

def __iter__(self):
return iter(self.masks)
Expand Down Expand Up @@ -308,7 +312,7 @@ def __repr__(self):
s = self.__class__.__name__ + "("
s += "num_groups={}, ".format(len(self.polygons))
s += "image_width={}, ".format(self.size[0])
s += "image_height={}, ".format(self.size[1])
s += "image_height={})".format(self.size[1])
return s


Expand Down

0 comments on commit cd63feb

Please sign in to comment.