-1

I come looking for general tips about the program I'm writing now.

The goal is: Use neural network program to recognize 3 letters [D,O,M] (or display "nothing is recognized" if i input anything other than those 3).

Here's what I have so far:

A class for my single neuron

public class neuron
{
    double[] weights;
    public neuron()
    {
        weights = null;
    }
    public neuron(int size)
    {
        weights = new double[size + 1];
        Random r = new Random();
        for (int i = 0; i <= size; i++)
        {
            weights[i] = r.NextDouble() / 5 - 0.1;
        }
    }
    public double output(double[] wej)
    {
        double s = 0.0;
        for (int i = 0; i < weights.Length; i++) s += weights[i] * wej[i];
        s = 1 / (1 + Math.Exp(s));
        return s;
    }
}

A class for a layer:

public class layer 
{
    neuron[] tab;
    public layer()
    {
        tab = null;
    }
    public layer(int numNeurons, int numInputs)
    {
        tab = new neuron[numNeurons];
        for (int i = 0; i < numNeurons; i++)
        {
            tab[i] = new neuron(numInputs);
        }
    }
    public double[] compute(double[] wejscia)
    {
        double[] output = new double[tab.Length + 1];
        output[0] = 1;
        for (int i = 1; i <= tab.Length; i++)
        {
            output[i] = tab[i - 1].output(wejscia);
        }
        return output;
    }
}

And finally a class for a network

public class network
{
    layer[] layers = null;
    public network(int numLayers, int numInputs, int[] npl)
    {
        layers = new layer[numLayers];
        for (int i = 0; i < numLayers; i++)
        {
            layers[i] = new layer(npl[i], (i == 0) ? numInputs : (npl[i - 1]));
        }

    }
    double[] compute(double[] inputs)
    {
        double[] output = layers[0].compute(inputs);
        for (int i = 1; i < layers.Length; i++)
        {
            output = layers[i].compute(output);

        }
        return output;
    }
}

Now for the algorythm I chose:

I have a picture box, size 200x200, where you can draw a letter (or read one from jpg file).

I then convert it to my first array(get the whole picture) and 2nd one(cut the non relevant background around it) like so:

Bitmap bmp2 = new Bitmap(this.pictureBox1.Image);
        int[,] binaryfrom = new int[bmp2.Width, bmp2.Height];

        int minrow=0, maxrow=0, mincol=0, maxcol=0;
        for (int i = 0; i < bmp2.Height; i++)
        {
            for (int j = 0; j < bmp2.Width; j++)
            {
                if (bmp2.GetPixel(j, i).R == 0)
                {
                    binaryfrom[i, j] = 1;
                    if (minrow == 0) minrow = i;
                    if (maxrow < i) maxrow = i;
                    if (mincol == 0) mincol = j;
                    else if (mincol > j) mincol = j;
                    if (maxcol < j) maxcol = j;
                }
                else
                {
                    binaryfrom[i, j] = 0;
                }
            }
        }


        int[,] boundaries = new int[binaryfrom.GetLength(0)-minrow-(binaryfrom.GetLength(0)-(maxrow+1)),binaryfrom.GetLength(1)-mincol-(binaryfrom.GetLength(1)-(maxcol+1))];

        for(int i = 0; i < boundaries.GetLength(0); i++)
        {
            for(int j = 0; j < boundaries.GetLength(1); j++)
            {
                boundaries[i, j] = binaryfrom[i + minrow, j + mincol];

            }
        }

And convert it to my final array of 12x8 like so (i know I could shorten this a fair bit, but wanted to have every step in different loop so I can see what went wrong easier[if anything did]):

int[,] finalnet = new int[12, 8];

        int k = 1;
        int l = 1;

        for (int i = 0; i < finalnet.GetLength(0); i++)
        {
            for (int j = 0; j < finalnet.GetLength(1); j++)
            {
                finalnet[i, j] = 0;
            }
        }

        while (k <= finalnet.GetLength(0))
            {
                while (l <= finalnet.GetLength(1))
                {
                    for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i < (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
                    {
                        for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (l - 1); j < (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * l; j++)
                        {
                            if (boundaries[i, j] == 1) finalnet[k-1, l-1] = 1;
                        }
                    }
                    l++;
                }
                l = 1;
                k++;
            }
        int a = boundaries.GetLength(0);
        int b = finalnet.GetLength(1);
       if((a%b) != 0){

            k = 1;

            while (k <= finalnet.GetLength(1))
            {
                for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i < boundaries.GetLength(0); i++)
                {
                    for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (k - 1); j < (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * k; j++)
                    {
                        if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, k - 1] = 1;
                    }

                }
                k++;
            }
        }

        if (boundaries.GetLength(1) % finalnet.GetLength(1) != 0)
        {
            k = 1;

            while (k <= finalnet.GetLength(0))
            {
                for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i < (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
                {
                    for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j < boundaries.GetLength(1); j++)
                    {
                        if (boundaries[i, j] == 1) finalnet[k - 1, finalnet.GetLength(1) - 1] = 1;
                    } 
                }
                k++;
            }

            for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i < boundaries.GetLength(0); i++)
            {
                for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j < boundaries.GetLength(1); j++)
                {
                    if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, finalnet.GetLength(1) - 1] = 1;
                }
            }
        }

The result is a 12x8 (I can change it in the code to get it from some form controls) array of 0 and 1, where 1 form the rough shape of a letter you drawn.

Now my questions are: Is this a correct algorythm? Is my function

1/(1+Math.Exp(x))

good one to use here? What should be the topology? 2 or 3 layers, and if 3, how many neurons in hidden layer? I have 96 inputs (every field of the finalnet array), so should I also take 96 neurons in the first layer? Should I have 3 neurons in the final layer or 4(to take into account the "not recognized" case), or is it not necessary?

Thank you for your help.

EDIT: Oh, and I forgot to add, I'm gonna train my network using Backpropagation algorythm.

2 Answers 2

2
  1. You may need 4 layers at least to get accurate results using back propagation method. 1 input, 2 middle layers, and an output layer.

  2. 12 * 8 matrix is too small(and you may end up in data loss which will result in total failure) - try some thing 16 * 16. If you want to reduce the size then you have to peel out the outer layers of black pixels further.

  3. Think about training the network with your reference characters.

  4. Remember that you have to feed back the output back to the input layer again and iterate it multiple times.

2
  • 1. Can you explain to me shortly why I need 4 layers? 2. I started with 7x5 matrix, then found that the 12x8 is preffered size if I don't want a super accurate results (it's a program for my laboratories at uni anyway). By 16x16 do you mean it's better to take AxA matrix than AxB or simply larger one? 3. I have a set of training images, written by 5 different people, 10 for each letter. 4. I think my english is not well enough to understand that point, sorry :) Also why am I getting downvoted, I tried my best to explain my case.
    – NagashTDN
    Nov 8, 2014 at 10:47
  • Middle (hidden) layers play a critical role in learning the samples. Hence one more layer will increase its ability to learn. If the accuracy is not a concern you can use 3 layers. However in industrial strength code, people use dynamically configurable network to increase the accuracy and for better testing. 16X16 is the optimized one from my experience.
    – Raj
    Nov 8, 2014 at 12:03
1

A while back I created a neural net to recognize digits 0-9 (python, sorry), so based on my (short) experience, 3 layers are ok and 96/50/3 topology will probably do a good job. As for the output layer, it's your choice; you can either backpropagate all 0s when the input image is not a D, O or M or use the fourth output neuron to indicate that the letter was not recognized. I think that the first option would be the best one because it's simpler (shorter training time, less problems debugging the net...), you just need to apply a threshold under which you classify the image as 'not recognized'.
I also used the sigmoid as activation function, I didn't try others but it worked :)

2
  • Well, at my lectures it was not specified if it is better to use bipolar or unipolar function, so I did a search over the net and I'm still unsure about that. 3 output neurons seem easier yes, but the threshold is confusing me, so let's say all my neurons returned value close to 0, like 0,1;0,2;0,15 is it safe to assume then it recognized nothing?
    – NagashTDN
    Nov 8, 2014 at 10:46
  • 1
    @NagashTDN yes exactly
    – BlackBear
    Nov 8, 2014 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.