Click here to Skip to main content
Email Password   helpLost your password?
Screenshot - screen211.png

Introduction

Artificial Neural Networks are a recent development tool that are modeled from biological neural networks. The powerful side of this new tool is its ability to solve problems that are very hard to be solved by traditional computing methods (e.g. by algorithms). This work briefly explains Artificial Neural Networks and their applications, describing how to implement a simple ANN for image recognition.

Background

I will try to make the idea clear to the reader who is just interested in the topic.

About Artificial Neural Networks (ANNs)

Artificial Neural Networks (ANNs) are a new approach that follow a different way from traditional computing methods to solve problems. Since conventional computers use algorithmic approach, if the specific steps that the computer needs to follow are not known, the computer cannot solve the problem. That means, traditional computing methods can only solve the problems that we have already understood and knew how to solve. However, ANNs are, in some way, much more powerful because they can solve problems that we do not exactly know how to solve. That's why, of late, their usage is spreading over a wide range of area including, virus detection, robot control, intrusion detection systems, pattern (image, fingerprint, noise..) recognition and so on.

ANNs have the ability to adapt, learn, generalize, cluster or organize data. There are many structures of ANNs including, Percepton, Adaline, Madaline, Kohonen, BackPropagation and many others. Probably, BackPropagation ANN is the most commonly used, as it is very simple to implement and effective. In this work, we will deal with BackPropagation ANNs.

BackPropagation ANNs contain one or more layers each of which are linked to the next layer. The first layer is called the "input layer" which meets the initial input (e.g. pixels from a letter) and so does the last one "output layer" which usually holds the input's identifier (e.g. name of the input letter). The layers between input and output layers are called "hidden layer(s)" which only propagate the previous layer's outputs to the next layer and [back] propagates the following layer's error to the previous layer. Actually, these are the main operations of training a BackPropagation ANN which follows a few steps.

A typical BackPropagation ANN is as depicted below. The black nodes (on the extreme left) are the initial inputs. Training such a network involves two phases. In the first phase, the inputs are propagated forward to compute the outputs for each output node. Then, each of these outputs are subtracted from its desired output, causing an error [an error for each output node]. In the second phase, each of these output errors is passed backward and the weights are fixed. These two phases is continued until the sum of [square of output errors] reaches an acceptable value.

Screenshot - fig1_nnet_thinner.png

Implementation

The network layers in the figure above are implemented as arrays of structs. The nodes of the layers are implemented as follows:

[Serializable]
struct PreInput
{
    public double Value;
    public double[] Weights;            
};

[Serializable]
struct Input
{
    public double InputSum;                
    public double Output;                
    public double Error;                
    public double[] Weights;        
};
            
[Serializable]        
struct Hidden        
{                
    public double InputSum;                    
    public double Output;                
    public double Error;                
    public double[] Weights;        
};
            
[Serializable]        
struct Output<T> where T : IComparable<T>         
{                
    public double InputSum;                
    public double output;                
    public double Error;                
    public double Target;     
    public T Value;   
};

The layers in the figure are implemented as follows (for a three layer network):

private PreInput[] PreInputLayer;
private Input[] InputLayer;
private Hidden[] HiddenLayer;
private Output<string>[] OutputLayer;

Training the network can be summarized as follows:

It is represented as shown below:

void TrainNetwork(TrainingSet,MaxError)
{
     while(CurrentError>MaxError)
     {
          foreach(Pattern in TrainingSet)
          {
               ForwardPropagate(Pattern);//calculate output 
               BackPropagate()//fix errors, update weights
          }
     }
}

This is implemented as follows:

public bool Train()
{
    double currentError = 0;
    int currentIteration = 0;
    NeuralEventArgs Args = new NeuralEventArgs() ;

    do
    {
        currentError = 0;
        foreach (KeyValuePair<T, double[]> p in TrainingSet)
        {
            NeuralNet.ForwardPropagate(p.Value, p.Key);
            NeuralNet.BackPropagate();
            currentError += NeuralNet.GetError();
        }
                
        currentIteration++;
    
        if (IterationChanged != null && currentIteration % 5 == 0)
        {
            Args.CurrentError = currentError;
            Args.CurrentIteration = currentIteration;
            IterationChanged(this, Args);
        }

    } while (currentError > maximumError && currentIteration < 
    maximumIteration && !Args.Stop);

    if (IterationChanged != null)
    {
        Args.CurrentError = currentError;
        Args.CurrentIteration = currentIteration;
        IterationChanged(this, Args);
    }

    if (currentIteration >= maximumIteration || Args.Stop)   
        return false;//Training Not Successful
            
    return true;
}

Where ForwardPropagate(..) and BackPropagate() methods are as shown for a three layer network:

private void ForwardPropagate(double[] pattern, T output)
{
    int i, j;
    double total;
    //Apply input to the network
    for (i = 0; i < PreInputNum; i++)
    {
        PreInputLayer[i].Value = pattern[i];
    }
    //Calculate The First(Input) Layer's Inputs and Outputs
    for (i = 0; i < InputNum; i++)
    {
        total = 0.0;
        for (j = 0; j < PreInputNum; j++)
        {
            total += PreInputLayer[j].Value * PreInputLayer[j].Weights[i];
        }
        InputLayer[i].InputSum = total;
        InputLayer[i].Output = F(total);
    }
    //Calculate The Second(Hidden) Layer's Inputs and Outputs
    for (i = 0; i < HiddenNum; i++)
    {
        total = 0.0;
        for (j = 0; j < InputNum; j++)
        {
            total += InputLayer[j].Output * InputLayer[j].Weights[i];
        }

        HiddenLayer[i].InputSum = total;
        HiddenLayer[i].Output = F(total);
    }
    //Calculate The Third(Output) Layer's Inputs, Outputs, Targets and Errors
    for (i = 0; i < OutputNum; i++)
    {
        total = 0.0;
        for (j = 0; j < HiddenNum; j++)
        {
            total += HiddenLayer[j].Output * HiddenLayer[j].Weights[i];
        }

        OutputLayer[i].InputSum = total;
        OutputLayer[i].output = F(total);
        OutputLayer[i].Target = OutputLayer[i].Value.CompareTo(output) == 0 ? 1.0 : 0.0;
        OutputLayer[i].Error = (OutputLayer[i].Target - OutputLayer[i].output) *
                                       (OutputLayer[i].output) * (1 - OutputLayer[i].output);
        }
    }        
    
private void BackPropagate()
{
    int i, j;
    double total;
    //Fix Hidden Layer's Error
    for (i = 0; i < HiddenNum; i++)
    {
        total = 0.0;
        for (j = 0; j < OutputNum; j++)
        {
            total += HiddenLayer[i].Weights[j] * OutputLayer[j].Error;
        }
        HiddenLayer[i].Error = total;
    }
    //Fix Input Layer's Error
    for (i = 0; i < InputNum; i++)
    {
        total = 0.0;
        for (j = 0; j < HiddenNum; j++)
        {
            total += InputLayer[i].Weights[j] * HiddenLayer[j].Error;
        }
        InputLayer[i].Error = total;
    }
    //Update The First Layer's Weights
    for (i = 0; i < InputNum; i++)
    {
        for(j = 0; j < PreInputNum; j++)
        {
            PreInputLayer[j].Weights[i] +=
                LearningRate * InputLayer[i].Error * PreInputLayer[j].Value;
        }
    }
    //Update The Second Layer's Weights
    for (i = 0; i < HiddenNum; i++)
    {
        for (j = 0; j < InputNum; j++)
        {
            InputLayer[j].Weights[i] +=
                LearningRate * HiddenLayer[i].Error * InputLayer[j].Output;
        }
    }
    //Update The Third Layer's Weights
    for (i = 0; i < OutputNum; i++)
    {
        for (j = 0; j < HiddenNum; j++)
        {
            HiddenLayer[j].Weights[i] +=
                LearningRate * OutputLayer[i].Error * HiddenLayer[j].Output;
        }
    }
}

Testing the App

The program trains the network using bitmap images that are located in a folder. This folder must be in the following format:

As testing the classes requires to train the network first, there must be a folder in this format. "PATTERNS" and "ICONS" folders [depicted below] in the Debug folder fit this format.

Screenshot - fig2_sampleInput_thinner.png Screenshot - fig3_sampleInput_thinner.png

History

References & External Links

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalgreat work
Mikant
11:51 8 Feb '10  
thank you, Murat for providing people with such a great code. your code organisation is perfect (simple and powerful)
GeneralRe: great work
Murat Firat
3:02 9 Feb '10  
Thanks buddy, You are welcome Smile
Generaladdition
rasleen_13
6:04 6 Dec '09  
n also what do the iterations signify ?
Generalhigh n low ?
rasleen_13
6:00 6 Dec '09  
it's a great application
thanks a ton for providing us with it
but i have a question:
since i am new to neural networks...i tried to understand the working with the help of the discussions...but i still have a doubt:
what do high n low signify during pattern matching ?
GeneralRe: high n low ?
Murat Firat
0:08 7 Dec '09  
rasleen_13 wrote:
what do high n low signify during pattern matching ?

thanks but unfortunately I couldn't understand the questionConfused
QuestionHow can i know what is desired output of each node?
ranzan Pokhrel
17:01 14 Nov '09  
i ve one confusion...if i want to make a NN for pen recognition, how can i know the desired ouptup of each node??how the output of each node can be computed?
GeneralComplex Numbers
KadirErturk
20:59 22 Oct '09  
Did you ever try net with complex numbers. I mean input weights outputs are in complex forms (a+ib)

Kadir Erturk
GeneralRe: Complex Numbers
Murat Firat
8:55 24 Oct '09  
No, I didnt use any complex number form as input-output.
good luck,
Murat
Questionidentification value in Image Recognition with Neural Networks [modified]
hankia1411
21:40 2 Oct '09  
dear, mr. murat

in your Image Recognition with Neural Networks article, i have a question in identification process. what value that you compare in that process? is it pixel or biner? thanks a lot for your answer.


with honor,

destario F

modified on Saturday, October 3, 2009 3:29 AM

AnswerRe: identification value in Image Recognition with Neural Networks
Murat Firat
7:19 4 Oct '09  
Hi,
As input, array of pixel values [of image] is used for identification [just by checking the code].

good luck,
Murat.
GeneralAwesome!!!!
zorou
9:11 19 Aug '09  
Really help me with my job.

Many thanks!
Questionwhat is the activation function??????
shanaprasad2009
13:16 10 Jun '09  
what is the activation function??????
AnswerRe: what is the activation function??????
Scott Benner
9:36 25 Jun '09  
Here, I believe it is implied to be Sigmoid, since the training function is (T - F(x) ) x dF(x)/dx, where x is the sum of weights * inputs.

Sigmoid(x)        = 1 / ( 1 + exp( -x ) ) 
d Sigmoid(x) / dx = ( 1 + exp( -x ) )^-2 * ( -1 ) * exp( -x )
= Sigmoid(x) * ( 1 - Sigmoid(x) )
= Output * ( 1 - Output )
For the details, see the Logistic Function[^] in Wikipedia
Generalwebcame
zulham97
23:08 14 May '09  
How to add webcame application in draw area
Questionhey need help
Member 4530771
1:03 4 May '09  
m beginner to neural network. so i do not understand the code very well. so can i get the full documentation of the code or any place where they explain the algorithm of this code? can anyone help me please ?? Smile
AnswerRe: hey need help
SafarTimura
13:27 9 May '09  
the algorithm used here is the delta rule in backpropagation wikipeadia has a reasonable article on it.

from the code i beleive that momentum is also implimented in the learning rule.
however i cannot see fro just the article what activation function is being used it is most likely to be the sigmoid activation function but it could be the tanh function. or just a simple boolean activation function.

Usually the sigmoid function is better suited to classification problems such as the ones here.

James Binary Warrior.

Generalwhat is the convergence?
onuriztech
10:27 7 Apr '09  
Merhaba Murat Bey

Ben bilgisayar mühendisliğinde okuyorum.Soft computing dersiyle alakalı ANN ödevi için kodunuzu örnek aldım.Hatta ödevi göndereyim.

# Open source code of an ANN for learning & recognising patterns from images or videos
# Specify the domain: problem, objective, solution approach, results
# Document the basic concepts: neural architecture, learning algorithm, convergence
# Develop a code versions, such that the algorithm still converges:

* one version with modified neural architecture

Bu ödevde bahsedildiği gibi koddaki convergence,results,objective lerimiz neler?
ve modified neural architecture kısmını nasıl değiştirebilriz ki converge etmeye devam etsin?
Şimdiden teşekkürler.İyi günler

Onur FİDAN
bil müh 4.sınıf
GeneralRe: what is the convergence?
Murat Firat
2:20 8 Apr '09  
Onur selam,

2 de bahsedilen sanıyorum problem nedir, amaç nedir, problemi nasıl çözeriz, sonuçlar nelerdir gibisinden klasik şeyler. amaç (algoritma gibi) geleneksel yontemlerle çözülemeyen problemleri çözebilmek denebilir (bu uygulama için harfleri ayırabilmek mesela). sonuç her duruma göre değişir buna hiç girmeyeyim. convergence ne manaya geliyor bilmiyorum açıkçası, bunun için google benden daha yardımcı olur muhtemelen Smile

iyi çalışmalar,

Murat
QuestionCurrent Error
jimbobmcgee
10:00 30 Mar '09  
I appear to be missing something major here.

I have taken your PATTERNS folder and replaced the contents with a 16 random images taken from a Google Image Search. I have standardised the size of each to 64x64 and tried to train with 1, 2 and 3 layers.
My aim is to make copies of those images and do some image processing (e.g. change colours, add noise, scale/rotate, etc) and use your recognition app to try to match with the original 16 images.

However, my training run never seems to finish, as the Error value never seems to change -- it stays at 120.0 for each iteration (each image has a currentError of 7.5) -- and is being compared to the default Maximum Error setting of 1.1 (if I set this setting 121.0, the training completes, but recognition returns the same image every time).

What is supposed to happen on each iteration? Should the error value decrease over time? I have left this to run for up to three hours before, performing over 200 iterations and the error value never changes.

Why?
Generalproblem running and compiling
prophet86
9:15 17 Mar '09  
I'm sorry for probably noob-like question(s) and problem(s) but>
when i tried to run the demo it crashed (the windows program encountered an error and will end now screen) so i tired to compile the source and run it in Debug mode..
The buil process showed no errors, but when i tried to run it (in Visual Studio Express 2008) in the Debug mode ..

it stops on line 311 neuralNetwork.MaximumError = Double.Parse(textBoxMaxError.Text);
saying that imput string was not in correct format

i really don't know what can be wrong, if anyone can be so kind and help i would really appreciate it.

BR
GeneralRe: problem running and compiling
zimpzon
23:37 15 Mar '10  
It's probably your language settings.   The textbox contains '.' where ',' is exptected, or the other way around.
General[Message Deleted]
Member 4530771
4:15 15 Mar '09  

GeneralCode in VB.Net
mu'a
10:44 7 Mar '09  
help me, in vb.net
Generalwhat can i do to increase acuracy?
Member 4530771
5:00 3 Mar '09  
increase the number of layers?Confused Confused Confused
GeneralRe: what can i do to increase acuracy?
SafarTimura
13:21 9 May '09  
you can do a variety of things to increase the accuracy of neural networks.

1. you could run the simulation of learning for more iterations.
2. You could turn upthe learning rate slighty however be very careful with this because it can make the algorithm unstable and it can actually make the learning harder.
3. increase the momentum in the code so that it learns faster again becareful as this can do the same as the learning rate.
4. simply run the code again from a different starting vector so that the search is different.
5. incease the number of hidden nodes however this does increase the learning time by a considerable amount.

hopefully this helps.

James Binary Warrior.


Last Updated 30 Oct 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010