Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 208
MVS_beginner Avatar asked Feb 10 '19 05:02

MVS_beginner


4 Answers

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
...
like image 132
youkaichao Avatar answered Oct 29 '22 11:10

youkaichao


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!!

like image 27
Wey Shi Avatar answered Oct 29 '22 10:10

Wey Shi


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.:)

like image 3
Tao Yang Avatar answered Oct 29 '22 11:10

Tao Yang


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.

like image 2
kakrafoon Avatar answered Oct 29 '22 09:10

kakrafoon