It seems like there are some mini-problems that I do over and over. Displaying an MNIST digit is one such mini-problem. I’m in the process of writing an article for Visual Studio Magazine ( https://visualstudiomagazine.com/Home.aspx ) and the topic is MNIST image classification.
An article about MNIST needs to explain what MNIST is, and that means showing an MNIST digit. MNIST (“modified National Institute of Standards and Technology”) is a collection of 70,000 images of handwritten digits. Each digit is 28×28 pixels, and each pixel value is between 0 and 255.
As is usually the case, a picture is worth a thousand words, so I wrote a little Python program that loads the Keras library pre-installed MNIST images into memory and then displays the first training image. I show the image both in terms of raw values (expressed in hex to make the output nicer) and in terms of a grayscale image.
# show_image.py # load built-in Keras MNIST dataset # display (first training) image import numpy as np import keras as K import matplotlib.pyplot as plt def main(): print("\nBegin show MNIST image \n") (train_x, train_y), \ (test_x, test_y) = K.datasets.mnist.load_data() # print(train_x.shape) # (60000, 28, 28) # print(train_y.shape) # (60000,) d = train_x[0] d = d.reshape(28,28) for row in range(0,28): for col in range(0,28): print("%02X " % d[row][col], end="") print("") lbl = train_y[0] print("\ndigit = ", lbl) plt.imshow(d, cmap=plt.get_cmap('gray_r')) plt.show() print("\nEnd \n") if __name__ == "__main__": main()
I don’t think there’s a moral to the story. Displaying an MNIST digit is a fun little challenge but after the first 20 or so times the novelty wears off a bit. However, repetition is important for anyone who writes code — if you don’t practice continuously, you’ll lose your coding skills quickly.
You must be logged in to post a comment.