Predictions on Test Image Data with a Loaded Learner

Hello, I’m sure this is a simple issue, but I’ve been having a lot of difficulty and would love some help. I’m loading a learner with Load_Learner and then attempting to add a test set to train it. Here is what I have.

CNN = load_learner(path, ‘ReallyGood.pkl’)
bs = 64
data = ImageDataBunch.from_df(‘Data/’, pdlabels, folder=‘train_images’, test=‘MyTest’, ds_tfms=get_transforms(), size=224, bs=bs).normalize(imagenet_stats)
CNN.predict(data, is_test=True)

This has been giving me the error:

AttributeError Traceback (most recent call last)
in
----> 1 CNN.predict(data, is_test=True)

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in predict(self, item, **kwargs)
357 def predict(self, item:ItemBase, **kwargs):
358 “Return predicted class, label and probabilities for item.”
–> 359 batch = self.data.one_item(item)
360 res = self.pred_batch(batch=batch)
361 pred,x = res[0],batch[0]

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in one_item(self, item, detach, denorm, cpu)
179 ds = self.single_ds
180 with ds.set_item(item):
–> 181 return self.one_batch(ds_type=DatasetType.Single, detach=detach, denorm=denorm, cpu=cpu)
182
183 def show_batch(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, reverse:bool=False, **kwargs)->None:

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in one_batch(self, ds_type, detach, denorm, cpu)
166 w = self.num_workers
167 self.num_workers = 0
–> 168 try: x,y = next(iter(dl))
169 finally: self.num_workers = w
170 if detach: x,y = to_detach(x,cpu=cpu),to_detach(y,cpu=cpu)

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in iter(self)
73 def iter(self):
74 “Process and returns items from DataLoader.”
—> 75 for b in self.dl: yield self.proc_batch(b)
76
77 @classmethod

~/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in next(self)
613 if self.num_workers == 0: # same-process loading
614 indices = next(self.sample_iter) # may raise StopIteration
–> 615 batch = self.collate_fn([self.dataset[i] for i in indices])
616 if self.pin_memory:
617 batch = pin_memory_batch(batch)

~/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in (.0)
613 if self.num_workers == 0: # same-process loading
614 indices = next(self.sample_iter) # may raise StopIteration
–> 615 batch = self.collate_fn([self.dataset[i] for i in indices])
616 if self.pin_memory:
617 batch = pin_memory_batch(batch)

~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in getitem(self, idxs)
631 else: x,y = self.item ,0
632 if self.tfms or self.tfmargs:
–> 633 x = x.apply_tfms(self.tfms, **self.tfmargs)
634 if hasattr(self, ‘tfms_y’) and self.tfm_y and self.item is None:
635 y = y.apply_tfms(self.tfms_y, **{**self.tfmargs_y, ‘do_resolve’:False})

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in getattr(self, k)
120 return cls(*dls, path=path, device=device, dl_tfms=dl_tfms, collate_fn=collate_fn, no_check=no_check)
121
–> 122 def getattr(self,k:int)->Any: return getattr(self.train_dl, k)
123 def setstate(self,data:Any): self.dict.update(data)
124

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in getattr(self, k)
36
37 def len(self)->int: return len(self.dl)
—> 38 def getattr(self,k:str)->Any: return getattr(self.dl, k)
39 def setstate(self,data:Any): self.dict.update(data)
40

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in DataLoader___getattr__(dl, k)
18 torch.utils.data.DataLoader.init = intercept_args
19
—> 20 def DataLoader___getattr__(dl, k:str)->Any: return getattr(dl.dataset, k)
21 DataLoader.getattr = DataLoader___getattr__
22

~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in getattr(self, k)
621 res = getattr(y, k, None)
622 if res is not None: return res
–> 623 raise AttributeError(k)
624
625 def setstate(self,data:Any): self.dict.update(data)

AttributeError: apply_tfms

=============================================================

I also would like to try using CNN.pred_batch and CNN.get_preds but I’m not sure how to load the test data into the re-loaded model.

I cannot help you on the fly with your error, however the docs for load_learner show that you can add an ItemList as test set using
cnn = load_learner(path, name, test_ItemList)
Maybe this also resolves your error.

Thanks for the note, I’m currently running:
CNN = load_learner(path, ‘ReallyGood.pkl’, data.test_ds)
and
CNN.data.add_test(data.test_ds)

On two different notebooks, and both are taking an obscenely long time to run. The test folder only has 300 images, so I’m wondering if they’re caught in some sort of loop and I have a logic error of some kind.