FileNotFoundError: torchvision.datasets.ImageFolder using WSL path

username: Aditya1
cooked: <p>Hi,<br>
I am trying to run some python code in WSL but it seems like it’s unable to find the WSL path?</p>
<pre data-code-wrap="py"><code class="lang-nohighlight">trainset = torchvision.datasets.ImageFolder(root='/mnt/d/Projects/Datasets/CUB_200_2011/train',
                                            transform=transform_train)
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "CUB-200-2011.py", line 44, in &lt;module&gt;
    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.
</code></pre>
<p>However, if I run a simple python console and <code>os.listdir</code> for the same path, it returns results.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; print(len(os.listdir('/mnt/d/Projects/Datasets/CUB_200_2011/train')))
5794
</code></pre>
<p>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.</p>
<p>System info -</p>
<p>Python              3.10<br>
PyTorch            1.12.1<br>
torch                 1.12.1+cu116<br>
torchaudio         0.12.1+cu116<br>
torchvision        0.13.1+cu116</p>

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

username: Aditya1
cooked: <p>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.</p>

username: Luism
cooked: <p>A little bit late, but this might help someone later on.<br>
I had an issue trying to a dataset with ImageFolder. The reason was that I had other folders besides the intended class image folders.<br>
Just a visual reminder</p>
<pre data-code-wrap="Markdown"><code class="lang-Markdown">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

</code></pre>