Load csv and Image dataset in pytorch
I am doing image classification with PyTorch. I have a separate Images folder and train and test csv file with images ids and labels . I don’t have any an idea about how to combine those images and ID and converting into tensors.

train.csv : contains all ID of Image like 4325.jpg, 2345.jpg,…so on and contains Labels like cat,dog.
Image_data : contains all the images of with ID name.


You can create custom dataset class by inherting pytorch's torch.utils.data.Dataset.
The assumption for the following custom dataset class is 

csv file format is 






filename
label




4325.jpg
cat


2345.jpg
dog





All images are inside images folder.

class CustomDataset(torch.utils.data.Dataset):
    def __init__(self, csv_path, images_folder, transform = None):
        self.df = pd.read_csv(csv_path)
        self.images_folder = images_folder
        self.transform = transform
        self.class2index = {&quot;cat&quot;:0, &quot;dog&quot;:1}

    def __len__(self):
        return len(self.df)
    def __getitem__(self, index):
        filename = self.df[index, &quot;FILENAME&quot;]
        label = self.class2index[self.df[index, &quot;LABEL&quot;]]
        image = PIL.Image.open(os.path.join(self.images_folder, filename))
        if self.transform is not None:
            image = self.transform(image)
        return image, label
        

Now you can use this class to load the training and test dataset using both csv file and image folder.

train_dataset = CustomDataset(&quot;path - to - train.csv&quot;, &quot;path - to - images - folder&quot;  )
test_dataset = CustomDataset(&quot;path - to - test.csv&quot;, &quot;path - to - images - folder&quot;  )


image, label = train_dataset[0]

