Skip to content

Example 5 View MNIST handwritten digit images

H.C. Chen edited this page Oct 6, 2017 · 4 revisions

If you want to view mnist handwritten digit images right away then please jump to the bottom of this article to do the copy-paste introduced there. Go go go, good luck!

Most Machine Learning learners or Neuro Network, e.g. TensorFlow, learners must have heard about the MNIST dataset. I asume you are one of us and have also been asking "How to view the handwritten digits in the MNIST dataset?". This Youtube video tutorial and its source code is one example among many other tutorials that use the MNIST dataset. To view images in MNIST, we copy a few lines out of the source code.

...snip...
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
...snip...

Now let's start,

1. Install peforth (see its README.md)
pip install peforth
2. run python and import peforth.
import peforth

It starts with a long list of self tests like this: fig1

3. Type in the above lines from at the beginning of the mentioned tutorial:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

fig2

4. Type in this stragne line to jump into peforth
peforth.ok(loc=locals(),cmd=":> [0] inport")
OK

Where :> [0] inport are 3 peforth 'words' or commands. If you know FORTH, and you want explanations of them although you don't need to, they mean:

  • :> about the object, which was from the argument loc=locals() when ok() was called, at top of the peforth data stack
  • [0] get its first cell, which is the locals() dictionary when ok() was called. What we are interested now is 'mnist' dataset which is in that dictionary.
  • inport converts the dictionary at TOS (top of the peforth data stack) into peforth values. So we have 'mnist' now as a peforth value (or variable).

Type help :> and help inport to view these peforth words' help messages.

5. Assume PIL or Pillow is installed in your computer. Type in these lines to invoke PIL:
py:~ import PIL.Image as image; push(image)
value image 

fig3

This is to demonstrate that we can import python modules we need when in peforth. Again, If you know FORTH, and you want explanations of the above lines although you don't have to, they mean:

  • py:~ indicates that the rest of the line is peforth's inline python code.
  • image is our nick name for PIL.image object.
  • push(image) puts image to TOS, and then the inline python code ends with the end of the line. Some other FORTH related functions are available in peforth's inline python code. Refer to peforth's project-k kernel for details.
  • value image assigns TOS to the peforth variable named also 'image' because it really is.

Type help py:~ and help value to view these peforth words' help messages.

6. Create a PIL image instance
image :> new("L",(28,28)) value pic // ( -- PIL.image ) object

If you feel the need to know more about this line, it means:

  • The statement image :> new("L",(28,28)) in peforth equals to the statement image.new("L",(28,28)) in python. It leaves a PIL image instance on the TOS.
  • value pic gives the value on TOS to a new variable "pic".
  • All the rests after // is help message for the last word, "pic". Type help pic, you can see it.
  • "L" indicates that images in MNIST are grey level pictures,28x28 is their size. You can type in mnist py: help(pop()) to view details of the mnist dataset.
7. Now we are ready to view any image in mnist.
mnist :> train.images[0] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()

This is the result. I guess it's an image of handwritten digit 7 or 3 in the first image of mnist train dataset.

fig4

These peforth words mean:

  • mnist is a value variable from above inport command.
  • mnist :> train.images[0] in peforth equals to mnist.train.images[0] in python. It leaves the result at TOS which is the first image in the mnist.train dataset.
  • py> starts a piece of inline python. TOS is a image, py> tuple(pop()*256) adjust the scale of each dots of that image.
  • pic :: putdata(pop()) pic :: show() in peforth equals to pic.putdata(pop());pic.show(); in python that loads the PIL image instance pic with the image and show it on your computer screen.
8. There are also test and validaion datasets in mnist along with train.

let's see #31 image of the test dataset and the #123 image of the validation dataset.

mnist :> test.images[31] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()
mnist :> validation.images[123] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()

fig5

9. Copy-paste, Just do it

You can ignore all the explanations above. Copy-paste following lines all at once to python interpreter to view 3 arbitrarily selected images

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import peforth
peforth.ok(loc=locals(),cmd=":> [0] inport")
py:~ import PIL.Image as image; push(image)
value image 
image :> new("L",(28,28)) value pic // ( -- PIL.image ) object
mnist :> train.images[0] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()
mnist :> test.images[31] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()
mnist :> validation.images[123] py> tuple(pop()*256) pic :: putdata(pop()) pic :: show()

Have fun and

May the FORTH be with you!

H.C. Chen hcchen5600@gmail.com
FigTaiwan
2017.9.29

Clone this wiki locally