This is a tiny experiment to visualize the weights of an image classifier as a graphical plot.
The image classifier in this experiment is a single neuron (yes, a single neuron) that uses the sigmoid function as its activation function.
Although the model trained in this experiment works with about 70% to 80% accuracy, that's not the primary concern of this experiment. The primary concern of this experiment is to visualize the weights in a trained model and see if it offers any insight into how a trained model makes decisions.
The development steps here are written for a Linux or Mac system. All steps mentioned below assume that Python 3 is installed and you are at the top-level directory of this project.
-
Enter the following command to create a Python 3 virtual environment with
numpy
,matplotlib
andh5py
.make venv
-
Enter the following command to enter the virtual environment.
. venv
-
Enter the following command to train a model, test it and write the model to a file named
model.json
../model.py
To alter the learning parameters, look for the
train()
function in this file, edit the values ofcount
andalpha
variables and run this script again. -
Classify arbitrary 64x64 PNG images in the
extra-set
directory with the following command. You can copy any image into this directory as long as it is a 64x64 PNG and run the following command../classify.py
-
To generate graphical plots of the learned model, enter the following command.
./plotmodel.py
This generates four weight plots
wr.png
,wg.png
,wb.png
andw.png
. These are plots of the weights for each channel (R, G and B channels) and an overall plot of all weights, respectively.This generates four weight plots
ar.png
,ag.png
,ab.png
anda.png
. These are plots of the activations for each channel (R, G and B channels) and an overall plot of all activations, respectively.These plots are explained in a little more detail in the next few sections.
Here are the graphical plots of the red, blue and green channel weights. The fourth image is a plot of the weights of all the channels.
Similarly, these are the activation plots of the red, blue, green and all channel weights.
Note: These plots look tiny because they are 64x64 images. If you want to zoom, try opening the image in a new tab and then zooming it.
Here is a brief description of each weight plot.
-
The first plot contains the plot of the weights for the red channel. The red area is where the neuron assigns positive weights to the red component of the pixels. The cyan area is where the neuron assigns negative weights to the red component of the pixels. Note that red, orange, yellow, purple, gray and white are examples of colors that have positive red component, so the weights would affect the red component of these colors.
All positive weights are normalized with respect to the maximum positive weight. All negative weights are normalized with respect to the minimum positive weight.
-
The second plot contains the plot of the weights for the green channel. The green area represents positive weights and the magenta area represents negative weights. The weights are normalized as explained in the first point. It is easy to see that the model has associated very negative weights with the presence of green component around the edge of the image, perhaps because such images are typical of landscapes.
-
The third plot contains the plot of the weights for the blue channel. The blue area represents positive weights and the yellow area represents negative weights. The weights are normalized as explained in the first point.
-
The fourth plot contains the plot of the weights for all the channels. All weights are normalized to a positive real number between 0 and 1. This means that the plot is more colorful but the negative weights are not easy to visualize.
I could not come up with a clever way to color-code negative weights of each channel while color-coding the positive weights of the other two channels and still display the negative and positive weights of each channel. That is why I am simply plotting all (r, g, b) weights normalized to positive real numbers.
Here is a brief description of each activation plot.
-
The first plot contains the plot of the activations of the red component in each pixel of the input image. The activation for each pixel being red is computed separately and displayed as a graphical plot. Notice the similarity between the bright red areas in the weight plot and the bright red areas in the activation plot. Similarly, notice the similarity between the cyan areas in the weight plot and the dark areas in the activation plot.
-
The second plot contains the plot of the activations of the green component in each pixel of the input image.
-
The third plot contains the plot of the activations of the blue component in each pixel of the input image.
-
The fourth plot contains the plot of the red, green and blue components of the first three activation plots combined.
Out of 50 test samples, 41 were correctly classified.
The test accuracy is: 82.00%.
Alter the learning parameters in count
and alpha
variables in
train()
function of model.py
to alter the test accuracy.
The training images and test images are present in train-set
and
test-set
directories.
The training and test data were obtained from a few HDF5 files shared by
Andrew Ng. The original H5 files are present in the h5data
directory.
The script h5toimg.py
converts this data to separate PNG image
files and writes them to train-set
and test-set
directories.
By tuning the learning parameters (number of iterations and learning rate), it is possible to alter the accuracy. The accuracy was observed to be between 70% and 80% for most tests.
An accuracy of 80% is not great because 1 out of every 5 predictions would be false which is not impressive. But it is not bad either considering that this model uses just one single neuron.
Here is a similar experiment that shows the activation plots of a multi-layer neural network: mycask/cat-classifier-dnn.