1 from __future__
import division
2 from __future__
import print_function
11 from urllib.error
import URLError
12 from urllib.request
import urlretrieve
14 from urllib2
import URLError
15 from urllib
import urlretrieve
18 'train-images-idx3-ubyte.gz',
19 'train-labels-idx1-ubyte.gz',
20 't10k-images-idx3-ubyte.gz',
21 't10k-labels-idx1-ubyte.gz',
25 def report_download_progress(chunk_number, chunk_size, file_size):
27 percent = min(1, (chunk_number * chunk_size) / file_size)
28 bar =
'#' * int(64 * percent)
29 sys.stdout.write(
'\r0% |{:<64}| {}%'.format(bar, int(percent * 100)))
32 def download(destination_path, url, quiet):
33 if os.path.exists(destination_path):
35 print(
'{} already exists, skipping ...'.format(destination_path))
37 print(
'Downloading {} ...'.format(url))
39 hook =
None if quiet
else report_download_progress
40 urlretrieve(url, destination_path, reporthook=hook)
42 raise RuntimeError(
'Error downloading resource!')
49 def unzip(zipped_path, quiet):
50 unzipped_path = os.path.splitext(zipped_path)[0]
51 if os.path.exists(unzipped_path):
53 print(
'{} already exists, skipping ... '.format(unzipped_path))
55 with gzip.open(zipped_path,
'rb')
as zipped_file:
56 with open(unzipped_path,
'wb')
as unzipped_file:
57 unzipped_file.write(zipped_file.read())
59 print(
'Unzipped {} ...'.format(zipped_path))
63 parser = argparse.ArgumentParser(
64 description=
'Download the MNIST dataset from the internet')
66 '-d',
'--destination', default=
'.', help=
'Destination directory')
71 help=
"Don't report about progress")
72 options = parser.parse_args()
74 if not os.path.exists(options.destination):
75 os.makedirs(options.destination)
78 for resource
in RESOURCES:
79 path = os.path.join(options.destination, resource)
80 url =
'http://yann.lecun.com/exdb/mnist/{}'.format(resource)
82 unzip(path, options.quiet)
83 except KeyboardInterrupt:
87 if __name__ ==
'__main__':
Module caffe2.python.models.download.