Search code examples
matlabneural-networkcell-array

Using cell array as input in Neural Networks Wizard in MATLAB?


I have a 1x1000 cell array for 28x28 images of the digit 0. I am using this as the input in the Neural Network Wizard. But i am unsure as to what i should have in the target output section. I tried a simple 1x1000 array of zeros(0) and a 1x1000 cell array of zeros.

I am at a complete loss as to what the variable type for the target output should be.

If someone could help, it would be amazing.

Thank You.


Solution

  • In order to classify images using a neural network, you need to deal with them as with a 1D array of pixels. So if you have an image 28x28 pixels, you have to reshape it to 1x784 array. If you have 1000 of such image, you will get a 1000x784 matrix.

    You may ask, how the network should use the 2-dimensional information from the image. It should not do it! You can even shuffle all your 1x784 arrays in the same way and feed the network, the result will be the same.

    What about the target? Let's say originally you have a 1000x1 vector with your labels. For a classification problem you need to transform this vector to 1000xN matrix, where N is number of your classes. So if you are going to classify digits from 0 to 9 you need 10 classes. The rows of this matrix will contain only 0 and 1. Each 1 depicts the current class.

    As an example I took a set of hand written digits from here. Each image has 28x28 pixels. The whole set has 10000 samples. Each pixel is coded by a number from 0 to 255 corresponding to the gray level. I normalized the numbers dividing them by 255, so now I have a real number in the range [0..1] for each pixel.

    You can download the preprocessed data set from here.

    Here is your code:

    load('X.mat'); %inputs
    load('Y.mat'); %targets
    
    net = patternnet(10); %a network with 10 units in the hidden layer
    
    [net,tr] = train(net, X',Y'); %training stage
    

    It will generate a network like this:

    neural network for classification

    The data set will be divided automatically to the training, validation and test sets.

    The training progress can be observed here:

    enter image description here

    To see the network's precision you might want to have a look at the confusion plots:

    confusion matrix for training set confusion matrix for validation set confusion matrix for test set

    As you can see the results are pretty good. You can tune the network to get much better results.