I’ve trained a u-net for semantic segmentation and now I want to try making predictions on a set of test images contained in a separate folder.
After much searching I came across the article “Deep Learning on a Shoestring” (https://docs.fast.ai/tutorial.resources.html)
This seemed to have exactly what I wanted - the ability to save a trained network and then load it again explicitly for inference, with the suggested code being this:
# end of training
learn.fit_one_cycle(epochs)
learn.freeze()
learn.export()
learn.purge()
# beginning of inferences
learn = load_learner(path, test=ImageItemList.from_folder(path/'test'))
preds = learn.get_preds(ds_type=DatasetType.Test)
However, when I run this I get the following error:
--------------------------------------------------------------------------- Exception Traceback (most recent call last) /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs) 537 x = ds[0] --> 538 try: x.apply_tfms(tfms, **kwargs) 539 except Exception as e: /opt/conda/lib/python3.6/site-packages/fastai/core.py in apply_tfms(self, tfms, **kwargs) 156 "Subclass this method if you want to apply data augmentation with `tfms` to this `ItemBase`." --> 157 if tfms: raise Exception(f"Not implemented: you can't apply transforms to this type of item ({self.__class__.__name__})") 158 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) <ipython-input-57-a6feb4485995> in <module>() 1 # beginning of inferences ----> 2 learn = load_learner('/kaggle/working', test=ImageItemList.from_folder(path_test_img)) 3 preds = learn.get_preds(ds_type=DatasetType.Test) /opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in load_learner(path, fname, test) 502 model = state.pop('model') 503 src = LabelLists.load_state(path, state.pop('data')) --> 504 if test is not None: src.add_test(test) 505 data = src.databunch() 506 cb_state = state.pop('cb_state') /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in add_test(self, items, label) 508 if isinstance(items, ItemList): items = self.valid.x.new(items.items, xtra=items.xtra).process() 509 else: items = self.valid.x.new(items).process() --> 510 self.test = self.valid.new(items, labels) 511 return self 512 /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in new(self, x, y, **kwargs) 573 def new(self, x, y, **kwargs)->'LabelList': 574 if isinstance(x, ItemList): --> 575 return self.__class__(x, y, tfms=self.tfms, tfm_y=self.tfm_y, **self.tfmargs) 576 else: 577 return self.new(self.x.new(x, **kwargs), self.y.new(y, **kwargs)).process() /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in __init__(self, x, y, tfms, tfm_y, **kwargs) 546 self.y.x = x 547 self.item=None --> 548 self.transform(tfms, **kwargs) 549 550 def __len__(self)->int: return len(self.x) if self.item is None else 1 /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in transform(self, tfms, tfm_y, **kwargs) 663 _check_kwargs(self.x, tfms, **kwargs) 664 if tfm_y is None: tfm_y = self.tfm_y --> 665 if tfm_y: _check_kwargs(self.y, tfms, **kwargs) 666 self.tfms,self.tfmargs = tfms,kwargs 667 self.tfm_y,self.tfms_y,self.tfmargs_y = tfm_y,tfms,kwargs /opt/conda/lib/python3.6/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs) 538 try: x.apply_tfms(tfms, **kwargs) 539 except Exception as e: --> 540 raise Exception(f"It's not possible to apply those transforms to your dataset:\n {e}") 541 542 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 appears to be wanting to apply a transform or add labels to the test data - I wouldn’t want either of these things to happen, since I want the un-altered image to go through the network and for this to produce the label.
Any pointers to how I could fix this would be most appreciated!