Search code examples
pythonpytorchtorchvision

Torchvision ImageFolder "Could not find any class folder"


The code below

plastic_train_image_folder = torchvision.datasets.ImageFolder(plastic_dir, transform=transforms)

throws the following error:

Could not find any any class folder in /Users/username/Documents/Jupyter/archive/Garbage classification/Garbage classification/plastic.

Yet, there are files there. The code below prints 482.

list_plastic = os.listdir(plastic_dir) 
number_files_plastic = len(list_plastic)
print(number_files_plastic)

Why is this error happening?


Solution

  • As you can see in the documentation, the ImageFolder class expects images to be within directories, one for each class of interest:

    A generic data loader where the images are arranged in this way:

    root/dog/xxx.png
    root/dog/xxy.png
    root/dog/xxz.png
    
    root/cat/123.png
    root/cat/nsdf3.png
    root/cat/asd932_.png
    

    Your images are probably in the root directory, which is not the way it is expecting, hence the error.