前提・実現したいこと
エラーの原因を知りたい。また、その改善策を知りたい。
発生している問題
jupyter notebook上でCIFAR10の画像を表示しようとしたらエラーが出てしまいました。
本に載っているコードをそのままコピーしているのでコードミスでは無いと思うのですが、原因が分かりません。
該当のソースコード
import numpy as np
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
%matplotlib inline
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train:',X_train.shape, 'y_train:', y_train.shape)
print('X_test :', X_test.shape, 'y_test :', y_test.shape)
num_classes = 10 # 分類先のクラスの数
pos = 1 # 画像の描画位置を保持する変数
for target_class in range(num_classes):
target_idx = [] for i in range(len(y_train)): if y_train[i][0] == target_class: target_idx.append(i) np.random.shuffle(target_idx) # クラスiの画像のインデックスをシャッフル plt.figure(figsize=(20, 20)) # 描画エリアを横25インチ、縦3インチにする for idx in target_idx[:10]: plt.subplot(10, 10, pos) # 10行、10列の描画領域のpos番目の位置を指定 plt.imshow(X_train[idx]) # Matplotlibのimshow()で画像を描画 pos += 1
plt.show()
エラーメッセージ
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
SSLCertVerificationError Traceback (most recent call last)
~\anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1353 try:
-> 1354 h.request(req.get_method(), req.selector, req.data, headers,
1355 encode_chunked=req.has_header('Transfer-encoding'))
~\anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
1254 """Send a complete request to the server."""
-> 1255 self._send_request(method, url, body, headers, encode_chunked)
1256
~\anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
1300 body = _encode(body, 'body')
-> 1301 self.endheaders(body, encode_chunked=encode_chunked)
1302
~\anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked)
1249 raise CannotSendHeader()
-> 1250 self._send_output(message_body, encode_chunked=encode_chunked)
1251
~\anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011
~\anaconda3\lib\http\client.py in send(self, data)
949 if self.auto_open:
--> 950 self.connect()
951 else:
~\anaconda3\lib\http\client.py in connect(self)
1423
-> 1424 self.sock = self._context.wrap_socket(self.sock,
1425 server_hostname=server_hostname)
~\anaconda3\lib\ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,
~\anaconda3\lib\ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1040 self.do_handshake()
1041 except (OSError, ValueError):
~\anaconda3\lib\ssl.py in do_handshake(self, block)
1308 self.settimeout(None)
-> 1309 self._sslobj.do_handshake()
1310 finally:
SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
~\anaconda3\lib\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)
273 try:
--> 274 urlretrieve(origin, fpath, dl_progress)
275 except urllib.error.HTTPError as e:
~\anaconda3\lib\site-packages\keras\utils\data_utils.py in urlretrieve(url, filename, reporthook, data)
81
---> 82 response = urlopen(url, data)
83 with open(filename, 'wb') as fd:
~\anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
223
~\anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525 response = self._open(req, data)
526
~\anaconda3\lib\urllib\request.py in _open(self, req, data)
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 '_open', req)
~\anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
501 func = getattr(handler, meth_name)
--> 502 result = func(*args)
503 if result is not None:
~\anaconda3\lib\urllib\request.py in https_open(self, req)
1396 def https_open(self, req):
-> 1397 return self.do_open(http.client.HTTPSConnection, req,
1398 context=self._context, check_hostname=self._check_hostname)
~\anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1356 except OSError as err: # timeout error
-> 1357 raise URLError(err)
1358 r = h.getresponse()
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)>
During handling of the above exception, another exception occurred:
Exception Traceback (most recent call last)
<ipython-input-3-577dfc82a816> in <module>
5
6 # CIFAR-10データセットをロード
----> 7 (X_train, y_train), (X_test, y_test) = cifar10.load_data()
8 # データの形状を出力
9 print('X_train:',X_train.shape, 'y_train:', y_train.shape)
~\anaconda3\lib\site-packages\keras\datasets\cifar10.py in load_data()
77 dirname = 'cifar-10-batches-py'
78 origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
---> 79 path = get_file(
80 dirname,
81 origin=origin,
~\anaconda3\lib\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)
276 raise Exception(error_msg.format(origin, e.code, e.msg))
277 except urllib.error.URLError as e:
--> 278 raise Exception(error_msg.format(origin, e.errno, e.reason))
279 except (Exception, KeyboardInterrupt) as e:
280 if os.path.exists(fpath):
Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
#その他
長くなってしまい申し訳ありませんが、何か分かる方教えて頂けるとありがたいです。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー