Pytorch无法识别数据集的目录

时间:2018-12-24 18:04:07

标签: python python-3.x pytorch

我正在尝试从Mac上的官方PyTorch站点(https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html)运行Deep Convolutional GAN的代码。

当我尝试加载数据时,我不断收到“ FileNotFound”错误。

Random Seed:  999
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-6-1019cc990fb4> in <module>()
 78                                transforms.CenterCrop(image_size),
 79                                transforms.ToTensor(),
---> 80                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
 81                            ]))
 82 # Create the dataloader

/anaconda3/lib/python3.6/site-packages/torchvision/datasets/folder.py in __init__(self, root, transform, target_transform, loader)
176         super(ImageFolder, self).__init__(root, loader, IMG_EXTENSIONS,
177                                           transform=transform,
--> 178                                           target_transform=target_transform)
179         self.imgs = self.samples

/anaconda3/lib/python3.6/site-packages/torchvision/datasets/folder.py in __init__(self, root, loader, extensions, transform, target_transform)
 73 
 74     def __init__(self, root, loader, extensions, transform=None, target_transform=None):
---> 75         classes, class_to_idx = find_classes(root)
 76         samples = make_dataset(root, class_to_idx, extensions)
 77         if len(samples) == 0:

/anaconda3/lib/python3.6/site-packages/torchvision/datasets/folder.py in find_classes(dir)
 21 
 22 def find_classes(dir):
---> 23     classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
 24     classes.sort()
 25     class_to_idx = {classes[i]: i for i in range(len(classes))}

FileNotFoundError: [Errno 2] No such file or directory: 'Users/user1/Downloads/DCGANs/celeba/'

在这里我尝试在dataroot = "Users/user1/Downloads/DCGANs/celeba/"

处加载数据集

数据集是一个包含约200,000张图像的文件夹(名为celeba)。

dataset = dset.ImageFolder(root=dataroot,
                       transform=transforms.Compose([
                           transforms.Resize(image_size),
                           transforms.CenterCrop(image_size),
                           transforms.ToTensor(),
                           transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                       ]))

我在Atom和Jupyter Notebook上都尝试过,但这没什么不同。

非常感谢所有帮助:)

1 个答案:

答案 0 :(得分:1)

可识别的目录结构以/开头。所以我认为,您应该替换

dataroot = "Users/user1/Downloads/DCGANs/celeba/"

通过

dataroot = "/Users/user1/Downloads/DCGANs/celeba/"