Not able to evaluate my fast-ai model on the test set, gives IndexError: list index out of range

147 Views Asked by At

This is an image classification task. I've a folder 'Images', and within this folder, I've three subfolders, 'train', 'valid' and 'test'. Furhter, all these subfolders train, test and valid have 10 subfolders which are labelled as 'category 1', 'category 2',....'cat 10'. And these folders have the images stored in them.

I've been using fast-ai to load the images using dataloaders, then train them and then trying to evaluate them on the test set. It is working fine on the validation set, but on test set it gives error. Here's the code -

import os
from pathlib import Path
from fastai import *
from fastai.vision import *
from fastai.vision.all import *

path = Path('/content/drive/MyDrive/Colab_data/ISCC/data_Images_10_SCC')
bs = 32
model_id = 'model-1'
model_dir = 'model/resnet18'

tfms = []
size = 399

data = ImageDataLoaders.from_folder(path, 
                                     ds_tfms=tfms, 
                                     bs=bs, 
                                     size=size, 
                                     seed=123, 
                                     train='train', 
                                     valid='valid', 
                                     test='test')

learn = cnn_learner(data, models.resnet18, metrics=[error_rate], model_dir=model_dir)
learn.metrics=[accuracy, Precision(average='macro'), Recall(average='macro'), FBeta(beta=1, average='macro')]

learn.lr_find()

learn.fit_one_cycle(8,5e-3)

preds, targets = learn.get_preds(ds_idx=2)  # Get predictions on the test set

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-18-900b0fa94c58> in <cell line: 2>()
      1 # Assuming 'learn' is your Learner object
----> 2 preds, targets = learn.get_preds(ds_idx=2)  # Get predictions on the test set
      3 pred_labels = torch.argmax(preds, dim=1)  # Convert predictions to class labels (if needed)
      4 true_labels = targets  # Assuming you have the true labels for the test set

1 frames
/usr/local/lib/python3.10/dist-packages/fastai/learner.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, act, inner, reorder, cbs, **kwargs)
    292         **kwargs
    293     )-> tuple:
--> 294         if dl is None: dl = self.dls[ds_idx].new(shuffle=False, drop_last=False)
    295         else:
    296             try: len(dl)

/usr/local/lib/python3.10/dist-packages/fastai/data/core.py in __getitem__(self, i)
    209         if device is not None and (loaders!=() and hasattr(loaders[0],'to')): self.device = device
    210 
--> 211     def __getitem__(self, i): return self.loaders[i]
    212     def __len__(self): return len(self.loaders)
    213     def new_empty(self):

IndexError: list index out of range
0

There are 0 best solutions below