|
| 1 | +"""Functions for downloading and reading MNIST data.""" |
| 2 | +import gzip |
| 3 | +import os |
| 4 | +import urllib |
| 5 | +import numpy |
| 6 | +SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/' |
| 7 | + |
| 8 | + |
| 9 | +def maybe_download(filename, work_directory): |
| 10 | + """Download the data from Yann's website, unless it's already here.""" |
| 11 | + if not os.path.exists(work_directory): |
| 12 | + os.mkdir(work_directory) |
| 13 | + filepath = os.path.join(work_directory, filename) |
| 14 | + if not os.path.exists(filepath): |
| 15 | + filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath) |
| 16 | + statinfo = os.stat(filepath) |
| 17 | + print 'Succesfully downloaded', filename, statinfo.st_size, 'bytes.' |
| 18 | + return filepath |
| 19 | + |
| 20 | + |
| 21 | +def _read32(bytestream): |
| 22 | + dt = numpy.dtype(numpy.uint32).newbyteorder('>') |
| 23 | + return numpy.frombuffer(bytestream.read(4), dtype=dt) |
| 24 | + |
| 25 | + |
| 26 | +def extract_images(filename): |
| 27 | + """Extract the images into a 4D uint8 numpy array [index, y, x, depth].""" |
| 28 | + print 'Extracting', filename |
| 29 | + with gzip.open(filename) as bytestream: |
| 30 | + magic = _read32(bytestream) |
| 31 | + if magic != 2051: |
| 32 | + raise ValueError( |
| 33 | + 'Invalid magic number %d in MNIST image file: %s' % |
| 34 | + (magic, filename)) |
| 35 | + num_images = _read32(bytestream) |
| 36 | + rows = _read32(bytestream) |
| 37 | + cols = _read32(bytestream) |
| 38 | + buf = bytestream.read(rows * cols * num_images) |
| 39 | + data = numpy.frombuffer(buf, dtype=numpy.uint8) |
| 40 | + data = data.reshape(num_images, rows, cols, 1) |
| 41 | + return data |
| 42 | + |
| 43 | + |
| 44 | +def dense_to_one_hot(labels_dense, num_classes=10): |
| 45 | + """Convert class labels from scalars to one-hot vectors.""" |
| 46 | + num_labels = labels_dense.shape[0] |
| 47 | + index_offset = numpy.arange(num_labels) * num_classes |
| 48 | + labels_one_hot = numpy.zeros((num_labels, num_classes)) |
| 49 | + labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1 |
| 50 | + return labels_one_hot |
| 51 | + |
| 52 | + |
| 53 | +def extract_labels(filename, one_hot=False): |
| 54 | + """Extract the labels into a 1D uint8 numpy array [index].""" |
| 55 | + print 'Extracting', filename |
| 56 | + with gzip.open(filename) as bytestream: |
| 57 | + magic = _read32(bytestream) |
| 58 | + if magic != 2049: |
| 59 | + raise ValueError( |
| 60 | + 'Invalid magic number %d in MNIST label file: %s' % |
| 61 | + (magic, filename)) |
| 62 | + num_items = _read32(bytestream) |
| 63 | + buf = bytestream.read(num_items) |
| 64 | + labels = numpy.frombuffer(buf, dtype=numpy.uint8) |
| 65 | + if one_hot: |
| 66 | + return dense_to_one_hot(labels) |
| 67 | + return labels |
| 68 | + |
| 69 | + |
| 70 | +class DataSet(object): |
| 71 | + def __init__(self, images, labels, fake_data=False): |
| 72 | + if fake_data: |
| 73 | + self._num_examples = 10000 |
| 74 | + else: |
| 75 | + assert images.shape[0] == labels.shape[0], ( |
| 76 | + "images.shape: %s labels.shape: %s" % (images.shape, |
| 77 | + labels.shape)) |
| 78 | + self._num_examples = images.shape[0] |
| 79 | + # Convert shape from [num examples, rows, columns, depth] |
| 80 | + # to [num examples, rows*columns] (assuming depth == 1) |
| 81 | + assert images.shape[3] == 1 |
| 82 | + images = images.reshape(images.shape[0], |
| 83 | + images.shape[1] * images.shape[2]) |
| 84 | + # Convert from [0, 255] -> [0.0, 1.0]. |
| 85 | + images = images.astype(numpy.float32) |
| 86 | + images = numpy.multiply(images, 1.0 / 255.0) |
| 87 | + self._images = images |
| 88 | + self._labels = labels |
| 89 | + self._epochs_completed = 0 |
| 90 | + self._index_in_epoch = 0 |
| 91 | + |
| 92 | + @property |
| 93 | + def images(self): |
| 94 | + return self._images |
| 95 | + |
| 96 | + @property |
| 97 | + def labels(self): |
| 98 | + return self._labels |
| 99 | + |
| 100 | + @property |
| 101 | + def num_examples(self): |
| 102 | + return self._num_examples |
| 103 | + |
| 104 | + @property |
| 105 | + def epochs_completed(self): |
| 106 | + return self._epochs_completed |
| 107 | + |
| 108 | + def next_batch(self, batch_size, fake_data=False): |
| 109 | + """Return the next `batch_size` examples from this data set.""" |
| 110 | + if fake_data: |
| 111 | + fake_image = [1.0 for _ in xrange(784)] |
| 112 | + fake_label = 0 |
| 113 | + return [fake_image for _ in xrange(batch_size)], [ |
| 114 | + fake_label for _ in xrange(batch_size)] |
| 115 | + start = self._index_in_epoch |
| 116 | + self._index_in_epoch += batch_size |
| 117 | + if self._index_in_epoch > self._num_examples: |
| 118 | + # Finished epoch |
| 119 | + self._epochs_completed += 1 |
| 120 | + # Shuffle the data |
| 121 | + perm = numpy.arange(self._num_examples) |
| 122 | + numpy.random.shuffle(perm) |
| 123 | + self._images = self._images[perm] |
| 124 | + self._labels = self._labels[perm] |
| 125 | + # Start next epoch |
| 126 | + start = 0 |
| 127 | + self._index_in_epoch = batch_size |
| 128 | + assert batch_size <= self._num_examples |
| 129 | + end = self._index_in_epoch |
| 130 | + return self._images[start:end], self._labels[start:end] |
| 131 | + |
| 132 | + |
| 133 | +def read_data_sets(train_dir, fake_data=False, one_hot=False): |
| 134 | + class DataSets(object): |
| 135 | + pass |
| 136 | + data_sets = DataSets() |
| 137 | + if fake_data: |
| 138 | + data_sets.train = DataSet([], [], fake_data=True) |
| 139 | + data_sets.validation = DataSet([], [], fake_data=True) |
| 140 | + data_sets.test = DataSet([], [], fake_data=True) |
| 141 | + return data_sets |
| 142 | + TRAIN_IMAGES = 'train-images-idx3-ubyte.gz' |
| 143 | + TRAIN_LABELS = 'train-labels-idx1-ubyte.gz' |
| 144 | + TEST_IMAGES = 't10k-images-idx3-ubyte.gz' |
| 145 | + TEST_LABELS = 't10k-labels-idx1-ubyte.gz' |
| 146 | + VALIDATION_SIZE = 5000 |
| 147 | + local_file = maybe_download(TRAIN_IMAGES, train_dir) |
| 148 | + train_images = extract_images(local_file) |
| 149 | + local_file = maybe_download(TRAIN_LABELS, train_dir) |
| 150 | + train_labels = extract_labels(local_file, one_hot=one_hot) |
| 151 | + local_file = maybe_download(TEST_IMAGES, train_dir) |
| 152 | + test_images = extract_images(local_file) |
| 153 | + local_file = maybe_download(TEST_LABELS, train_dir) |
| 154 | + test_labels = extract_labels(local_file, one_hot=one_hot) |
| 155 | + validation_images = train_images[:VALIDATION_SIZE] |
| 156 | + validation_labels = train_labels[:VALIDATION_SIZE] |
| 157 | + train_images = train_images[VALIDATION_SIZE:] |
| 158 | + train_labels = train_labels[VALIDATION_SIZE:] |
| 159 | + data_sets.train = DataSet(train_images, train_labels) |
| 160 | + data_sets.validation = DataSet(validation_images, validation_labels) |
| 161 | + data_sets.test = DataSet(test_images, test_labels) |
| 162 | + return data_sets |
0 commit comments