Ali Fakhry
Ali Fakhry

Reputation: 75

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?

Upvotes: 4

Views: 29199

Answers (2)

ghiles....git
ghiles....git

Reputation: 1

When you get it the path, it makes sure that there ia a directory in it:

def find_classes(directory: str) -> Tuple[List[str], Dict[str, int]]:
"""Finds the class folders in a dataset.

See :class:`DatasetFolder` for details.
"""
classes = sorted(entry.name for entry in os.scandir(directory) if entry.is_dir())
if not classes:
    raise FileNotFoundError(f"Couldn't find any class folder in {directory}.")

class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
return classes, class_to_idx
  • So if your dataset is for exemple in /kaggle/dataset/women-clothes then yo want to it it /kaggle/dataset as a path directory and not /kaggle/dataset/dataset/women-clothes.

Upvotes: 0

Berriel
Berriel

Reputation: 13641

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.

Upvotes: 9

Related Questions