What fake label for segmentation test set

Hello,

I am trying to setup the datablock api chain for a segmentation task.
Unfortunately I have the following problem, when I want to add a test set:

src = (SegmentationItemList.from_csv(path=PATH, csv_name=TRAIN_PATHS_CSV_NAME)
.split_by_rand_pct(valid_pct=0.2, seed = seed)
.label_from_func(get_y_fn, classes=array([‘background’, ‘nucleus’])))
data = (src.add_test_folder(TEST_ONE_FOLDER, label=src.train.y[0])
.transform(tfms, tfm_y=True, size=sz)
.databunch(bs=bs)
.normalize())


Exception Traceback (most recent call last)
~/work/network/fastai/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
570 x = ds[0]
–> 571 try: x.apply_tfms(tfms, **kwargs)
572 except Exception as e:

~/work/network/fastai/fastai/core.py in apply_tfms(self, tfms, **kwargs)
160 “Subclass this method if you want to apply data augmentation with tfms to this ItemBase.”
–> 161 if tfms: raise Exception(f"Not implemented: you can’t apply transforms to this type of item ({self.class.name})")
162 return self

Exception: Not implemented: you can’t apply transforms to this type of item (EmptyLabel)

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)
in
1 data = (src.add_test_folder(TEST_ONE_FOLDER)
----> 2 .transform(tfms, tfm_y=True, size=sz)
3 .databunch(bs=bs)
4 .normalize())

~/work/network/fastai/fastai/data_block.py in transform(self, tfms, **kwargs)
487 self.train.transform(tfms[0], **kwargs)
488 self.valid.transform(tfms[1], **kwargs)
–> 489 if self.test: self.test.transform(tfms[1], **kwargs)
490 return self
491

~/work/network/fastai/fastai/data_block.py in transform(self, tfms, tfm_y, **kwargs)
694 _check_kwargs(self.x, tfms, **kwargs)
695 if tfm_y is None: tfm_y = self.tfm_y
–> 696 if tfm_y: _check_kwargs(self.y, tfms, **kwargs)
697 self.tfms,self.tfmargs = tfms,kwargs
698 self.tfm_y,self.tfms_y,self.tfmargs_y = tfm_y,tfms,kwargs

~/work/network/fastai/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
571 try: x.apply_tfms(tfms, **kwargs)
572 except Exception as e:
–> 573 raise Exception(f"It’s not possible to apply those transforms to your dataset:\n {e}")
574
575 class LabelList(Dataset):

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)

It seems that fastai trys to apply the transformation on the test set masks too, but these are of course non existent.
I tried to use a mask from the train set as fake label and a zeroed numpy array, but this did not work either.

Is there a way to tell fastai, that it should not apply the transforms on the test masks or what fake test mask could I use?

Thanks in advance!

Christoph

1 Like

Hey, did you ever figure this out? I just ran into the same issue.

I created a new dataset for prediction and set tfm_y to false.

training dataset:
data_train = (ImageImageList.from_folder(TRAIN_LR)
.split_by_rand_pct(0.1, seed=seed))
.label_from_func(lambda x: TRAIN/x.name)
.transform(tfms, size=size, tfm_y=True)
.databunch(bs=bs)
.normalize(do_y=True))

test data:

data_test = (ImageImageList.from_folder(TRAIN_LR)
    .split_by_rand_pct(0.1, seed=seed)
    .label_from_func(lambda x: TRAIN/x.name)
    .add_test_folder(test_folder=TRAIN)
    .transform(tfms, size=sz, tfm_y=False)
    .databunch(bs=bs)
    .normalize(do_y=False))

I solved this by adding adding apply_tfms to EmptyLabel.

class EmptyLabel(ItemBase):
    "Should be used for a dummy label."
    def __init__(self): self.obj,self.data = 0,0
    def __str__(self):  return ''
    def __hash__(self): return hash(str(self))
    def apply_tfms(self, tfms:Collection, **kwargs): return self