| 26 | |
| 27 | # Returns a numpy buffer of shape (num_images, 28, 28) |
| 28 | def load_mnist_data(buffer): |
| 29 | raw_buf = np.fromstring(buffer, dtype=np.uint8) |
| 30 | # Make sure the magic number is what we expect |
| 31 | assert raw_buf[0:4].view(">i4")[0] == 2051 |
| 32 | num_images = raw_buf[4:8].view(">i4")[0] |
| 33 | image_h = raw_buf[8:12].view(">i4")[0] |
| 34 | image_w = raw_buf[12:16].view(">i4")[0] |
| 35 | # Colors in the dataset are inverted vs. what the samples expect. |
| 36 | return np.ascontiguousarray(255 - raw_buf[16:].reshape(num_images, image_h, image_w)) |
| 37 | |
| 38 | # Returns a list of length num_images |
| 39 | def load_mnist_labels(buffer): |