Search code examples
pythonpython-3.xmnistmlxtend

How to fix 'No such file or directory' error while loading MNIST dataset


I have downloaded the MNIST training images and labels from yann.lecun.com and unzipped them. I am trying to load them using this code-

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')

However, I get this error -

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'

The directory does exist, and the filenames are correct. How can I fix this?

EDIT: I tried the same with the python-mnist package-

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

I got a similar error-

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'

The error only seems to be with the training-labels file; I tried redownloading the file, but that didn't fix it.

EDIT 2: As requested, here is the output of 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

EDIT 3: Here is the output of print(os.listdir('/python/mnist-files')):

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

I'm totally mystified- I know the directory exists! I can see it when I cd into /python!


Solution

  • There is a difference between a / and ~. By default,

    os.dir('/')
    

    will check at '/'. I guess your file python is in '~' i.e. your home directory.

    You can try this:

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

    Let me know if it helps.