Skip to content

Commit 5c923c6

Browse files
ppwwyyxxfacebook-github-bot
authored andcommitted
support creating bitmasks from empty polygons
Reviewed By: HannaMao Differential Revision: D30795434 fbshipit-source-id: 7a841e0f980f6c8ff40411acff9c732a53ac2c3b
1 parent f88b04f commit 5c923c6

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

detectron2/structures/masks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ def from_polygon_masks(
170170
if isinstance(polygon_masks, PolygonMasks):
171171
polygon_masks = polygon_masks.polygons
172172
masks = [polygons_to_bitmask(p, height, width) for p in polygon_masks]
173-
return BitMasks(torch.stack([torch.from_numpy(x) for x in masks]))
173+
if len(masks):
174+
return BitMasks(torch.stack([torch.from_numpy(x) for x in masks]))
175+
else:
176+
return BitMasks(torch.empty(0, height, width, dtype=torch.bool))
174177

175178
@staticmethod
176179
def from_roi_masks(roi_masks: "ROIMasks", height: int, width: int) -> "BitMasks":

tests/structures/test_masks.py

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def test_get_bounding_box(self):
3838
reconstruct_box = PolygonMasks([[poly]]).get_bounding_boxes()[0].tensor
3939
self.assertTrue(torch.all(box == reconstruct_box).item())
4040

41+
def test_from_empty_polygons(self):
42+
masks = BitMasks.from_polygon_masks([], 100, 100)
43+
self.assertEqual(masks.tensor.shape, (0, 100, 100))
44+
4145

4246
if __name__ == "__main__":
4347
unittest.main()

0 commit comments

Comments
 (0)