-
Notifications
You must be signed in to change notification settings - Fork 4
Example 5 View MNIST handwritten digit images
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
import peforth
It starts with a long list of self tests like this:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
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 argumentloc=locals()
whenok()
was called, at top of the peforth data stack -
[0]
get its first cell, which is thelocals()
dictionary whenok()
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.
py:~ import PIL.Image as image; push(image)
value image
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.
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 statementimage.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". Typehelp pic
, you can see it. -
"L"
indicates that images in MNIST are grey level pictures,28x28
is their size. You can type inmnist py: help(pop())
to view details of the mnist dataset.
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.
These peforth words mean:
-
mnist
is a value variable from aboveinport
command. -
mnist :> train.images[0]
in peforth equals tomnist.train.images[0]
in python. It leaves the result at TOS which is the first image in themnist.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 topic.putdata(pop());pic.show();
in python that loads the PIL image instance pic with the image and show it on your computer screen.
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()
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
H.C. Chen hcchen5600@gmail.com
FigTaiwan
2017.9.29