Getting a 'KeyError" while trying to access a Custom Dataset
I am trying to learn PyTorch and make use of a custom dataset. Code Credit - https://github.com/vineeth2309/Custom-Dataset-and-Dataloader-in-Torch.
However, when I run the code I get a 'KeyError'.
import glob
import cv2
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader


class CustomDataset(Dataset):
    def __init__(self):
        self.imgs_path = &quot;Dog_Cat_Dataset/&quot;
        file_list = glob.glob(self.imgs_path + &quot;*&quot;)
        print(file_list)
        self.data = []
        for class_path in file_list:
            class_name = class_path.split(&quot;/&quot;)[-1]
            for img_path in glob.glob(class_path + &quot;/*.jpeg&quot;):
                self.data.append([img_path, class_name])
        print(self.data)
        self.class_map = {&quot;dogs&quot; : 0, &quot;cats&quot;: 1}
        self.img_dim = (416, 416)
    
    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        img_path, class_name = self.data[idx]
        img = cv2.imread(img_path)
        img = cv2.resize(img, self.img_dim)
        class_id = self.class_map[class_name]
        img_tensor = torch.from_numpy(img)
        img_tensor = img_tensor.permute(2, 0, 1)
        class_id = torch.tensor([class_id])
        return img_tensor, class_id

if __name__ == &quot;__main__&quot;:
    dataset = CustomDataset()
    print (dataset)
    data_loader = DataLoader(dataset, batch_size=4, shuffle=True)
    print (data_loader)
    for imgs, labels in data_loader:
        print(&quot;Batch of images has shape: &quot;,imgs.shape)
        print(&quot;Batch of labels has shape: &quot;, labels.shape)

Stack-Trace :
C:\Users\parag\anaconda3\envs\tf-gpu\python.exe C:/Users/parag/PycharmProjects/Custom-Dataset-and-Dataloader-in-Torch/Main.py
['Dog_Cat_Dataset\\cats', 'Dog_Cat_Dataset\\dogs']
[['Dog_Cat_Dataset\\cats\\1.jpeg', 'Dog_Cat_Dataset\\cats'], ['Dog_Cat_Dataset\\cats\\2.jpeg', 'Dog_Cat_Dataset\\cats'], ['Dog_Cat_Dataset\\cats\\3.jpeg', 'Dog_Cat_Dataset\\cats'], ['Dog_Cat_Dataset\\cats\\4.jpeg', 'Dog_Cat_Dataset\\cats'], ['Dog_Cat_Dataset\\cats\\5.jpeg', 'Dog_Cat_Dataset\\cats'], ['Dog_Cat_Dataset\\dogs\\1.jpeg', 'Dog_Cat_Dataset\\dogs'], ['Dog_Cat_Dataset\\dogs\\2.jpeg', 'Dog_Cat_Dataset\\dogs'], ['Dog_Cat_Dataset\\dogs\\3.jpeg', 'Dog_Cat_Dataset\\dogs'], ['Dog_Cat_Dataset\\dogs\\4.jpeg', 'Dog_Cat_Dataset\\dogs'], ['Dog_Cat_Dataset\\dogs\\5.jpeg', 'Dog_Cat_Dataset\\dogs']]
&lt;__main__.CustomDataset object at 0x00000254C0568FD0&gt;
&lt;torch.utils.data.dataloader.DataLoader object at 0x00000254C250A7F0&gt;
Traceback (most recent call last):
  File &quot;C:\Users\parag\PycharmProjects\Custom-Dataset-and-Dataloader-in-Torch\Main.py&quot;, line 40, in &lt;module&gt;
    for imgs, labels in data_loader.dataset:
  File &quot;C:\Users\parag\PycharmProjects\Custom-Dataset-and-Dataloader-in-Torch\Main.py&quot;, line 29, in __getitem__
    class_id = self.class_map[class_name]
KeyError: 'Dog_Cat_Dataset\\cats'

Process finished with exit code 1

[My Folder Structure][1]
I have tried, but not able to resolve the error. Could someone please help me with this?
[1]: https://i.stack.imgur.com/1giJw.png

You've used forward slash when it should be backslash:
    for class_path in file_list:
        class_name = class_path.split(&quot;\\&quot;)[-1]
        for img_path in glob.glob(class_path + &quot;\*.jpeg&quot;):
            self.data.append([img_path, class_name])

I'm guessing you're running on windows when the example is from linux.
