Lesson 3: learn.TTA(is_test=True) is giving the error 'NoneType' object is not iterable

I am using resnet50 on Kaggle’s Plant Seedling Classifier Dataset. I have obtained around 97% accuracy on the validation set. I want to try it on the test set now and make a submission. I went with the method given in Lecture Notes for lesson 3.

log_preds,y = learn.TTA()
probs = np.mean(np.exp(log_preds),0)
Runs as expected without any errors.

But when I try this on the test set as:

log_preds,y = learn.TTA(is_test=True)
probs = np.mean(np.exp(log_preds))

I get the error:

TypeError Traceback (most recent call last)
in ()
----> 1 log_preds,y = learn.TTA(is_test=True)
2 probs = np.mean(np.exp(log_preds))

~/fastai/courses/dl1/fastai/learner.py in TTA(self, n_aug, is_test)
388 dl1 = self.data.test_dl if is_test else self.data.val_dl
389 dl2 = self.data.test_aug_dl if is_test else self.data.aug_dl
–> 390 preds1,targs = predict_with_targs(self.model, dl1)
391 preds1 = [preds1]*math.ceil(n_aug/4)
392 preds2 = [predict_with_targs(self.model, dl2)[0] for i in tqdm(range(n_aug), leave=False)]

~/fastai/courses/dl1/fastai/model.py in predict_with_targs(m, dl)
248
249 def predict_with_targs(m, dl):
–> 250 preda,targa = predict_with_targs_(m, dl)
251 return np.concatenate(preda), np.concatenate(targa)
252

~/fastai/courses/dl1/fastai/model.py in predict_with_targs_(m, dl)
244 if hasattr(m, ‘reset’): m.reset()
245 res = []
–> 246 for *x,y in iter(dl): res.append([get_prediction(to_np(m(*VV(x)))),to_np(y)])
247 return zip(*res)
248

TypeError: ‘NoneType’ object is not iterable

Did you set the testname ?

data = ImageClassifierData.from_paths(PATH, tfms= tfms, bs=bs, test_name='test')

I guess because it is not iterable then maybe your test folder is empty.

Not related but I think in the 2nd line you should set the axis for np.mean. Similar to the validation set. You missed the 0 in the end.

probs = np.mean(np.exp(log_preds),0)

Hope that helps,

1 Like