Using Images to Train ANNs Using SNNS


Create a working directory in which to put files for this tutorial. For example purposes, let's call it images. Make images your current directory.

Download the following images and code:

Build the program pgm2snns, which creates SNNS pattern files from images, by compiling pgm2snns.c.

gcc -o pgm2snns pgm2snns.c ppmIO.c -I. -lm

Now use pgm2snns to create a pattern file, emboss.pat from the training images.

pgm2snns mccabe.pgm mccabe.emboss.pgm > emboss.pat

Note that the output pattern image (mccabe.emboss.pgm) has the outside rows and columns removed compared to the input image (mccabe.pgm). This is because we will be using a 3x3 input layer on our neural network and a single output. Since the input layer of the neural network has to be placed completely on the input image, the output image cannot include the outside rows and columns.

SNNS Setup

Start snns. If you do not know how to use SNNS (the Stuttgart Neural Network Simulator), then go through the initial SNNS Tutorial first.

Using the FILE window, load the emboss.pat pattern file.

Now we will use the BIGNET window to create our network. Click on the BIGNET button in the SNNS central window and select general to get the window below.

The top half of this window is the most important; it is where you enter the parameters for each layer of the network. Start by clicking on the TYPE button and see how the layer type under the Edit Plane column changes. Set the type to input.

The two fields under the place type are the number of units in the X and Y direction for that plane. For our simple network, make X and Y both 3 so that we have a 3x3 input layer. You can leave the z-coordinate of the plane blank. Once you have filled in all of the values, and the type is input, then click on ENTER to create the layer. The values you put in the Edit Plane column should now be transferred over to the Current Plane column.

To create the hidden layer, set the type to hidden and then make the hidden layer 2x2 by putting 2 in the X field and 2 in the Y field. Click ENTER once the fields are correct.

To create the output layer, set the type to output and make the output layer 1x1 by putting 1 in the X and Y fields. Click ENTER to create the output layer.

Once you have created all of the layers, you can use the let and right arrows at the bottom of the top window to view each plane.

To create the links in your network, just click the FULL CONNECTION button at the bottom of the window. The click on the CREATE NET button, also at the bottom of the window.

To see your network, open the DISPLAY window from the main snns window. Your network should look something like the window below.

If you want to see the links, use the SETUP button, but showing the links makes for a very cluttered image.

Training the Network

To train the network, first bring up the control window. Next bring up the sub-pattern window by clicking on the SUB PAT button on the right of the control window. This will bring up a window like the one below.

This window lets you indicate how to step through the 2D pattern file. In this case, the Size of the input should be 3x3 while the output should be 1x1, as shown in the window above. Set up your system to be correct and then click on the right arrow in the sub-pattern window. This should let you step through the input patterns.

Before starting to train, click on SHUFFLE in the sub-pattern window to ensure that you don't walk through the inputs in order each time.

Now go back to the control window and start training the network by clicking on the ALL button. Watch your network's progress in the graph window. The network should train fairly quickly since there are so many training examples in a single image. When your network has trained reasonably well (e.g. the graph looks something like the one below), save your network to the file using the FILE window.

Using Your Network

Now use the snns2c program (which comes with snns) to convert your network file to a c file.


The above command should create a file called emboss.c. Now edit the file template3x3.c and change the call to the function anns inside the for loop to be emboss. This is so that the function calls the network function created by snns2c, which is called emboss (the name of the network file) by default.

Now compile your emboss function using the following:

gcc -o emboss emboss.c template3x3.c ppmIO.c -I. -lm

You can now run the emboss program on a new image by running it with the image as an argument. The output image will be called output.pgm

Now have fun.