Error using ClassificationInterpretation.from_learner

Hello,

Im using a resnet34 model to train my image classifier. Basically, i have divided the images i have in five subfolders and running the learner. Here is the code:
learn = create_cnn(data, models.resnet34, metrics=error_rate) learn.fit_one_cycle(2) learn.save('emotion-stage-1')
These 3 steps work perfectly, then i try to create a classificationInterpretation object to check for the top losses but i get an error on this line:
interp = ClassificationInterpretation.from_learner(learn)

Does anyone has any idea what is happening here?

Error stack trace is:
IndexError Traceback (most recent call last) <ipython-input-12-04160ac96557> in <module> ----> 1 interp = ClassificationInterpretation.from_learner(learn) /anaconda3/lib/python3.7/site-packages/fastai/vision/learner.py in _cl_int_from_learner(cls, learn, ds_type, activ, tta) 128 def _cl_int_from_learner(cls, learn:Learner, ds_type:DatasetType=DatasetType.Valid, activ:nn.Module=None, tta=False): 129 "Create an instance of ClassificationInterpretation. ``tta indicates if we want to use Test Time Augmentation.
--> 130 preds = learn.TTA(ds_type=ds_type, with_loss=True) if tta else learn.get_preds(ds_type=ds_type, activ=activ, with_loss=True) 131 132 return cls(learn, *preds, ds_type=ds_type)

/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in get_preds(self, ds_type, activ, with_loss, n_batch, pbar)
    337         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(self.callbacks)
    338         return get_preds(self.model, self.dl(ds_type), cb_handler=CallbackHandler(callbacks),
--> 339                          activ=activ, loss_func=lf, n_batch=n_batch, pbar=pbar)
    340 
    341     def pred_batch(self, ds_type:DatasetType=DatasetType.Valid, batch:Tuple=None, reconstruct:bool=False,

/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
     42     "Tuple of predictions and targets, and optional losses (if `loss_func`) using `dl`, max batches `n_batch`."
     43     res = [to_float(torch.cat(o).cpu()) for o in
---> 44            zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
     45     if loss_func is not None:
     46         with NoneReduceOnCPU(loss_func) as lf: res.append(lf(res[0], res[1]))

/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
     55         val_losses,nums = [],[]
     56         if cb_handler: cb_handler.set_dl(dl)
---> 57         for xb,yb in progress_bar(dl, parent=pbar, leave=(pbar is not None)):
     58             if cb_handler: xb, yb = cb_handler.on_batch_begin(xb, yb, train=False)
     59             val_loss = loss_batch(model, xb, yb, loss_func, cb_handler=cb_handler)

/anaconda3/lib/python3.7/site-packages/fastprogress/fastprogress.py in __iter__(self)
     70         self.update(0)
     71         try:
---> 72             for i,o in enumerate(self._gen):
     73                 if i >= self.total: break
     74                 yield o

/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)
    817             else:
    818                 del self._task_info[idx]
--> 819                 return self._process_data(data)
    820 
    821     next = __next__  # Python 2 compatibility

/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
    844         self._try_put_index()
    845         if isinstance(data, ExceptionWrapper):
--> 846             data.reraise()
    847         return data
    848 

/anaconda3/lib/python3.7/site-packages/torch/_utils.py in reraise(self)
    383             # (https://bugs.python.org/issue2651), so we work around it.
    384             msg = KeyErrorMessage(msg)
--> 385         raise self.exc_type(msg)

IndexError: Caught IndexError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/anaconda3/lib/python3.7/site-packages/fastai/data_block.py", line 651, in __getitem__
    if self.item is None: x,y = self.x[idxs],self.y[idxs]
  File "/anaconda3/lib/python3.7/site-packages/fastai/data_block.py", line 120, in __getitem__
    if isinstance(idxs, Integral): return self.get(idxs)
  File "/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py", line 270, in get
    fn = super().get(i)
  File "/anaconda3/lib/python3.7/site-packages/fastai/data_block.py", line 75, in get
    return self.items[i]
IndexError: index 0 is out of bounds for axis 0 with size 0`

Based on the error, it seems that you have an empty validation set. Can you check the size of the validation set?

im having a similar issue. I am training a resnet18 model and getting the same error
learn.get_preds(DatasetType.Test) gives me the error-

IndexError                                Traceback (most recent call last)
<ipython-input-20-708362d48a6c> in <module>
----> 1 learn.get_preds(DatasetType.Test)

/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(self, ds_type, activ, with_loss, n_batch, pbar)
    337         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(self.callbacks)
    338         return get_preds(self.model, self.dl(ds_type), cb_handler=CallbackHandler(callbacks),
--> 339                          activ=activ, loss_func=lf, n_batch=n_batch, pbar=pbar)
    340 
    341     def pred_batch(self, ds_type:DatasetType=DatasetType.Valid, batch:Tuple=None, reconstruct:bool=False,

/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
     42     "Tuple of predictions and targets, and optional losses (if `loss_func`) using `dl`, max batches `n_batch`."
     43     res = [to_float(torch.cat(o).cpu()) for o in
---> 44            zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
     45     if loss_func is not None:
     46         with NoneReduceOnCPU(loss_func) as lf: res.append(lf(res[0], res[1]))

/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
     55         val_losses,nums = [],[]
     56         if cb_handler: cb_handler.set_dl(dl)
---> 57         for xb,yb in progress_bar(dl, parent=pbar, leave=(pbar is not None)):
     58             if cb_handler: xb, yb = cb_handler.on_batch_begin(xb, yb, train=False)
     59             val_loss = loss_batch(model, xb, yb, loss_func, cb_handler=cb_handler)

/opt/conda/envs/fastai/lib/python3.6/site-packages/fastprogress/fastprogress.py in __iter__(self)
     70         self.update(0)
     71         try:
---> 72             for i,o in enumerate(self._gen):
     73                 if i >= self.total: break
     74                 yield o

/opt/conda/envs/fastai/lib/python3.6/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

/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    817             else:
    818                 del self.task_info[idx]
--> 819                 return self._process_data(data)
    820 
    821     next = __next__  # Python 2 compatibility

/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
    844         self._try_put_index()
    845         if isinstance(data, ExceptionWrapper):
--> 846             data.reraise()
    847         return data
    848 

/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/_utils.py in reraise(self)
    367             # (https://bugs.python.org/issue2651), so we work around it.
    368             msg = KeyErrorMessage(msg)
--> 369         raise self.exc_type(msg)

IndexError: Caught IndexError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/data_block.py", line 651, in __getitem__
    if self.item is None: x,y = self.x[idxs],self.y[idxs]
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/data_block.py", line 120, in __getitem__
    if isinstance(idxs, Integral): return self.get(idxs)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/vision/data.py", line 270, in get
    fn = super().get(i)
  File "/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/data_block.py", line 75, in get
    return self.items[i]
IndexError: index 0 is out of bounds for axis 0 with size 0

when I check my validation set with
data.valid_ds
I get-

LabelList (8400 items)
x: ImageList
Image (3, 28, 28),Image (3, 28, 28),Image (3, 28, 28),Image (3, 28, 28),Image (3, 28, 28)
y: CategoryList
1,5,0,5,9
Path: MNIST data/train

Your validation set has things in it, but not your test set.