火炬视觉 ImageFolder "Could not find any class folder"

Torchvision ImageFolder "Could not find any class folder"

下面的代码

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

抛出以下错误:

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

然而,那里有文件。下面的代码打印 482.

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

为什么会出现这个错误?

如您在 documentation 中所见,ImageFolder class 期望图像位于目录中,每个感兴趣的 class 一张:

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

您的图像可能在根目录中,这不是它期望的方式,因此 error