12,635,269 members (24,474 online)
alternative version

1.6M views
272 bookmarked
Posted

# Image Recognition with Neural Networks

, 30 Oct 2007 CPOL
 Rate this:
This article contains a brief description of BackPropagation Artificial Neural Network and its implementation for Image Recognition

## 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.

## 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:

• Apply input to the network.
• Calculate the output.
• Compare the resulting output with the desired output for the given input. This is called the error.
• Modify the weights for all neurons using the error.
• Repeat the process until the error reaches an acceptable value (e.g. error < 1%), which means that the NN was trained successfully, or if we reach a maximum count of iterations, which means that the NN training was not successful.

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:

• There must be one (input) folder that contains input images [*.bmp].
• Each image's name is the target (or output) value for the network (the pixel values of the image are the inputs, of course) .

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.

## History

• 30th September, 2007: Simplified the app
• 24th June, 2007: Initial Release

## Share

 Software Developer (Senior) Turkey
Has BS degree on computer science, working as software engineer in istanbul.

## You may also be interested in...

 ImageProcessing.ToMatrix denny_cucu14-Jun-08 23:30 denny_cucu 14-Jun-08 23:30
 Re: ImageProcessing.ToMatrix Murat Firat17-Jun-08 11:05 Murat Firat 17-Jun-08 11:05
 Re: ImageProcessing.ToMatrix denny_cucu28-Jun-08 23:25 denny_cucu 28-Jun-08 23:25
 Re: ImageProcessing.ToMatrix Himdevi24-Mar-12 21:18 Himdevi 24-Mar-12 21:18
 Re: ImageProcessing.ToMatrix Murat Firat25-Mar-12 12:55 Murat Firat 25-Mar-12 12:55
 Re: ImageProcessing.ToMatrix Himdevi26-Mar-12 5:22 Himdevi 26-Mar-12 5:22
 Image Detection YehudaG12-May-08 5:12 YehudaG 12-May-08 5:12
 what is the initial value of output layer??? sandipmuk7-May-08 22:18 sandipmuk 7-May-08 22:18
 Re: what is the initial value of output layer??? Murat Firat11-May-08 21:22 Murat Firat 11-May-08 21:22
 whats the network archeticture? jamilkhan00723-Apr-08 22:46 jamilkhan007 23-Apr-08 22:46
 Re: whats the network archeticture? Murat Firat23-Apr-08 23:32 Murat Firat 23-Apr-08 23:32
 Re: whats the network archeticture? jamilkhan00727-Apr-08 0:52 jamilkhan007 27-Apr-08 0:52
 Re: whats the network archeticture? Murat Firat27-Apr-08 12:05 Murat Firat 27-Apr-08 12:05
 Re: whats the network archeticture? jamilkhan0072-May-08 23:29 jamilkhan007 2-May-08 23:29
 can u give me any guide lines to make it an eye classifier (input is an eye r not?)? (Jameel)
 Face Detection Nueral Network? jamilkhan00718-Apr-08 2:53 jamilkhan007 18-Apr-08 2:53
 Image Recognition Advanced Engine Development ankswe15-Apr-08 4:51 ankswe 15-Apr-08 4:51
 Excellent! newbie0827-Mar-08 2:00 newbie08 27-Mar-08 2:00
 Re: Excellent! Murat Firat28-Mar-08 22:52 Murat Firat 28-Mar-08 22:52
 Re: You are awesome! Murat Firat23-Mar-08 6:29 Murat Firat 23-Mar-08 6:29
 pattern recognition K|nS|ayer28-Feb-08 22:01 K|nS|ayer 28-Feb-08 22:01
 Re: pattern recognition Murat Firat29-Feb-08 22:29 Murat Firat 29-Feb-08 22:29
 pattern recognition method rie athena6-Feb-08 15:00 rie athena 6-Feb-08 15:00
 Re: pattern recognition method Murat Firat7-Feb-08 8:01 Murat Firat 7-Feb-08 8:01
 Threshold Value cbc100031-Dec-07 15:40 cbc1000 31-Dec-07 15:40
 Re: Threshold Value Murat Firat1-Jan-08 3:14 Murat Firat 1-Jan-08 3:14
 About the pattern and input randy1014198326-Nov-07 3:51 randy10141983 26-Nov-07 3:51
 Re: About the pattern and input Murat Firat26-Nov-07 5:26 Murat Firat 26-Nov-07 5:26
 About demo project newrocker24-Nov-07 21:27 newrocker 24-Nov-07 21:27
 Re: About demo project Murat Firat25-Nov-07 11:08 Murat Firat 25-Nov-07 11:08
 Questions randy1014198324-Nov-07 17:43 randy10141983 24-Nov-07 17:43
 Re: Questions Murat Firat25-Nov-07 11:01 Murat Firat 25-Nov-07 11:01
 Doubt in Input and Hidden Node. Vimalr18-Nov-07 20:24 Vimalr 18-Nov-07 20:24
 Re: Doubt in Input and Hidden Node. Murat Firat19-Nov-07 2:09 Murat Firat 19-Nov-07 2:09
 Reg deciding the layer units i_coder15-Nov-07 7:57 i_coder 15-Nov-07 7:57
 Re: Reg deciding the layer units Murat Firat15-Nov-07 21:15 Murat Firat 15-Nov-07 21:15
 Can I use your Neural network implementation for this... Jean-Francois Dufour12-Nov-07 23:42 Jean-Francois Dufour 12-Nov-07 23:42
 Re: Can I use your Neural network implementation for this... Murat Firat13-Nov-07 9:36 Murat Firat 13-Nov-07 9:36
 Excellent merlin98131-Oct-07 5:01 merlin981 31-Oct-07 5:01
 Re: Excellent Murat Firat1-Nov-07 0:10 Murat Firat 1-Nov-07 0:10
 Error in HiddenNum.cs CBrauer4-Oct-07 9:07 CBrauer 4-Oct-07 9:07
 Re: Error in HiddenNum.cs Murat Firat30-Oct-07 14:16 Murat Firat 30-Oct-07 14:16
 error in backpropagation code leeyang114012-Sep-07 10:01 leeyang1140 12-Sep-07 10:01
 Re: error in backpropagation code Murat Firat12-Sep-07 11:31 Murat Firat 12-Sep-07 11:31
 Re: error in backpropagation code leeyang114012-Sep-07 12:40 leeyang1140 12-Sep-07 12:40
 Re: error in backpropagation code Murat Firat12-Sep-07 18:50 Murat Firat 12-Sep-07 18:50
 About Question Of Hidden Layer cbc100011-Sep-07 22:49 cbc1000 11-Sep-07 22:49
 Re: About Question Of Hidden Layer Murat Firat12-Sep-07 4:00 Murat Firat 12-Sep-07 4:00
 Question belenong10-Sep-07 19:02 belenong 10-Sep-07 19:02
 Re: Question Murat Firat11-Sep-07 0:29 Murat Firat 11-Sep-07 0:29
 Last Visit: 31-Dec-99 19:00     Last Update: 10-Dec-16 17:34 Refresh « Prev1234 Next »