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

support RLE and binary mask #150

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 6 additions & 7 deletions maskrcnn_benchmark/structures/segmentation_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ def __init__(self, segm, size, mode):
else:
if type(segm) == list:
wangg12 marked this conversation as resolved.
Show resolved Hide resolved
# polygons
rle = mask_utils.frPyObjects(segm, height, width)
mask = np.array(mask_utils.decode(rle), dtype=np.float32)
mask = np.sum(mask, axis=2)
mask = torch.from_numpy(np.array(mask > 0, dtype=np.float32))
mask = Polygons(segm, size, 'polygon').convert('mask').to(dtype=torch.float32)
elif type(segm) == dict and 'counts' in segm:
if type(segm['counts']) == list:
# uncompressed RLE
Expand All @@ -41,7 +38,7 @@ def __init__(self, segm, size, mode):
if type(segm) == np.ndarray:
mask = torch.from_numpy(segm).to(dtype=torch.float32)
else: # torch.Tensor
mask = segm
mask = segm.to(dtype=torch.float32)
self.mask = mask
self.size = size
self.mode = mode
Expand All @@ -65,7 +62,9 @@ def transpose(self, method):
def crop(self, box):
box = [int(b) for b in box]
w, h = box[2] - box[0], box[3] - box[1]
cropped_mask = self.mask[box[1]: box[3]+1, box[0]: box[2]+1]
w = max(w, 1)
h = max(h, 1)
cropped_mask = self.mask[box[1]: box[3], box[0]: box[2]]
return Mask(cropped_mask, size=(w, h), mode=self.mode)

def resize(self, size, *args, **kwargs):
Expand All @@ -74,7 +73,7 @@ def resize(self, size, *args, **kwargs):
return Mask(scaled_mask, size=size, mode=self.mode)

def convert(self, mode):
mask = self.mask
mask = self.mask.to(dtype=torch.uint8)
return mask

def __iter__(self):
Expand Down
Loading