tensorflow: Failing to download MNIST dataset at load_data()

System information

  • OS Platform and Distribution: macOS version10.15
  • TensorFlow version: 2.0
  • Python version: 3.7
  • Installed using: pip install
  • Bazel version (if compiling from source): 1.0.0

I have tried the first beginner example:

`from future import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

mnist = tf.keras.datasets.mnist

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

model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation=‘relu’), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation=‘softmax’) ])

model.compile(optimizer=‘adam’, loss=‘sparse_categorical_crossentropy’, metrics=[‘accuracy’])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test, y_test, verbose=2)`

But it always seems to break at (x_train, y_train), (x_test, y_test) = mnist.load_data(). The programs run fine on Colaboratory but if I try to run in locally on Terminal, it fails. Seems to be a certification issue.

I have download installed everything, even upgraded just to be sure.

Here are the logs that are printed out:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz Traceback (most recent call last): File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 1317, in do_open encode_chunked=req.has_header(‘Transfer-encoding’)) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 1229, in request self._send_request(method, url, body, headers, encode_chunked) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 1275, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 1224, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 1016, in _send_output self.send(msg) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 956, in send self.connect() File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 1392, in connect server_hostname=server_hostname) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”, line 412, in wrap_socket session=session File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”, line 853, in _create self.do_handshake() File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”, line 1117, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/Users/DanialZikri/venv37/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/data_utils.py”, line 251, in get_file urlretrieve(origin, fpath, dl_progress) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 222, in urlopen return opener.open(url, data, timeout) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 525, in open response = self._open(req, data) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 543, in _open ‘_open’, req) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 503, in _call_chain result = func(*args) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 1360, in https_open context=self._context, check_hostname=self._check_hostname) File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py”, line 1319, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/Users/DanialZikri/Documents/TensorflowFirst.py”, line 7, in <module> (x_train, y_train), (x_test, y_test) = mnist.load_data() File “/Users/DanialZikri/venv37/lib/python3.7/site-packages/tensorflow_core/python/keras/datasets/mnist.py”, line 50, in load_data ‘731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1’) File “/Users/DanialZikri/venv37/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/data_utils.py”, line 255, in get_file raise Exception(error_msg.format(origin, e.errno, e.reason)) Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None – [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23

Commits related to this issue

Most upvoted comments

oh my bad…I forgot an import statement there…

import requests before the call… so, it should be…

import requests
requests.packages.urllib3.disable_warnings()
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

anyways…happy that it worked 😃

Incase of linux…

go to .local/python3.X/lib/python3.6/site-packages/keras/utils/data_utils.py

and below import statements add these----

requests.packages.urllib3.disable_warnings()
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

now try new instance of python and …hopefully it works 😸

Seems like this might be a duplicate of https://github.com/tensorflow/tensorflow/issues/10779. The problem may be that you don’t have the appropriate certificates installed for python 3. On OSX you can install them with:

/Applications/Python\ 3.6/Install\ Certificates.command

Remember to replace the python version with your own version.

On Mac, for me, the file was located at ~/Library/Python/3.7/lib/python/site-packages/tensorflow_core/python/keras/utils/data_utils.py (not in the keras folder)

On Mac : go to Applications then Python and Install certificates.command. fix it

Working with virtualenv: env/lib/python3.7/site-packages/tensorflow_core/python/keras/utils/data_utils.py

/Install\ Certificates.command

This worked for me too in Mac I ran:

/Applications/Python\ 3.10/Install\ Certificates.command

Thanks a lot! It works great with a one change, I had to remove

requests.packages.urllib3.disable_warnings()

because the name requests was never specified. But besides that, it worked!!

on Linux, Ubuntu

1- Download file https://s3.amazonaws.com/img-datasets/mnist.npz OR https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz from another computer with proper (proxy) access.

2- Move mnist.npz to “~/.keras/datasets/” directory

Finish. now this code will work: (X_train, y_train), (X_test, y_test) = mnist.load_data()

That works! Installing python certificates solve my problem. Thanks

Awesome ! This really helped. Can I not add this piece of code into my local program rather than modifying core programs of keras?

On Mac : go to Applications then Python and Install certificates.command. fix it

you save me.

Awesome ! This really helped. Can I not add this piece of code into my local program rather than modifying core programs of keras?

Yes you can ! in my case, just paste everything from import ssl on before you load the database.

Why is this closed. This seems to be a common issue. @oanush

for me(MAC) it was ~/Library/Python/3.7/lib/python/site-packages/tensorflow/python/keras/utils/data_utils.py

For some of you the path to the file might be: /venv/lib/python3.8/site-packages/tensorflow/python/keras/utils