Label_empty() got an unexpected keyword argument ‘label_cls’

Hi,

edited because I did not read the docs thoroughly enough

I was trying to initialize an empty databunch to predict using a test set in a production environment, following this thread: How do I predict a batch of images without labels

I got stuck trying to use label_empty():
label_empty() got an unexpected keyword argument ‘label_cls’

After looking more closely at the inference tutorial (https://docs.fast.ai/tutorial.inference.html) I now see that the new way to do this is to use Learner.export() and then load_learner().

-Shea

I’m still interested to have a minimal example of that error, since you shouldn’t get that bug.

Hi Sylvain,

Sure.

This post has an example: How do I predict a batch of images without labels

And here’s my example:

./test_set is empty except for the folder test/ which contains some images.

data=ImageItemList.from_folder('./test_set').no_split()
databunch=data2.label_empty().add_test_folder('test').databunch()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-80abe444b65e> in <module>()
----> 1 databunch=data2.label_empty().add_test_folder('test').databunch()

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in _inner(*args, **kwargs)
    421             assert isinstance(self.train, LabelList)
    422             kwargs['label_cls'] = self.train.y.__class__
--> 423             self.valid = fv(*args, **kwargs)
    424             self.__class__ = LabelLists
    425             self.process()

TypeError: label_empty() got an unexpected keyword argument 'label_cls'

Should be fixed in master now.

1 Like

Adding to this thread rather than creating another. I’m having an issue with label_empty() too. Trying to use it with the camvid segmentation dataset:

test_data =(SegmentationItemList.from_folder(path_img).split_none().label_empty().databunch())

gives the error:

Exception: Your data isn’t split, if you don’t want a validation set, please use split_none.

split_none works by itself though, it’s only when I switch to label_empty that the error pops up.

This bug has been fixed in master :wink:

1 Like

Thanks! :grin:

I can create a segmentation test databunch successfully now, but when trying to use add_test I get the error:

TypeError: object of type ‘ImageDataBunch’ has no len()

My code:

test_data = (SegmentationItemList
.from_folder(path_img)
.split_none()
.label_empty()
.transform(size=128, tfm_y=False)
.databunch(bs=16))

learn.data.add_test(test_data, EmptyLabel)

Now, if you add a test set, it must be an ItemList. So just SegmentationItemList .from_folder(path_img).

1 Like

Hmm feeling a bit dumb that I can’t figure this out. So now I just tried:

test_data = SegmentationItemList.from_folder(path_img)
learn.data.add_test(test_data)

And getting the 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)

Well you can’t apply data augmentation to your images at inference in a segmentation task: you may lose some pixels of your original image and you won’t have predictions for them. I don’t know what where the validation transforms you passed, but there shouldn’t be any.

1 Like

Does the setup from the Camvid notebook apply transforms to the validation set? The full code snippet I’m trying is:

from fastai.vision import *

path = untar_data(URLs.CAMVID)

path_lbl = path/'labels'
path_img = path/'images'

get_y_fn = lambda x: path_lbl/f'{x.stem}_P{x.suffix}'

codes = np.loadtxt(path/'codes.txt', dtype=str)

src = (SegmentationItemList.from_folder(path_img)
       .split_by_fname_file(path/'valid.txt')
       .label_from_func(get_y_fn, classes=codes))

data = (src.transform(get_transforms(), size=128, tfm_y=True)
        .databunch(bs=8)
        .normalize(imagenet_stats))

learn = unet_learner(data, models.resnet34, metrics=accuracy, wd=1e-2)

test_imgs = path/'test'
test_data = SegmentationItemList.from_folder(test_imgs)
learn.data.add_test(test_data)

If I set tfm_y=False it works, but I think this turns off training label transforms too which I shouldn’t do.

I’m thinking maybe the only transformation on the validation set is resizing, but that could be what’s causing the issue?


Edit: Ah okay, I didn’t realize that get_transforms() by default is cropping the validation set and then that transformation is applied when trying to add a test set. I think I can just do this after training then:

# add test data
learn.data.valid_ds.tfms = []
learn.data.valid_ds.tfms_y = []
test_imgs = path/'test'
test_data = SegmentationItemList.from_folder(test_imgs)
learn.data.add_test(test_data)

Thanks!

1 Like