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

谁说胖子不能爱 提交于 2020-08-04 05:29:12

问题


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)

回答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
...



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




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




回答4:


See the documentation of ImageFolder dataset to see how this dataset class expects the images to be organized into subfolders under `./Dataset/images' according to image classes. Make sure your images adhere to this order.




回答5:


Apparently, the solution is just making the picture name alpha-numeric. They may be another solution but this work.




回答6:


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.



来源:https://stackoverflow.com/questions/54613573/runtimeerror-found-0-files-in-subfolders-of-error-about-subfolder-in-pytor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!