Psychology 149
Machine Learning & AI

Lab Project 6:  Multiclass classification



1)  Practice with loading and maipulating images.  These images will then be used in question 2 below to train a network to identify different animals.  Write a program that loads one of the following five images (cat, dog, horse, fish, owl), resizes them to be much smaller in resolution so that a neural network can handle the smaller images during training.  Then change the color images to grayscale (this also helps reduce the image size).   If an image has a resolution of 1000 by 1000 pixels (1000 rows by 1000 columns) which is not an unusual size, it will then have 1000x1000 = 1,000,000 pixels.  This means that for you to inut this image to a network, you would need a network with one million input nodes! Not very practical. It is therefore typical to resize the images to be much smaller.  In question 1, you should:

a) load an image using the matlab function "imread" (for image read).  The five  image files are named "cat.jpg", "dog.jpg", "horse.jpg", "fish.jpg", and "owl.jpg". You can directly load an image from the course URL (see step 1, or type help imread).  The images are at 
http://socsci.uci.edu/~saberi/psych149/project6/animals/

b) reduce its size using the Matlab  function "imresize"

c) change it from color to grayscale using the Matlab function "rgb2gray"

d) display the image to the monitor using the Matlab function "imshow"

e) crop the image so that it is a square image (same number of rows and columns, which gives you the same number of horizontal and vertical pixles).  To crop the image, use the Matlab function "imcrop".  Type: help imcrop in the Matlab command window to see how to use this function.   

   
    

Step 1 

Step 2 

Step 3  

Step 4

Step 5

Step 6





2) Train a neural network to identify which of the five animal pictures is the input image to the network.  The network should be able to take as its input a 5x5 pixel image (25 total input nodes).  To do this, each picture has to be reduced significantly in size so that it is in effect only a pattern of 5x5 values.  Think of a 5x5 grid with only ones and zeros as the values of its elements.

Just like prior weeks, you  need to write two programs.  We'll go through each separately.
The first program, which you can call "MultiClass" should be a function that is able to take an input matrix (of 5 images), as well as correct answers (cat, dog, etc.), and weights.
The network should have 25 input nodes, a hidden layer with 50 nodes, and an output layer with 5 nodes (for the five animals). It should then use the Softmax activation function and back propagation to generate new updated weights (this program goes through one epoch of training). The function should have the form MultiClass(W1, W2, X, D) where W1 are the weights between the input layer and the hidden layer, W2 are the weights between the hidden and output layers, X is the input matrix (images), and D is the correct answer vector (cat, dog, ...) transformed to numeric values as discussed in class.

Step 1 

Step 2 

Step 3  

Step 4

Step 5

Step 6 


The second program, which you can call "TestMultiClass" should use the MultiClass function you just created to train the network (for 1000 epochs?) and then, after it has been trained, ask the user to enter a picture of an animal (Which animal picture should I load? ) and  return a decision as to which picture the network "thinks" you have loaded (cat, dog, etc.).
 
The second program should :
a) load all five images.
b) change them to gray scale
c) resize the gray scale image so that it is reduced to a very small matrix of 5 x 5 (total of 25 pixels).  
d) give the network intial random weights
fe use the MultiClass function you created earlier in a loop of 1000 epochs to train the network.
f) once the network is trained, you should be able test it by loading an image and allowing the trained network to tell the user the name of the animal.  

Step 1 

Step 2 

Step 3  

Step 4

Step 5

Step 6 

Step 7  

Step 8