Skip to content

Instantly share code, notes, and snippets.

Forked from akesling/
Last active December 13, 2017 20:47
What would you like to do?
Python script for working with MNIST dataset. Minor edits to work with python3 and ascii_show function
import os
import struct
import numpy as np
Loosely inspired by
which is GPL licensed.
def read(dataset = "training", path = "."):
Python function for importing the MNIST data set. It returns an iterator
of 2-tuples with the first element being the label and the second element
being a numpy.uint8 2D array of pixel data for the given image.
if dataset is "training":
fname_img = os.path.join(path, 'train-images.idx3-ubyte')
fname_lbl = os.path.join(path, 'train-labels.idx1-ubyte')
elif dataset is "testing":
fname_img = os.path.join(path, 't10k-images.idx3-ubyte')
fname_lbl = os.path.join(path, 't10k-labels.idx1-ubyte')
raise(ValueError, "dataset must be 'testing' or 'training'")
# Load everything in some numpy arrays
with open(fname_lbl, 'rb') as flbl:
magic, num = struct.unpack(">II",
lbl = np.fromfile(flbl, dtype=np.int8)
with open(fname_img, 'rb') as fimg:
magic, num, rows, cols = struct.unpack(">IIII",
img = np.fromfile(fimg, dtype=np.uint8).reshape(len(lbl), rows, cols)
get_img = lambda idx: (lbl[idx], img[idx])
# Create an iterator which returns each image in turn
for i in range(len(lbl)):
yield get_img(i)
def show(image):
Render a given numpy.uint8 2D array of pixel data.
from matplotlib import pyplot
import matplotlib as mpl
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1)
imgplot = ax.imshow(image,
def ascii_show(image):
for y in image[1]:
row = ""
for x in y:
row += '{0: <4}'.format(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment