簡體   English   中英

下載 MNIST 數據集時出現“HTTP 錯誤 403:禁止”錯誤

[英]Getting "HTTP Error 403: Forbidden" error when download MNIST dataset

我使用以下代碼來獲取 MNIST 數據集:

import torchvision.datasets
MNIST_train = torchvision.datasets.MNIST('./', download=True, train=True)

這段代碼以前工作過,但現在它顯示了錯誤:

Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./MNIST\raw\train-images-idx3-ubyte.gz
HTTP Error 403: Forbidden
Stack trace:
 >  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\urllib\request.py", line 650, in http_error_default
 >    raise HTTPError(req.full_url, code, msg, hdrs, fp)

使用這里提到的建議,將其添加到我的腳本頂部有效:

from six.moves import urllib    
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)

似乎您可能需要向 urllib 請求添加標頭(由於該站點轉向 Cloudflare 保護)

例如。

opener = urllib.request.URLopener()
opener.addheader('User-Agent', some_user_agent)
opener.retrieve(
    url, fpath,
    reporthook=gen_bar_updater()
)

這個問題是在github上論壇pytorch提到這里為好,與這一問題的幾種解決方案。

給出的更完整的 Python3 解決方案之一如下:

from torchvision import datasets
import torchvision.transforms as transforms
import urllib

num_workers = 0
batch_size = 20
basepath = 'some/base/path'
transform = transforms.ToTensor()

def set_header_for(url, filename):
    opener = urllib.request.URLopener()
    opener.addheader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36')
    opener.retrieve(
    url, f'{basepath}/{filename}')

set_header_for('http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz', 'train-images-idx3-ubyte.gz')
set_header_for('http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz', 'train-labels-idx1-ubyte.gz')
set_header_for('http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz', 't10k-images-idx3-ubyte.gz')
set_header_for('http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz', 't10k-labels-idx1-ubyte.gz')
train_data = datasets.MNIST(root='data', train=True,
                                   download=True, transform=transform)
test_data = datasets.MNIST(root='data', train=False,
                                  download=False, transform=transform)

他們使用函數為每個檢索添加標題,從而簡化了過程。

我查了一下,問題是該文件夾已在 CloudFlare 保護下移動,正如這里的評論者之一所提到的: https : //github.com/pytorch/vision/issues/1938

還解釋了如何通過在那里添加標題來解決/修復此問題。 我希望它有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM