Download (and unzip) a file from the MNIST dataset if not already done.
(directory, filename)
| 65 | |
| 66 | |
| 67 | def download(directory, filename): |
| 68 | """Download (and unzip) a file from the MNIST dataset if not already done.""" |
| 69 | filepath = os.path.join(directory, filename) |
| 70 | if tf.gfile.Exists(filepath): |
| 71 | return filepath |
| 72 | if not tf.gfile.Exists(directory): |
| 73 | tf.gfile.MakeDirs(directory) |
| 74 | # CVDF mirror of http://yann.lecun.com/exdb/mnist/ |
| 75 | url = 'https://storage.googleapis.com/cvdf-datasets/mnist/' + filename + '.gz' |
| 76 | _, zipped_filepath = tempfile.mkstemp(suffix='.gz') |
| 77 | print('Downloading %s to %s' % (url, zipped_filepath)) |
| 78 | urllib.request.urlretrieve(url, zipped_filepath) |
| 79 | with gzip.open(zipped_filepath, 'rb') as f_in, \ |
| 80 | tf.gfile.Open(filepath, 'wb') as f_out: |
| 81 | shutil.copyfileobj(f_in, f_out) |
| 82 | os.remove(zipped_filepath) |
| 83 | return filepath |
| 84 | |
| 85 | |
| 86 | def dataset(directory, images_file, labels_file): |