5

I am running some sample code to test out the installation of Tensorflow for running on a GPU. Python works fine and I can import Tensorflow without any errors.

However, when I try to load a dataset from the TensorFlow website, I run into problems and I receive the error messages below.

The problem is related to some SSL certificates, something I have little knowledge of and therefore I am reaching out for help.

(Relevant) CODE:

import tensorflow as tf
(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()

ERROR MESSAGES:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>

...

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>

...

Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

FULL TRACEBACK:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
Traceback (most recent call last):
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1254, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1300, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1249, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1036, in _send_output
    self.send(msg)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 974, in send
    self.connect()
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1415, in connect
    server_hostname=server_hostname)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 817, in __init__
    self.do_handshake()
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/utils/data_utils.py", line 251, in get_file
    urlretrieve(origin, fpath, dl_progress)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/filip/PycharmProjects/mist_test/mist_test_file.py", line 2, in <module>
    (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/datasets/mnist.py", line 49, in load_data
    file_hash='8a61469f7ea1b51cbae51d4f78837e45')
  File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/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 (_ssl.c:852)

SYSTEM INFO:

# packages in environment at /home/filip/anaconda3/envs/tf:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
absl-py                   0.8.0                    pypi_0    pypi
astor                     0.8.0                    pypi_0    pypi
ca-certificates           2019.5.15                     1  
certifi                   2019.6.16                py36_1  
gast                      0.3.1                    pypi_0    pypi
google-pasta              0.1.7                    pypi_0    pypi
grpcio                    1.23.0                   pypi_0    pypi
h5py                      2.10.0                   pypi_0    pypi
keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.0                    pypi_0    pypi
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 9.1.0                hdf63c60_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
markdown                  3.1.1                    pypi_0    pypi
ncurses                   6.1                  he6710b0_1  
numpy                     1.17.2                   pypi_0    pypi
openssl                   1.1.1d               h7b6447c_0  
pip                       19.2.2                   py36_0  
protobuf                  3.9.1                    pypi_0    pypi
python                    3.6.9                h265db76_0  
readline                  7.0                  h7b6447c_5  
setuptools                41.0.1                   py36_0  
six                       1.12.0                   pypi_0    pypi
sqlite                    3.29.0               h7b6447c_0  
tensorboard               1.14.0                   pypi_0    pypi
tensorflow-estimator      1.14.0                   pypi_0    pypi
tensorflow-gpu            1.14.0                   pypi_0    pypi
termcolor                 1.1.0                    pypi_0    pypi
tk                        8.6.8                hbc83047_0  
werkzeug                  0.15.6                   pypi_0    pypi
wheel                     0.33.4                   py36_0  
wrapt                     1.11.2                   pypi_0    pypi
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               h7b6447c_3
1

1 Answer 1

1

You can add the below code before the download. Worked for the error for a similar download from the internet. For more details please refer this link

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

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