Skip to content

Commit 53ddcab

Browse files
committed
Handle empty change indices in RLE conversion for masks
1 parent 91f14f1 commit 53ddcab

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/transformers/models/sam/image_processing_sam.py

+16
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,14 @@ def _mask_to_rle_pytorch(input_mask: "torch.Tensor"):
13731373
out = []
13741374
for i in range(batch_size):
13751375
cur_idxs = change_indices[change_indices[:, 0] == i, 1] + 1
1376+
if len(cur_idxs) == 0:
1377+
# No changes => either all 0 or all 1
1378+
# If the entire mask is 0, RLE is [height*width] or if the entire mask is 1, RLE is [0, height*width].
1379+
if input_mask[i, 0] == 0:
1380+
out.append({"size": [height, width], "counts": [height * width]})
1381+
else:
1382+
out.append({"size": [height, width], "counts": [0, height * width]})
1383+
continue
13761384
btw_idxs = cur_idxs[1:] - cur_idxs[:-1]
13771385
counts = [] if input_mask[i, 0] == 0 else [0]
13781386
counts += [cur_idxs[0].item()] + btw_idxs.tolist() + [height * width - cur_idxs[-1]]
@@ -1396,6 +1404,14 @@ def _mask_to_rle_tf(input_mask: "tf.Tensor"):
13961404
out = []
13971405
for i in range(batch_size):
13981406
cur_idxs = change_indices[change_indices[:, 0] == i, 1] + 1
1407+
if len(cur_idxs) == 0:
1408+
# No changes => either all 0 or all 1
1409+
# If the entire mask is 0, RLE is [height*width] or if the entire mask is 1, RLE is [0, height*width].
1410+
if input_mask[i, 0] == 0:
1411+
out.append({"size": [height, width], "counts": [height * width]})
1412+
else:
1413+
out.append({"size": [height, width], "counts": [0, height * width]})
1414+
continue
13991415
btw_idxs = cur_idxs[1:] - cur_idxs[:-1]
14001416
counts = [] if input_mask[i, 0] == 0 else [0]
14011417
counts += [cur_idxs[0].item()] + btw_idxs.tolist() + [height * width - cur_idxs[-1]]

0 commit comments

Comments
 (0)