Extract the images into a 4D uint8 numpy array [index, y, x, depth].
(filename)
| 22 | dt = numpy.dtype(numpy.uint32).newbyteorder('>') |
| 23 | return numpy.frombuffer(bytestream.read(4), dtype=dt) |
| 24 | def extract_images(filename): |
| 25 | """Extract the images into a 4D uint8 numpy array [index, y, x, depth].""" |
| 26 | print('Extracting', filename) |
| 27 | with gzip.open(filename) as bytestream: |
| 28 | magic = _read32(bytestream) |
| 29 | if magic != 2051: |
| 30 | raise ValueError( |
| 31 | 'Invalid magic number %d in MNIST image file: %s' % |
| 32 | (magic, filename)) |
| 33 | num_images = _read32(bytestream) |
| 34 | rows = _read32(bytestream) |
| 35 | cols = _read32(bytestream) |
| 36 | buf = bytestream.read(rows * cols * num_images) |
| 37 | data = numpy.frombuffer(buf, dtype=numpy.uint8) |
| 38 | data = data.reshape(num_images, rows, cols, 1) |
| 39 | return data |
| 40 | def dense_to_one_hot(labels_dense, num_classes=10): |
| 41 | """Convert class labels from scalars to one-hot vectors.""" |
| 42 | num_labels = labels_dense.shape[0] |
no test coverage detected