Any good code for inference segmentation?

Can someone please help with pointers to good code for how to deploy segmentation models in inference?

For example, after training, I reload the pkl model and use the following:
src = (SegmentationItemList.from_df(tst_df, path=tst_path, cols='filename').split_none())
data = src.databunch(bs=bs).normalize(imagenet_stats)

It gives the following error: Exception: Your data isn’t labeled, can’t turn it in a DataBunch yet!

I would like to use learn.get_preds to get inference predictions, but it requires a databunch to work?

I’ve been on the fastai tutorial for inference, but there’s very little information for segmentation predictions using learn.get_preds
https://docs.fast.ai/tutorial.inference.html

You should use

learn = load_learner(path, fname=..., test=SegmentationItemList.from_df(tst_df, path=tst_path, cols='filename'))

then learn.get_preds(ds_type=DatasetType.Test) should work.

That was one of the ways I tried, but it returns the following error:

Exception: It’s not possible to apply those transforms to your dataset:
Not implemented: you can’t apply transforms to this type of item (EmptyLabel)

1 Like

Oh, that’s because you have transforms applied to your validation set. The fastai library returns this error to protect you (in a way) because transforms are usually irreversible and you might lose some part of your images in the test set which won’t have a proper mask. Plus the predicted mask will apply to the transformed image, not the original one.

If your images aren’t all of the same size, you’ll need to manually resize them to have the same size, then post-process your predictions accordingly, but you shouldn’t have transforms on your validation set.

1 Like

One relevant note from another thread - you can solve the size issue as follows: