加载MNIST数据集时如何解决“无此类文件或目录”错误

时间:2019-02-08 17:47:32

标签: python python-3.x mnist mlxtend

我已经从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软件包也进行了尝试-

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时就可以看到它!

3 个答案:

答案 0 :(得分:1)

对我来说,它有助于将文件重命名为train-images.idx3-ubyte而不是train-images-idx3-ubyte-之后的images更改为.)。

答案 1 :(得分:0)

您可以尝试使用此代码。

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', validation_size=0)

我已经执行了代码,效果很好!!希望对您有所帮助。

答案 2 :(得分:0)

/ 之间是有区别的。默认情况下,

os.dir('/')

将在 '/' 进行检查。我猜您的文件python位于 '~' 中,即您的主目录中。

您可以尝试以下方法:

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

让我知道是否有帮助。