Binary segmentation in v2 - any special subclassing needed like in v1?

For v2 do we need to do anything special for binary segmentation, or just run it like camvid but with only 2 labels (i.e. class 1 and void)?

as background for this question - I did some binary segmentation in v1 and we had to do some special subclassing to handle binary (see below).

ala from v1:

class BinaryLabelList(SegmentationLabelList):
        def open(self, fn): return open_mask(fn, div=True)

 class BinaryItemList(SegmentationItemList):
        _label_cls = BinaryLabelList

Thanks!

You’d need to do two labels, (just like CamVid). But you need to be sure that your mask only has the 2 you want (0 and 1) for pixels. If not that’s not a fastai issue that’s a dataset issue. There’s a post discussing how to see how many total classes there are in a segmentation dataset, I’ll try to find it.

Short answer: no, you shouldn’t.

1 Like

Struggling with this. I have a binary mask with two values (0 and 255) I found that IntToFloatTensor(div_mask=255) should be the one function that returns the proper mask but I am still getting an AssertionError when trying to lr_find(). When trying:

img_fn = fnames[0]
get_msk = lambda o: mask_path/f'{o.stem}_mask{o.suffix}'
msk = PILMask.create(get_msk(img_fn))
max(tensor(msk)[1000])

got 255 if I tried to perform IntToFloatTensor:

tfm = IntToFloatTensor(div_mask=255)
max(tfm(TensorMask(msk)[1000]))

still got 255

What I am missing?

EDIT
Seems that I found the issue. Looking at MaskBlockI found that there is a batch_tfms there:

def MaskBlock(codes=None):
    "A `TransformBlock` for segmentation masks, potentially with `codes`"
    return TransformBlock(type_tfms=PILMask.create, item_tfms=AddMaskCodes(codes=codes), 
                          batch_tfms=IntToFloatTensor())

Adding IntToFloatTensor(div_mask=255) do the trick

2 Likes