Extract the images into a 4D uint8 numpy array [index, y, x, depth].
(filename)
| 29 | |
| 30 | |
| 31 | def extract_images(filename): |
| 32 | """Extract the images into a 4D uint8 numpy array [index, y, x, depth].""" |
| 33 | with gzip.open(filename) as bytestream: |
| 34 | magic = _read32(bytestream) |
| 35 | if magic != 2051: |
| 36 | raise ValueError( |
| 37 | 'Invalid magic number %d in MNIST image file: %s' % |
| 38 | (magic, filename)) |
| 39 | num_images = _read32(bytestream) |
| 40 | rows = _read32(bytestream) |
| 41 | cols = _read32(bytestream) |
| 42 | buf = bytestream.read(rows * cols * num_images) |
| 43 | data = numpy.frombuffer(buf, dtype=numpy.uint8) |
| 44 | data = data.reshape(num_images, rows, cols, 1) |
| 45 | data = data.astype('float32') / 255.0 |
| 46 | return data |
| 47 | |
| 48 | |
| 49 | def extract_labels(filename): |
no test coverage detected