【问题标题】:Getting "HTTP Error 403: Forbidden" error when download MNIST dataset下载 MNIST 数据集时出现“HTTP 错误 403:禁止”错误
【发布时间】:2020-03-05 14:48:40
【问题描述】:

我使用以下代码获取 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)

【问题讨论】:

    标签: python torchvision


    【解决方案1】:

    使用提到的建议here,将其添加到我的脚本顶部:

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

    【讨论】:

      【解决方案2】:

      似乎您可能必须在 urllib 请求中添加标头(由于该站点已迁移到 Cloudflare 保护)

      例如。

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

      pytorch here 的 github 论坛中也提到了这个问题,并提供了一些解决方案。

      其中一个更完整的 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)
      

      他们使用一个函数为每个检索添加标题,从而简化了过程。

      【讨论】:

      • @Kola73 没问题,很高兴它有帮助。
      【解决方案3】:

      我查了一下,问题是该文件夹已在 CloudFlare 保护下移动,正如其中一位评论员在此处提到的那样:https://github.com/pytorch/vision/issues/1938

      还解释了如何通过在此处添加标题来解决/修复此问题。希望对你有帮助。

      【讨论】:

      • 嗨,Kim,在答案中提供网址很有用,但在答案中提供步骤也会更有帮助,因为链接可能会中断,但网站上的答案将保留。如果您决定改进您的回复,将支持您。
      • 谢谢,有帮助:)
      猜你喜欢
      • 2021-06-05
      • 2021-06-13
      • 2021-06-02
      • 2020-07-29
      • 2011-07-12
      • 1970-01-01
      • 2011-06-24
      • 2016-01-12
      相关资源
      最近更新 更多