Caffe2 - Python API
A deep learning, cross platform ML framework
download_mnist.py
1 from __future__ import division
2 from __future__ import print_function
3 
4 import argparse
5 import gzip
6 import os
7 import sys
8 import urllib
9 
10 try:
11  from urllib.error import URLError
12  from urllib.request import urlretrieve
13 except ImportError:
14  from urllib2 import URLError
15  from urllib import urlretrieve
16 
17 RESOURCES = [
18  'train-images-idx3-ubyte.gz',
19  'train-labels-idx1-ubyte.gz',
20  't10k-images-idx3-ubyte.gz',
21  't10k-labels-idx1-ubyte.gz',
22 ]
23 
24 
25 def report_download_progress(chunk_number, chunk_size, file_size):
26  if file_size != -1:
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)))
30 
31 
32 def download(destination_path, url, quiet):
33  if os.path.exists(destination_path):
34  if not quiet:
35  print('{} already exists, skipping ...'.format(destination_path))
36  else:
37  print('Downloading {} ...'.format(url))
38  try:
39  hook = None if quiet else report_download_progress
40  urlretrieve(url, destination_path, reporthook=hook)
41  except URLError:
42  raise RuntimeError('Error downloading resource!')
43  finally:
44  if not quiet:
45  # Just a newline.
46  print()
47 
48 
49 def unzip(zipped_path, quiet):
50  unzipped_path = os.path.splitext(zipped_path)[0]
51  if os.path.exists(unzipped_path):
52  if not quiet:
53  print('{} already exists, skipping ... '.format(unzipped_path))
54  return
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())
58  if not quiet:
59  print('Unzipped {} ...'.format(zipped_path))
60 
61 
62 def main():
63  parser = argparse.ArgumentParser(
64  description='Download the MNIST dataset from the internet')
65  parser.add_argument(
66  '-d', '--destination', default='.', help='Destination directory')
67  parser.add_argument(
68  '-q',
69  '--quiet',
70  action='store_true',
71  help="Don't report about progress")
72  options = parser.parse_args()
73 
74  if not os.path.exists(options.destination):
75  os.makedirs(options.destination)
76 
77  try:
78  for resource in RESOURCES:
79  path = os.path.join(options.destination, resource)
80  url = 'http://yann.lecun.com/exdb/mnist/{}'.format(resource)
81  download(path, url, options.quiet)
82  unzip(path, options.quiet)
83  except KeyboardInterrupt:
84  print('Interrupted')
85 
86 
87 if __name__ == '__main__':
88  main()
Module caffe2.python.models.download.