加载 MNIST 数据集时如何修复“没有这样的文件或目录”错误

新手上路,请多包涵

我已经从 yann.lecun.com 下载了 MNIST 训练图像和标签并解压缩了它们。我正在尝试使用此代码加载它们-

 from mlxtend.data import loadlocal_mnist

features,labels = loadlocal_mnist(
    images_path='/python/mnist-files/train-images-idx3-ubyte',
    labels_path='/python/mnist-files/train-labels-idx1-ubyte')

但是,我收到此错误 -

 Traceback (most recent call last):
  File "generateClassifier.py", line 12, in <module>
    labels_path='/python/mnist-files/train-labels-idx1-ubyte')
  File "/home/inglorion/.local/lib/python3.6/site-
packages/mlxtend/data/local_mnist.py", line 36, in loadlocal_mnist
    with open(labels_path, 'rb') as lbpath:
FileNotFoundError: [Errno 2] No such file or directory: '/python/mnist-
files/train-labels-idx1-ubyte'

该目录确实存在,并且文件名是正确的。我怎样才能解决这个问题?

编辑:我对 python-mnist package- 进行了同样的尝试

from mnist import MNIST
mndata = MNIST('/python/mnist-files')
features,labels = mndata.load_training()

我遇到了类似的错误-

 Traceback (most recent call last):
  File "generateClassifier.py", line 11, in <module>
    features,labels = mndata.load_training()
  File "/home/inglorion/.local/lib/python3.6/site-packages/mnist/loader.py",
line 126, in load_training
    os.path.join(self.path, self.train_lbl_fname))
  File "/home/inglorion/.local/lib/python3.6/site-packages/mnist/loader.py",
line 247, in load
    with self.opener(path_lbl, 'rb') as file:
  File "/home/inglorion/.local/lib/python3.6/site-packages/mnist/loader.py",
line 239, in opener
    return open(path_fn, *args, **kwargs)
FileNotFoundError: [Errno 2] No such file or directory: '/python/mnist-
files/train-labels-idx1-ubyte'

错误似乎只与训练标签文件有关;我尝试重新下载文件,但没有解决问题。

编辑 2:根据要求,这里是 ls -l /python/mnist-files 的输出

total 46156
-rw-r--r-- 1 inglorion inglorion 47040016 Jul 21  2000 train-images-idx3-
ubyte
-rw-r--r-- 1 inglorion inglorion    60008 Jul 21  2000 train-labels-idx1-
ubyte
-rw-r--r-- 1 inglorion inglorion   147970 Feb  8 22:43 wget-log
-rw-r--r-- 1 inglorion inglorion      682 Feb  9 14:40 wget-log.1

编辑 3:这是 print(os.listdir('/python/mnist-files')) 的输出:

 FileNotFoundError: [Errno 2] No such file or directory: '/python/mnist-files'

我完全迷惑了——我 知道 目录存在!当我进入 /python 时我可以看到它!

原文由 Inglorion 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

/~ 之间是有区别的。默认,

 os.dir('/')

将检查 '/' 。我猜你的文件 python 在 '~' 即你的主目录。

你可以试试这个:

 from os.path import expanduser
home = expanduser("~")+'/python/mnist-files'
mndata = MNIST(home)
features,labels = mndata.load_training()

让我知道是否有帮助。

原文由 Ashutosh Chapagain 发布,翻译遵循 CC BY-SA 4.0 许可协议

For me it helped to rename the file to train-images.idx3-ubyte instead of train-images-idx3-ubyte (the - after images got changed to a . )。

原文由 Marc Dirven 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题