1

helloall this is my first post.

I am currently halfway through a Udemy course titled "Build and train a data model to recognize objects in images!" (https://www.udemy.com/course/pythondatascience/) and am having difficulty with one of the tasks (running on a 2019 MacBook Pro with 10.15.6, using PyCharm IDE).

The task requires to run the code below to open/render an image from the CiFAR10 dataset:

from PIL import Image

labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

from keras.datasets import cifar10
# X_train = images, y_train = labels
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

index = 5
display_image = X_train[index]
display_label = y_train[index][0]

from matplotlib import pyplot as plt

final_image = plt.imshow(display_image)
final_image.show()

However I get the following errors and do not know what to do. I have posted on the course but being 3 years old, I don’t think anyone is keeping up to date with it and am not getting and responses.

/Users/muratuckan/PycharmProjects/TensorFlow/venv/bin/python /Users/muratuckan/PycharmProjects/TensorFlow/ImageRecognitionTrainer.py
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1350, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 950, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1424, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/muratuckan/PycharmProjects/TensorFlow/venv/lib/python3.8/site-packages/tensorflow/python/keras/utils/data_utils.py", line 278, in get_file
    urlretrieve(origin, fpath, dl_progress)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1393, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1353, 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:1123)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/muratuckan/PycharmProjects/TensorFlow/ImageRecognitionTrainer.py", line 19, in <module>
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
  File "/Users/muratuckan/PycharmProjects/TensorFlow/venv/lib/python3.8/site-packages/tensorflow/python/keras/datasets/cifar10.py", line 52, in load_data
    path = get_file( dirname, origin=origin, untar=True)
  File "/Users/muratuckan/PycharmProjects/TensorFlow/venv/lib/python3.8/site-packages/tensorflow/python/keras/utils/data_utils.py", line 282, in get_file
    raise Exception(error_msg.format(origin, e.errno, e.reason))
Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

Process finished with exit code 1

The only information I have found mentions that I might have an unstable internet connection, but that is not the case.

With some of the posts I notice that their run terminal says "Using TensorFlow backend" however mine does not say this, it only says "Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"

Please help. Thank you

its_muri
  • 11
  • 2
  • I can't edit my post, but the start of the post should have said "hi there, this is my first post.." and the word uddery = Udemy (autocorrect) – its_muri Aug 30 '20 at 06:08
  • Please [edit] your question to add some explanation or code instead of using comments as you did. – help-info.de Aug 30 '20 at 07:51
  • The stack traces may seem overwhelming, but just look at the bottom of a block with the `File ...` entries. You're not able to fetch the dataset because of something to do with SSL: `certificate verify failed: unable to get local issuer certificate`. You'll probably find the answer here: https://stackoverflow.com/questions/59832388/why-does-python-fail-when-loading-data-set-load-data – tsj Aug 30 '20 at 19:31
  • 1
    Does this answer your question? [Why does python fail when loading data set ".load\_data()"](https://stackoverflow.com/questions/59832388/why-does-python-fail-when-loading-data-set-load-data) – tsj Aug 30 '20 at 19:32
  • @tsj yes thank you that helped! How do i mark an answer as solved? What I had to do was locate "Install Certificates.command" in python folder (I just searched it using the search) and ran the file. I restarted pycharm and its corrected that error. Thank you!! – its_muri Aug 31 '20 at 03:02

2 Answers2

1

As referenced here, adding the below code before dataset loading worked for me

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
pkumar90
  • 71
  • 4
0

This was answered by @tsj. What I had to do was locate "Install Certificates.command" in python folder (I just searched it using the search) and ran the file. I restarted pycharm and its corrected that error.

its_muri
  • 11
  • 2