Data Loading in Pytorch for a dataset having all the classes in same folder

I am new to deep learning and Pytorch. I have data set of 6000 images that have all four classes in a single folder. I used the following snippet to upload my data.

torchvision.datasets.ImageFolder(root='/content/drive/My Drive/DFU/base_dir/train_dir', transform=None)

I read that for ImageFolder, the images should be organized into sub-folders based on class labels. However, my dataset has all four class images in a single folder. I have a .csv file (Ground_Truth)that contains the one-hot-encoded class label for each image. How to load my dataset to Pytorch?

You should create a custom dataset class, which loads the corresponding csv file and return the tuple <image, label> in the __getitem__ function: Writing Custom Datasets, DataLoaders and Transforms — PyTorch Tutorials 1.8.1+cu102 documentation

@carloalbertobarbano the sample example has all the images of the same types in a single directory. I have multiple classes and all the images are in one directory. I have a .csv file thatGround_Truth_File has an image name column and then their respective one hot encoder.

The sample in the docs shows you how to create your custom dataset by extending all of the required functions. Of course you need to adjust the logic to suit your needs (read the CSV files and return the required attribute in the __getitem__) as I suggested in my previous reply

class DFUDataset(Dataset):
  def __init__(self, csv_file, root_dir, transform=None):
        self.DFU = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform
  def __len__(self):
        return len(self.DFU)
  def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
            img_name = os.path.join(self.root_dir, self.DFU.iloc[idx, 0]) #image names
            image = io.imread(img_name)
            img_label =  os.path.join(self.root_dir, self.DFU.iloc[0, idx]) #image labels
            sample = {'image': image, 'img_label': img_label}
            return sample
DFU_dataset = DFUDataset(csv_file='C:/Users/aleems2/Desktop/dfu/DFUC2021_trainset_210427/DFUC2021_train/Labelled_data_ground_truth.csv',
                                    root_dir="C:/Users/aleems2/Desktop/dfu/DFUC2021_trainset_210427/DFUC2021_train/Labelled_test_images")```


I am debugging but my code does not go to len, get_item function