FileNotFoundError: torchvision.datasets.ImageFolder using WSL path

Hi,
I am trying to run some python code in WSL but it seems like it’s unable to find the WSL path?

trainset = torchvision.datasets.ImageFolder(root='/mnt/d/Projects/Datasets/CUB_200_2011/train',
                                            transform=transform_train)
Traceback (most recent call last):
  File "CUB-200-2011.py", line 44, in <module>
    trainset = torchvision.datasets.ImageFolder(root='/mnt/d/Projects/Datasets/CUB_200_2011/train',
  File "/mnt/d/Projects/Papers/Mutual-Channel-Loss/cub-env/lib/python3.8/site-packages/torchvision/datasets/folder.py", line 310, in __init__
    super().__init__(
  File "/mnt/d/Projects/Papers/Mutual-Channel-Loss/cub-env/lib/python3.8/site-packages/torchvision/datasets/folder.py", line 145, in __init__
    classes, class_to_idx = self.find_classes(self.root)
  File "/mnt/d/Projects/Papers/Mutual-Channel-Loss/cub-env/lib/python3.8/site-packages/torchvision/datasets/folder.py", line 219, in find_classes
    return find_classes(directory)
  File "/mnt/d/Projects/Papers/Mutual-Channel-Loss/cub-env/lib/python3.8/site-packages/torchvision/datasets/folder.py", line 43, in find_classes
    raise FileNotFoundError(f"Couldn't find any class folder in {directory}.")
FileNotFoundError: Couldn't find any class folder in /mnt/d/Projects/Datasets/CUB_200_2011/train.

However, if I run a simple python console and os.listdir for the same path, it returns results.

>>> print(len(os.listdir('/mnt/d/Projects/Datasets/CUB_200_2011/train')))
5794

I am not sure if this is a python issue not recognizing WSL file paths, or a pytorch issue - I have previously developed on WSL and not run into this issue.

System info -

Python 3.10
PyTorch 1.12.1
torch 1.12.1+cu116
torchaudio 0.12.1+cu116
torchvision 0.13.1+cu116

What exactly is stored in this path? ImageFolder expects subfolders for each class containing the images. Your test code shows that 5794 entries are detected, but in case these are not folders, ImageFolder would still fail.

1 Like

This is probably it! the entries are not folders, they are just raw images. On reading the error again, β€œcouldn’t find any class folder” this error makes more sense. It’s looking for images split by folders per class.

A little bit late, but this might help someone later on.
I had an issue trying to a dataset with ImageFolder. The reason was that I had other folders besides the intended class image folders.
Just a visual reminder

Project_Folder/
β”œβ”€ data/
β”‚  β”œβ”€ train/
β”‚  β”‚  β”œβ”€ Class_01/
β”‚  β”‚  β”‚  β”œβ”€ Class_01_train_001.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_01_train_002.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_01_train_003.jpg
β”‚  β”‚  β”‚
β”‚  β”‚  β”œβ”€ Class_02/
β”‚  β”‚  β”‚  β”œβ”€ Class_02_train_001.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_02_train_002.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_02_train_003.jpg
β”‚  β”‚  β”‚
β”‚  β”‚  β”œβ”€ Class_n/
β”‚  β”‚  β”‚  β”œβ”€ Class_n_train_001.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_n_train_002.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_n_train_003.jpg
β”‚  β”‚  β”‚
β”‚  β”œβ”€ test/
β”‚  β”‚  β”œβ”€ Class_01/
β”‚  β”‚  β”‚  β”œβ”€ Class_01_test_001.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_01_test_002.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_01_test_003.jpg
β”‚  β”‚  β”‚
β”‚  β”‚  β”œβ”€ Class_n/
β”‚  β”‚  β”‚  β”œβ”€ Class_n_test_001.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_n_test_002.jpg
β”‚  β”‚  β”‚  β”œβ”€ Class_n_test_003.jpg
β”œβ”€ folder_02/
β”œβ”€ requirements.txt
β”œβ”€ README.md