"RuntimeError: Found 0 files in subfolders of ".. Error about subfolder in Pytorch

13,523

Solution 1

Can you post the structure of your files? In your case, it is supposed to be:

img_dir
|_class1
  |_a.jpg
  |_b.jpg
|_class2
  |_a.jpg
  |_b.jpg
...

Solution 2

I met the same problem when using celebA, including 200,000 images. As we can see there are many images. But in a small sample situation (I tried 20 images), I checked, the error will not be raised, which means we can read images successfully. But when the number grows, we should use other methods.

I solved the problem according to this website. Thanks to QimingChen Github solution

Simply, adding another folder named 1 (/train/--->train/1/) in the original folder will enable our program to work, without changing the path. That's because when facing large datasets, images should be sorted in subfolders of different classes.

The Original answer on Github:

Let's say I am going to use ImageFolder("/train/") to read jpg files in folder train. The file structure is /train/ -- 1.jpg -- 2.jpg -- 3.jpg

I failed to load them, leading to errors: RuntimeError: Found 0 images in subfolders of: ./data Supported image extensions are: .jpg,.JPG,.jpeg,.JPEG,.png,.PNG,.ppm,.PPM,.bmp,.BMP

I read the solution above and tried tens of times. When I changed the structure to /train/1/

-- 1.jpg -- 2.jpg -- 3.jpg

But the read in code is still -- ImageFolder("/train/"), IT WORKS.

It seems like the program tends to recursively read in files, that is convenient in some cases.

Hope this would help!!

Solution 3

According to the rules of the DataLoader in pytorch you should choose the the superior path of the image path. That means if your images locate in './Dataset/images/', the path of the data loader should be './Dataset' instead. I hope it can fix your bug.:)

Solution 4

You can modify the ImageFolder class to get to the root folder directly (without subfolders):

class ImageFolder(Dataset):
    def __init__(self, root, transform=None):
        #Call make_dataset to collect files. 
        self.samples = make_dataset(opt.dataroot)
        self.imgs = self.samples
        self.transformA = transformA

        ...

We call the make_dataset method to collect our files:

def make_dataset(dir):
    import os
    images = []
    d = os.path.expanduser(dir)

    if not os.path.exists(dir):
        print('path does not exist')

    for root, _, fnames in sorted(os.walk(d)):
        for fname in sorted(fnames):
            path = os.path.join(root, fname)
            images.append(path)
    return images    

All the action takes place in the loop containing os.walk. Here, the files are collected from the 'root' directory, which we specify as the directory containing our files.

Share:
13,523
MVS_beginner
Author by

MVS_beginner

Updated on July 06, 2022

Comments

  • MVS_beginner
    MVS_beginner almost 2 years

    I'm based on Window 10, Jupyter Notebook, Pytorch 1.0, Python 3.6.x currently.

    At first I confirm to the correct path of files using this code : print(os.listdir('./Dataset/images/')).

    and I could check that this path is correct.

    but I met Error :

    RuntimeError: Found 0 files in subfolders of: ./Dataset/images/ Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif"

    What is the matter? Could you suggest a solution?

    I tried to ./dataset/1/images like this method. but the result was same....

    img_dir = './Dataset/images/'
    img_data = torchvision.datasets.ImageFolder(os.path.join(img_dir), transforms.Compose([
                transforms.Scale(256),
                transforms.RandomResizedCrop(224),
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                ]))
    img_batch = data.DataLoader(img_data, batch_size=batch_size,
                                   shuffle = True, drop_last=True)
    
  • MVS_beginner
    MVS_beginner about 5 years
    Thanks your comment ! I catched error. but I cannt understand about this solution. Could you explain it for me ?? Thanks !
  • MVS_beginner
    MVS_beginner about 5 years
    Yeh. Definitely, right ! I search other documents, then i found same conclusion like your comment :) Pytorch ImageFolder is expect to Class of files! Thank you for your comment !!
  • MisterGeeky
    MisterGeeky about 5 years
    downvoted - faced same issue but we had class of files 'test' and 'train'