Telling fastai to make predictions on test set

I’m sorry if this is a silly question, but it’s not clear to me how to run the model on the test images once it’s ready.

When we say:

learn.predict

It’s giving the predictions for the validation set, right? So how do we get it to give predictions for the test set? And are the folder names built into fastai, so the test set needs to be in a folder called “test”?

Thanks a lot,
Alex

Hi Alex,

I think the command below, is what you’re looking for.
learn.predict(is_test=True)

The ‘test’ folder should be in the same directory as the ‘train’ folder. And, It should contain all the test data set images.

Let me know, if you have any other quires. Keep gritting!

Thanks a lot, however when I change it to:

preds = learn.predict(is_test = True)

It gives me this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-fb6c65c01ca6> in <module>()
  1 print('learn: {}'.format(learn.predict))
----> 2 preds = learn.predict(is_test=True)
  3 print(preds)

~/fastai/courses/dl1/fastai/learner.py in predict(self, is_test)
100         self.load('tmp')
101 
--> 102     def predict(self, is_test=False): return self.predict_with_targs(is_test)[0]
103 
104     def predict_with_targs(self, is_test=False):

~/fastai/courses/dl1/fastai/learner.py in predict_with_targs(self, is_test)
104     def predict_with_targs(self, is_test=False):
105         dl = self.data.test_dl if is_test else self.data.val_dl
--> 106         return predict_with_targs(self.model, dl)
107 
108     def TTA(self, n_aug=4, is_test=False):

~/fastai/courses/dl1/fastai/model.py in predict_with_targs(m, dl)
119     if hasattr(m, 'reset'): m.reset()
120     preda,targa = zip(*[(get_prediction(m(*VV(x))),y)
--> 121                         for *x,y in iter(dl)])
122     return to_np(torch.cat(preda)), to_np(torch.cat(targa))
123 

TypeError: 'NoneType' object is not iterable

Which looks like it’s because test_dl (test dataloader?) is None, and indeed when I say print(data.test_dl) that’s the case.

So for some reason when I make the data object using:

data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))

it’s not making a Data.test_dl object, right? Am I missing a step somewhere? I think the only important lines in my code before this point (other than imports) are:

arch = resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute = True)
learn.fit(0.1, 3)

Thanks again,
Alex

Ah, got it.

Have to specify the test folder using test_name, so I changed the data object definition to:

data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz), test_name='test')

Thanks for the help.

1 Like

I’m having a similar problem. I want to load my trained model and use it on a new dataset for testing purposes.I borrowed from Alex’s post and tried :

architecture = resnet50
PATH = “./data/NewSkinImageSet/”
sz = 224
tfms = tfms_from_model(architecture, sz)

data = ImageClassifierData.from_paths(PATH, tfms=tfms, test_name=‘test’)
learn = ConvLearner.pretrained(architecture, data, precompute=True)
learn.load(‘ModelNew50000’)
learn.fit(0.01, 2)

I have two sets inside test and am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: ‘./data/NewSkinImageSet/valid’
I think my question is, where should the ‘test’ datasets sit in the folder structure and why is it looking for a validation set?
Thanks
Tony

There’s a thread in which it’s discussed to predict on a single test image, not sure whether it helps

Or just have a look at the Jeremy’s notebooks ls commands outputs to get the directory structure…

A small hack would be sacrifice a single image to Validation set and kind of fast.ai will be happy again…