3

I am trying to import mnist dataset using keras code in Macbook. but it is giving the error below.

# Loading the data
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

The error I am getting:

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-11-fdb6855f8337> in <module>()
      2 from keras.datasets import mnist
      3 
----> 4 (x_train, y_train), (x_test, y_test) = mnist.load_data()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/datasets/mnist.py in load_data(path)
     21     path = get_file(path,
     22                     origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
---> 23                     file_hash='8a61469f7ea1b51cbae51d4f78837e45')
     24     f = np.load(path)
     25     x_train, y_train = f['x_train'], f['y_train']

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/utils/data_utils.py in get_file(fname, origin, untar, md5_hash, file_hash, cache_subdir, hash_algorithm, extract, archive_format, cache_dir)
    222                 urlretrieve(origin, fpath, dl_progress)
    223             except URLError as e:
--> 224                 raise Exception(error_msg.format(origin, e.errno, e.reason))
    225             except HTTPError as e:
    226                 raise Exception(error_msg.format(origin, e.code, e.msg))

Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

Thanks in advance

(Note: I tried to remove files from .keras/datasets/ as in said in this issue )

1

3 Answers 3

3

On the mac, go to the keras folder. Should be at ~/.keras/

There will be a folder called datasets there. Download the dataset from here and move it to that folder. Now run that the same code.

0
3

If you are having this issue on a Mac it is because Python3.6 on Mac has no certificates and fails to verify the certificate from Github.

Run the following command to install certificates:

/Applications/Python 3.6/Install Certificates.command

Check these links for more information on this issue

https://github.com/ageron/handson-ml/issues/46

urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error

1

I assume that you are not behind a proxy (otherwise, that is likely the issue). I'd bet this is a problem with certificates installed on your machine.

If this is python 3.6, try installing certifi and let me know if that resolves the issue.

If you want a slightly questionable bludgeon of a solution, you could try setting an environmental variable PYTHONHTTPSVERIFY=0

Let me know if either of those methods are effective. Happy to continue exploring the problem.

Download in the browser worked fine (likely a proxy or cert issue in terminal). He managed to fix it by manually placing the downloaded file where expected by the installer.

7
  • I tried the certifi and it didn't work, But I don't know how to change an environmental variable. Could you please explain it? Or would you recommend something else? Commented Mar 4, 2018 at 0:30
  • Are you running the python script from terminal on the mac? You can type 'export PYTHONHTTPSVERIFY=0'. Then try calling it again with python NAMEOFSCRIPT.py Commented Mar 4, 2018 at 0:51
  • Another question is: can you just go to s3.amazonaws.com/img-datasets/mnist.npz in your browser? That would be a good indication of whether this might be a problem with proxy connectivity Commented Mar 4, 2018 at 0:54
  • When I open the links it downloads it but when I run the script it doesn't work. Commented Mar 4, 2018 at 0:59
  • Yes, I tried both terminal and notebook, both of them didn't work Commented Mar 4, 2018 at 1:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.