Search code examples
pytorchdataset

Why is my dataset class giving index out of range errors (Pytorch)?


I am determining why my data set gives IndexError: list index out of range error.

Consider this torch data set:

class MyDataset(Dataset):

def __init__(self, imgs , transform = None):
    self.imgs = imgs
    self.transform = transform or transforms.ToTensor()
    self.class_to_idx = {}

def __getitem__(self, index):
    
    image_path = self.imgs[index]
    name = image_path.split('/D')[1]
    target = name.split('_')[0]
    
    image = Image.open(image_path)
    
    if self.transform is not None:
        image = self.transform(image)

    if target in class_to_idx : 
        target = [class_to_idx[target]]
    else : 
        class_to_idx[target] = (int(target)-1)
        target = [class_to_idx[target]]


    return image , target

def __len__(self):
    return len(self.imgs)

An image_path example: "Train/D01_I_flat_0009.jpg" The dataset has 35 classes: D01...D35

I want to calculate mean and std, but I get the error: enter image description here enter image description here

When I test the dataset, it works correctly:

enter image description here


Solution

  • I found the reason for the error. It was an odd cause! There weren't any other files but .jpg files in the Train folder. However, I applied the following change:

    train_files = glob.glob(train_dir + '/*')
    

    to

    train_files = glob.glob(train_dir + '/*.jpg')
    

    The problem was solved!

    It is worth mentioning that I didn't face this problem on Google Colab, but with Jupyter Notebook on my machine.