SegmentationDataLoaders show_batch not showing mask in other color

I have tried migrating my code to FastAI V2.

The previous code was as following:

src_size = np.array(mask.shape[1:])
# size = src_size//2
size = src_size
bs = 1
codes = np.loadtxt(path_manual/'codes.txt', dtype=str)
src = (SegmentationDataLoaders.from_folder(path_manual_img)
       # Split data into training and validation set 
       .split_by_rand_pct(valid_pct=0.1)
       # Label data using the get_y_fn function
       .label_from_func(get_y_fn_manual, classes=codes)
)
data = (src.transform(get_transforms(), size=size, tfm_y=True)
        # Flip images horizontally 
        .databunch(bs=bs)
        # Create a databunch
        .normalize(imagenet_stats)
        # Normalize for resnet
)

My updated code is as follows:

src_size = np.array(mask.shape[1:])
# size = src_size//2
size = src_size
bs = 1
codes = np.loadtxt(path_manual/'codes.txt', dtype=str)

dls = SegmentationDataLoaders.from_label_func(path=path_manual,fnames=fnames_manual,label_func=get_y_fn_manual,valid_pct=0.1,codes=codes,bs=1,shuffle_train=True,item_tfms=Resize((size,size)),batch_tfms=Normalize.from_stats(*imagenet_stats))

However, show_batch isn’t showing the mask in other color as used to do.

With this code I tested if the get_y_fn_manual function works perfectly and it does.

mask = PILMask.create(get_y_fn_manual(img_f))
mask.show(figsize=(5, 5), alpha=1)

You can pass in a color map if you want (cmap) and play with it. For example dls.show_batch(cmap=greys)

This is matplot code :wink:

1 Like

I have tried that. However, Mask is not showing properly.

In FastAI V1 version it shows as follows:

In FastAI V2 it shows as follows:

One other bit to try is set vmin=1 on show batch (sorry I’m not giving exact solutions, I worked with segmentation a little bit it was a little bit ago :slight_smile: )

I’ve also moved your topic to v2

3 Likes

With vmin=0 and vmax=1 its working properly!!!

What does vmin and vmax do?

Sorry for my newbiew question, I am just starting!

Not a problem! These are the best type of questions! (Where a search doesn’t really explain it well :slight_smile: ) think of it as visibility_min and visibility_max. So our mask codes are 0,1,2 etc right? We can adjust what codes show by moving this threshold. In our case we want pixels 0-1. But say with the CamVid example, we could do vmin=2, vmax=31. Also, you can put a min or max that’s not in the range, but you’ll see a result similar to your initial find :slight_smile:

Thank you very much! :smiley:

I am just starting my final degree project and I am going to use FastA’s library!!!

2 Likes

Hi, I encountered the same problem with my binary classification. Why is vmin=1 the default for TensorMask? IMHO vmin=0 would make more sense.

I’ve checked the fastai camvid dataset and lbl==0 is an Animal and not background or void (which can be verified on the Seq05VD_f01830.png photo and the corresponding mask) so it does not make sense to me even considering the default dataset.

THANKS! have been bangingg my head the whole day with this… class labelled 0 is not showing on my show_batch!

An example is always appreciated:

This vmin and vmax params don’t do what one would expect…

Probably the right solution is making use of masked arrays to overlay the codes over the image and ignore background class if available.

1 Like

I seems the problem got fixed in the repo a few days ago: