Modify segmentation mask value

I’m trying to do Semantic Segmentation with the Berkeley Deep Drive dataset.

The main issue I’m facing is that the mask is a single image with values 0 to 18 for defined classes and 255 for “Void”.

I’m able to replace the void class value from 255 to 19 by using the after_open parameter in open_mask function and make it call my custom function:

def change_void_val(x):
    # Change void value from 255 in the dataset to 19
    # for better visualization of segmentation map
    x = np.array(x)
    x[x==255] = 19
    return x

mask = open_mask(get_y_fn(img_f), after_open = change_void_val)

However I don’t have access to this parameter when creating the dataset. I also tried to use transform_y.

def _change_void_val(x):
    x = np.array(x)
    x[x==255] = 10000
    return x

change_void_val = TfmPixel(_change_void_val)

data = (src.transform_y([[change_void_val()],[change_void_val()]])
        .transform(get_transforms(), size=size, tfm_y=True)
        .databunch(bs=bs)
        .normalize(imagenet_stats))

Any idea?

My recommendation is, that you change the value as a pre-processing, so that the mask are saved with the correct values. This will increase the performance and solve the problem. As you probably will touch the data quite often, I think this is reasonable.

1 Like

Thanks, I guess it is a reasonable solution.

Hi, I have the same problem.
I want to change the void class value from 255 to 6. Could you please let me know how did you solve this problem?

I have a tutorial on this here: https://walkwithfastai.com/Binary_Segmentation

The main focus is to just make everything from 0-n, but you can generate your own custom mapping dictionary and use that

1 Like