Vision: Downloading pretrained models with torchvision gives HTTP Error 403

Created on 12 Feb 2020  Â·  26Comments  Â·  Source: pytorch/vision

I'm getting a 403 error when I try to download pre-trained weights with torchvision 0.5.

torchvision.models.mobilenet_v2(pretrained=True) results in HTTPError: HTTP Error 403: Forbidden. I've tried multiple models on multiple machines on multiple networks, always get a 403 error.

I can download the model directly from the URL with wget. But for some reason downloading it through torchvision doesn't work. I can progress since I can download the weights, but it's unfortunate I have to load them in manually.

bug models

Most helpful comment

And now it's failing with URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)> (again multiple locations, Python 3.6.9)

All 26 comments

I can confirm. This issue started happening this afternoon. I don't think it has anything to do with the library but rather with where the models are hosted.

Also having this is issue, when trying to download a pretrained vgg model. I am using Google colab.

Downloading: "https://download.pytorch.org/models/vgg19-dcbb9e9d.pth" to /root/.cache/torch/checkpoints/vgg19-dcbb9e9d.pth

HTTPError Traceback (most recent call last)
in ()

----> vgg_model = models.vgg19(pretrained=True)

/usr/lib/python3.6/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
648 class HTTPDefaultErrorHandler(BaseHandler):
649 def http_error_default(self, req, fp, code, msg, hdrs):
--> 650 raise HTTPError(req.full_url, code, msg, hdrs, fp)
651
652 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Having the same issue here when trying to download pretrained vgg16 model

Same here on macOS. curl works for me though. Any ideas?

same here
I am trying to use VGG19
File "vgg19.py", line 54, in _step1_load_model self.model = models.vgg19(pretrained=True) File "/opt/anaconda3/lib/python3.7/site-packages/torchvision/models/vgg.py", line 172, in vgg19 return _vgg('vgg19', 'E', False, pretrained, progress, **kwargs) File "/opt/anaconda3/lib/python3.7/site-packages/torchvision/models/vgg.py", line 93, in _vgg progress=progress) File "/opt/anaconda3/lib/python3.7/site-packages/torch/hub.py", line 492, in load_state_dict_from_url download_url_to_file(url, cached_file, hash_prefix, progress=progress) File "/opt/anaconda3/lib/python3.7/site-packages/torch/hub.py", line 391, in download_url_to_file u = urlopen(url) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(*args) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/opt/anaconda3/lib/python3.7/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

Also happens in torch hub.

This is the official notebook example which fails with the same error
https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/pytorch_vision_googlenet.ipynb#scrollTo=QjN0iFB8vAjw

+1 .. I see the same issue with 0.4.2

https://github.com/pytorch/vision/blob/0156d58ec867590b1c78fe1bc834c7da9afdf46a/torchvision/models/resnet.py#L11-L21
A temporary workaround would be to update the model_urls prefixes from https://download.pytorch.org/models/ to https://s3.amazonaws.com/pytorch/models/

For me, this only fails on the CI server (GitHub actions); locally the download works
EDIT: it works from some machines and fails on others (using Python / torchvision).
(wget always works)

Looks like it started working again.

Working again for me as well now.

It is working now.

And now it's failing with URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)> (again multiple locations, Python 3.6.9)

It's started failing again now:

Downloading: "https://download.pytorch.org/models/vgg19-dcbb9e9d.pth" to /root/.cache/torch/checkpoints/vgg19-dcbb9e9d.pth

SSLError Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1317 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error

20 frames
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

During handling of the above exception, another exception occurred:

URLError Traceback (most recent call last)
/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1318 encode_chunked=req.has_header('Transfer-encoding'))
1319 except OSError as err: # timeout error
-> 1320 raise URLError(err)
1321 r = h.getresponse()
1322 except:

URLError:

If you try to open the link for some model (eg. for resenet18 https://download.pytorch.org/models/resnet18-5c106cde.pth) in the browser, you will see an error with "https certificate expired" ...

Please fix this ...

As a temporary workaround, you can use http:// instead of https:// to download the model.
EDIT: should be fixed now. Can you confirm it works fine now?

As a temporary workaround, you can use http:// instead of https:// to download the model.
EDIT: should be fixed now. Can you confirm it works fine now?

Works for me.

The SSL Certificate issue is fixed now. The Cert was expired for ~4 hours, and we root-caused the auto-renewal problem, it wont happen again.

I'm getting something similar when trying to download torchvision.models.segmentation.deeplabv3_resnet50():

<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)>

@savourylie is this still an issue?

adding hub/ in the path after /.cache/torch/ worked for me

Still doesn't work for me

Reopening as its reported again.

Downloading: "https://download.pytorch.org/models/resnet18-5c106cde.pth" to C:\Users\bdtho/.cache\torch\hub\checkpoints\resnet18-5c106cde.pth

SSLCertVerificationError Traceback (most recent call last)
~.conda\envs\tensorflow_g\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1349 try:
-> 1350 h.request(req.get_method(), req.selector, req.data, headers,
1351 encode_chunked=req.has_header('Transfer-encoding'))

~.conda\envs\tensorflow_g\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

~.conda\envs\tensorflow_g\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

~.conda\envs\tensorflow_g\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

~.conda\envs\tensorflow_g\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011

~.conda\envs\tensorflow_g\lib\http\client.py in send(self, data)
949 if self.auto_open:
--> 950 self.connect()
951 else:

~.conda\envs\tensorflow_g\lib\http\client.py in connect(self)
1423
-> 1424 self.sock = self._context.wrap_socket(self.sock,
1425 server_hostname=server_hostname)

~.conda\envs\tensorflow_g\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,

~.conda\envs\tensorflow_g\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):

~.conda\envs\tensorflow_g\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: unable to get issuer certificate (_ssl.c:1123)

During handling of the above exception, another exception occurred:

URLError Traceback (most recent call last)
in
----> 1 model = torchvision.models.resnet18(pretrained=True)

~.conda\envs\tensorflow_g\lib\site-packages\torchvision\models\resnet.py in resnet18(pretrained, progress, *kwargs)
238 progress (bool): If True, displays a progress bar of the download to stderr
239 """
--> 240 return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,
241 *
kwargs)
242

~.conda\envs\tensorflow_g\lib\site-packages\torchvision\models\resnet.py in _resnet(arch, block, layers, pretrained, progress, *kwargs)
224 model = ResNet(block, layers, *
kwargs)
225 if pretrained:
--> 226 state_dict = load_state_dict_from_url(model_urls[arch],
227 progress=progress)
228 model.load_state_dict(state_dict)

~.conda\envs\tensorflow_g\lib\site-packages\torch\hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
553 r = HASH_REGEX.search(filename) # r is Optional[Match[str]]
554 hash_prefix = r.group(1) if r else None
--> 555 download_url_to_file(url, cached_file, hash_prefix, progress=progress)
556
557 if _is_legacy_zip_format(cached_file):

~.conda\envs\tensorflow_g\lib\site-packages\torch\hub.py in download_url_to_file(url, dst, hash_prefix, progress)
423 # certificates in older Python
424 req = Request(url, headers={"User-Agent": "torch.hub"})
--> 425 u = urlopen(req)
426 meta = u.info()
427 if hasattr(meta, 'getheaders'):

~.conda\envs\tensorflow_g\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
223
224 def install_opener(opener):

~.conda\envs\tensorflow_g\lib\urllib\request.py in open(self, fullurl, data, timeout)
523
524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525 response = self._open(req, data)
526
527 # post-process response

~.conda\envs\tensorflow_g\lib\urllib\request.py in _open(self, req, data)
540
541 protocol = req.type
--> 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 '_open', req)
544 if result:

~.conda\envs\tensorflow_g\lib\urllib\request.py in _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

~.conda\envs\tensorflow_g\lib\urllib\request.py in https_open(self, req)
1391
1392 def https_open(self, req):
-> 1393 return self.do_open(http.client.HTTPSConnection, req,
1394 context=self._context, check_hostname=self._check_hostname)
1395

~.conda\envs\tensorflow_g\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1351 encode_chunked=req.has_header('Transfer-encoding'))
1352 except OSError as err: # timeout error
-> 1353 raise URLError(err)
1354 r = h.getresponse()
1355 except:

URLError:

I have tried to download pre-trained vgg16 on local machine(MacOS 10.15) and got this stack:

Downloading:` "https://download.pytorch.org/models/vgg19-dcbb9e9d.pth" to /Users/root/.cache/torch/hub/checkpoints/vgg19-dcbb9e9d.pth
…
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

And, by the way, I can download it from colab

This might be related to S3 issues in different regions. This could explain why the models can be accessed from some regions while not from others. I tried both URLs and I can download the files just fine...

Was this page helpful?
0 / 5 - 0 ratings