通过 torchvision 下载 pytorch 数据集时出现 SSLCertVerificationError
SSLCertVerificationError when downloading pytorch datasets via torchvision
我无法从 pytorch 下载 CIFAR-10 数据集。大多数情况下,它似乎是一些我真的不知道如何解释的 SSL 错误。我也尝试过将根目录更改为其他各种文件夹,但其中 none 有效。我想知道这是否是我这边的权限类型设置,但我没有经验。希望能帮助解决这个问题!
执行的代码在这里:
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=1)
错误重现于此:
---------------------------------------------------------------------------
SSLCertVerificationError Traceback (most recent call last)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1354, in AbstractHTTPHandler.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'))
1356 except OSError as err: # timeout error
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1256, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
1255 """Send a complete request to the server."""
-> 1256 self._send_request(method, url, body, headers, encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1302, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
1301 body = _encode(body, 'body')
-> 1302 self.endheaders(body, encode_chunked=encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1251, in HTTPConnection.endheaders(self, message_body, encode_chunked)
1250 raise CannotSendHeader()
-> 1251 self._send_output(message_body, encode_chunked=encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1011, in HTTPConnection._send_output(self, message_body, encode_chunked)
1010 del self._buffer[:]
-> 1011 self.send(msg)
1013 if message_body is not None:
1014
1015 # create a consistent interface to message_body
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:951, in HTTPConnection.send(self, data)
950 if self.auto_open:
--> 951 self.connect()
952 else:
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1425, in HTTPSConnection.connect(self)
1423 server_hostname = self.host
-> 1425 self.sock = self._context.wrap_socket(self.sock,
1426 server_hostname=server_hostname)
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:500, in SSLContext.wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
494 def wrap_socket(self, sock, server_side=False,
495 do_handshake_on_connect=True,
496 suppress_ragged_eofs=True,
497 server_hostname=None, session=None):
498 # SSLSocket class handles server_hostname encoding before it calls
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,
502 server_side=server_side,
503 do_handshake_on_connect=do_handshake_on_connect,
504 suppress_ragged_eofs=suppress_ragged_eofs,
505 server_hostname=server_hostname,
506 context=self,
507 session=session
508 )
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:1040, in SSLSocket._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):
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:1309, in SSLSocket.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:1131)
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
Input In [8], in <module>
----> 1 trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
2 trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=1)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\cifar.py:66, in CIFAR10.__init__(self, root, train, transform, target_transform, download)
63 self.train = train # training set or test set
65 if download:
---> 66 self.download()
68 if not self._check_integrity():
69 raise RuntimeError('Dataset not found or corrupted.' +
70 ' You can use download=True to download it')
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\cifar.py:144, in CIFAR10.download(self)
142 print('Files already downloaded and verified')
143 return
--> 144 download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.tgz_md5)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:427, in download_and_extract_archive(url, download_root, extract_root, filename, md5, remove_finished)
424 if not filename:
425 filename = os.path.basename(url)
--> 427 download_url(url, download_root, filename, md5)
429 archive = os.path.join(download_root, filename)
430 print("Extracting {} to {}".format(archive, extract_root))
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:130, in download_url(url, root, filename, md5, max_redirect_hops)
127 _download_file_from_remote_location(fpath, url)
128 else:
129 # expand redirect chain if needed
--> 130 url = _get_redirect_url(url, max_hops=max_redirect_hops)
132 # check if file is located on Google Drive
133 file_id = _get_google_drive_file_id(url)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:78, in _get_redirect_url(url, max_hops)
75 headers = {"Method": "HEAD", "User-Agent": USER_AGENT}
77 for _ in range(max_hops + 1):
---> 78 with urllib.request.urlopen(urllib.request.Request(url, headers=headers)) as response:
79 if response.url == url or response.url is None:
80 return url
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:222, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:525, in OpenerDirector.open(self, fullurl, data, timeout)
522 req = meth(req)
524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525 response = self._open(req, data)
527 # post-process response
528 meth_name = protocol+"_response"
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:542, in OpenerDirector._open(self, req, data)
539 return result
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 '_open', req)
544 if result:
545 return result
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:502, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
500 for handler in handlers:
501 func = getattr(handler, meth_name)
--> 502 result = func(*args)
503 if result is not None:
504 return result
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1397, in HTTPSHandler.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)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1357, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
1354 h.request(req.get_method(), req.selector, req.data, headers,
1355 encode_chunked=req.has_header('Transfer-encoding'))
1356 except OSError as err: # timeout error
-> 1357 raise URLError(err)
1358 r = h.getresponse()
1359 except:
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)>
关闭ssl验证。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
我无法从 pytorch 下载 CIFAR-10 数据集。大多数情况下,它似乎是一些我真的不知道如何解释的 SSL 错误。我也尝试过将根目录更改为其他各种文件夹,但其中 none 有效。我想知道这是否是我这边的权限类型设置,但我没有经验。希望能帮助解决这个问题!
执行的代码在这里:
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=1)
错误重现于此:
---------------------------------------------------------------------------
SSLCertVerificationError Traceback (most recent call last)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1354, in AbstractHTTPHandler.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'))
1356 except OSError as err: # timeout error
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1256, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
1255 """Send a complete request to the server."""
-> 1256 self._send_request(method, url, body, headers, encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1302, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
1301 body = _encode(body, 'body')
-> 1302 self.endheaders(body, encode_chunked=encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1251, in HTTPConnection.endheaders(self, message_body, encode_chunked)
1250 raise CannotSendHeader()
-> 1251 self._send_output(message_body, encode_chunked=encode_chunked)
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1011, in HTTPConnection._send_output(self, message_body, encode_chunked)
1010 del self._buffer[:]
-> 1011 self.send(msg)
1013 if message_body is not None:
1014
1015 # create a consistent interface to message_body
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:951, in HTTPConnection.send(self, data)
950 if self.auto_open:
--> 951 self.connect()
952 else:
File C:\ProgramData\Miniconda3\envs\pDL\lib\http\client.py:1425, in HTTPSConnection.connect(self)
1423 server_hostname = self.host
-> 1425 self.sock = self._context.wrap_socket(self.sock,
1426 server_hostname=server_hostname)
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:500, in SSLContext.wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
494 def wrap_socket(self, sock, server_side=False,
495 do_handshake_on_connect=True,
496 suppress_ragged_eofs=True,
497 server_hostname=None, session=None):
498 # SSLSocket class handles server_hostname encoding before it calls
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,
502 server_side=server_side,
503 do_handshake_on_connect=do_handshake_on_connect,
504 suppress_ragged_eofs=suppress_ragged_eofs,
505 server_hostname=server_hostname,
506 context=self,
507 session=session
508 )
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:1040, in SSLSocket._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):
File C:\ProgramData\Miniconda3\envs\pDL\lib\ssl.py:1309, in SSLSocket.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:1131)
During handling of the above exception, another exception occurred:
URLError Traceback (most recent call last)
Input In [8], in <module>
----> 1 trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
2 trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=1)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\cifar.py:66, in CIFAR10.__init__(self, root, train, transform, target_transform, download)
63 self.train = train # training set or test set
65 if download:
---> 66 self.download()
68 if not self._check_integrity():
69 raise RuntimeError('Dataset not found or corrupted.' +
70 ' You can use download=True to download it')
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\cifar.py:144, in CIFAR10.download(self)
142 print('Files already downloaded and verified')
143 return
--> 144 download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.tgz_md5)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:427, in download_and_extract_archive(url, download_root, extract_root, filename, md5, remove_finished)
424 if not filename:
425 filename = os.path.basename(url)
--> 427 download_url(url, download_root, filename, md5)
429 archive = os.path.join(download_root, filename)
430 print("Extracting {} to {}".format(archive, extract_root))
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:130, in download_url(url, root, filename, md5, max_redirect_hops)
127 _download_file_from_remote_location(fpath, url)
128 else:
129 # expand redirect chain if needed
--> 130 url = _get_redirect_url(url, max_hops=max_redirect_hops)
132 # check if file is located on Google Drive
133 file_id = _get_google_drive_file_id(url)
File C:\ProgramData\Miniconda3\envs\pDL\lib\site-packages\torchvision\datasets\utils.py:78, in _get_redirect_url(url, max_hops)
75 headers = {"Method": "HEAD", "User-Agent": USER_AGENT}
77 for _ in range(max_hops + 1):
---> 78 with urllib.request.urlopen(urllib.request.Request(url, headers=headers)) as response:
79 if response.url == url or response.url is None:
80 return url
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:222, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:525, in OpenerDirector.open(self, fullurl, data, timeout)
522 req = meth(req)
524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525 response = self._open(req, data)
527 # post-process response
528 meth_name = protocol+"_response"
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:542, in OpenerDirector._open(self, req, data)
539 return result
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 '_open', req)
544 if result:
545 return result
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:502, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
500 for handler in handlers:
501 func = getattr(handler, meth_name)
--> 502 result = func(*args)
503 if result is not None:
504 return result
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1397, in HTTPSHandler.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)
File C:\ProgramData\Miniconda3\envs\pDL\lib\urllib\request.py:1357, in AbstractHTTPHandler.do_open(self, http_class, req, **http_conn_args)
1354 h.request(req.get_method(), req.selector, req.data, headers,
1355 encode_chunked=req.has_header('Transfer-encoding'))
1356 except OSError as err: # timeout error
-> 1357 raise URLError(err)
1358 r = h.getresponse()
1359 except:
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1131)>
关闭ssl验证。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context