Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header for MNIST download due to cloudflare browser check #1939

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion torchvision/datasets/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ def download(self):
makedir_exist_ok(self.raw_folder)
makedir_exist_ok(self.processed_folder)

# create fake header, see: https://github.com/pytorch/vision/issues/1938
header = [('User-agent', 'Mozilla/5.0')]

# download files
for url, md5 in self.resources:
filename = url.rpartition('/')[2]
download_and_extract_archive(url, download_root=self.raw_folder, filename=filename, md5=md5)
download_and_extract_archive(
url, download_root=self.raw_folder, filename=filename, md5=md5, header=header)

# process and save as torch files
print('Processing...')
Expand Down
15 changes: 12 additions & 3 deletions torchvision/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ def makedir_exist_ok(dirpath):
raise


def download_url(url, root, filename=None, md5=None):
def download_url(url, root, filename=None, md5=None, header=None):
"""Download a file from a url and place it in root.

Args:
url (str): URL to download file from
root (str): Directory to place downloaded file in
filename (str, optional): Name to save the file under. If None, use the basename of the URL
md5 (str, optional): MD5 checksum of the download. If None, do not check
header (list(tuples), optional): Header to pass to urlretrieve. If None, do not set
"""
from six.moves import urllib

Expand All @@ -78,6 +79,11 @@ def download_url(url, root, filename=None, md5=None):
if check_integrity(fpath, md5):
print('Using downloaded and verified file: ' + fpath)
else: # download the file
# set header, see #1938
if header is not None:
opener = urllib.request.build_opener()
opener.addheaders = header
urllib.request.install_opener(opener)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should revert back to the previous opener after the function has finished? Otherwise this globally affects the rest of the code using urllib. Not sure if it's possible to do it with the current API though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense and thanks for pointing this out!
It seems to work, if I create an empty opener and just install it again, as I get the same forbidden error then.

try:
print('Downloading ' + url + ' to ' + fpath)
urllib.request.urlretrieve(
Expand All @@ -98,6 +104,9 @@ def download_url(url, root, filename=None, md5=None):
# check integrity of downloaded file
if not check_integrity(fpath, md5):
raise RuntimeError("File not found or corrupted.")
# reset header, see #1938
opener = urllib.request.build_opener()
urllib.request.install_opener(opener)


def list_dir(root, prefix=False):
Expand Down Expand Up @@ -254,14 +263,14 @@ def extract_archive(from_path, to_path=None, remove_finished=False):


def download_and_extract_archive(url, download_root, extract_root=None, filename=None,
md5=None, remove_finished=False):
md5=None, remove_finished=False, header=None):
download_root = os.path.expanduser(download_root)
if extract_root is None:
extract_root = download_root
if not filename:
filename = os.path.basename(url)

download_url(url, download_root, filename, md5)
download_url(url, download_root, filename, md5, header)

archive = os.path.join(download_root, filename)
print("Extracting {} to {}".format(archive, extract_root))
Expand Down