I wrote a CustomImageDataset class, it now allows me to access image file names, but I now got another problem - it started to be way slower than when I used datasets.ImageFolder with torch.utils.data.DataLoader
Could you please advise what should I change to make it faster? (and one more problem I have is that I can’t use big batch size as it doesn’t fit in GPU memory despite that I do del my_vars and torch.cuda.empty_cache()
class CustomImageDataset(Dataset):
    def __init__(self, annotations_file): 
        self.img_labels = annotations_file

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

    def __getitem__(self, idx, idx2):
        img_path = self.img_labels[self.img_labels.columns[0]][idx:idx2]
        all_imgs = np.empty((0, 3, img_width, img_height), float)
        for imid in img_path:
            image = Image.open(imid)
            image = transforms.Scale((img_width, img_height), interpolation=2)(image)
            image = transforms.ToTensor()(image)
            image = image.unsqueeze(0)
            all_imgs = np.append(all_imgs, image, axis=0)

        label = torch.tensor(self.img_labels['class'][idx:idx2].values)
        all_imgs = torch.tensor(all_imgs)
        return all_imgs, label, img_path

df = pd.DataFrame(pics, columns = ['img_paths'])
df['class'] = df['img_paths'].apply(lambda row: row.split('/')[3]).astype('category').cat.codes.astype(int)
classes = np.unique(df['class'])
df = shuffle(df)
train_data = df[0:(len(df)/ /10)*7]
val_data = df[len(train_data):(len(df)/ /10)*9]
test_data =  df[len(train_data)+len(val_data):]
train_data = CustomImageDataset(train_data)
val_data = CustomImageDataset(val_data)
test_data = CustomImageDataset(test_data)