@@ -1373,6 +1373,14 @@ def _mask_to_rle_pytorch(input_mask: "torch.Tensor"):
1373
1373
out = []
1374
1374
for i in range (batch_size ):
1375
1375
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
1376
1384
btw_idxs = cur_idxs [1 :] - cur_idxs [:- 1 ]
1377
1385
counts = [] if input_mask [i , 0 ] == 0 else [0 ]
1378
1386
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"):
1396
1404
out = []
1397
1405
for i in range (batch_size ):
1398
1406
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
1399
1415
btw_idxs = cur_idxs [1 :] - cur_idxs [:- 1 ]
1400
1416
counts = [] if input_mask [i , 0 ] == 0 else [0 ]
1401
1417
counts += [cur_idxs [0 ].item ()] + btw_idxs .tolist () + [height * width - cur_idxs [- 1 ]]
0 commit comments