Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Image Recognition with Neural Networks

Rate me:
Please Sign up or sign in to vote.
4.82/5 (89 votes)
30 Oct 2007CPOL4 min read 947.3K   46.2K   286  
This article contains a brief description of BackPropagation Artificial Neural Network and its implementation for Image Recognition
/*##########################################################################
 * 
 * BP2Layer.cs
 * -------------------------------------------------------------------------
 * By
 * Murat FIRAT, June 2007
 * muratti24@gmail.com
 * 
 * -------------------------------------------------------------------------
 * Description:
 * BP2Layer.cs Implements Two Layer Backpropagation Neural Network
 * 
 * -------------------------------------------------------------------------
 * Notes:
 * If training process takes too long, modify (mostly increase) learning rate 
 * and initial weight.
 * 
 * To modify initial weight, change x(0<x<1) and y(0<y<100) in the following
 * code segment(in InitializeNetwork() function):
 * 
 * PreInputLayer[i].Weights[j] = x +((double)rand.Next(0, y) / 100);                  
 * InputLayer[i].Weights[j] = x + ((double)rand.Next(0, y) / 100);
 * 
 * -------------------------------------------------------------------------
 ###########################################################################*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ANNBase;

namespace ANN
{
    class BP2Layer
    {
        private PreInput[] PreInputLayer;
        private Input[] InputLayer; 
        private Output[] OutputLayer;

        private int PreInputNum;
        private int InputNum;
        private int OutputNum;

        private ArrayList OutputSet;

        private double LearningRate = 0.2; 

        public BP2Layer(int preInputNum, int inputNum, int outputNum)
        {
            PreInputNum = preInputNum;
            InputNum = inputNum;
            OutputNum = outputNum;

            PreInputLayer = new PreInput[PreInputNum];
            InputLayer = new Input[InputNum];
            OutputLayer = new Output[OutputNum];
            OutputSet = new ArrayList();

            InitializeNetwork();
        }

        public void Train(ArrayList TrainingInputs, ArrayList TrainingOutputs)
        {
            foreach (string s in TrainingOutputs)
            {
                if (!OutputSet.Contains(s))
                {
                    OutputSet.Add(s);
                }
            }

            for (int i = 0; i < TrainingInputs.Count; i++)
            {
                ForwardPropagate((double[])TrainingInputs[i], (string)TrainingOutputs[i]);
                BackPropagate();
            }
        }

        public void Recognize(double[] pattern, ref string Ohigh, ref double Phigh,
                                                ref string Olow, ref double Plow)
        {
            int i, j;
            double total = 0.0;
            double max = -1;

            for (i = 0; i < PreInputNum; i++)
            {
                PreInputLayer[i].Value = pattern[i];
            }

            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);
            }

               
            for (i = 0; i < OutputNum; i++)
            {
                total = 0.0;
                for (j = 0; j < InputNum; j++)
                {
                    total += InputLayer[j].Output * InputLayer[j].Weights[i];
                }
                OutputLayer[i].InputSum = total;
                OutputLayer[i].output = F(total);
                if (OutputLayer[i].output > max)
                {
                    Olow = Ohigh;
                    Plow = max;
                    max = OutputLayer[i].output;
                    Ohigh = (string)OutputSet[i];
                    Phigh = max;
                }
            }
        }

        private double F(double x) { return (1 / (1 + Math.Exp(-x))); }

        public double GetTotalError(ArrayList TrainingInputs, ArrayList TrainingOutputs)
        {
            double total = 0.0;
            for (int i = 0; i < TrainingInputs.Count; i++)
            {
                ForwardPropagate((double[])TrainingInputs[i], (string)TrainingOutputs[i]);
                for (int j = 0; j < OutputNum; j++)
                {
                    total += Math.Pow((OutputLayer[j].Target - OutputLayer[j].output), 2) / 2;                    
                }
            }
            return total;
        }

        public void SaveNetwork(string FileName, int AvHeight, int AvWidth)
        {
            ArrayList AL = new ArrayList();
            AL.Add("2Layer");
            AL.Add(OutputSet);
            AL.Add(PreInputLayer);
            AL.Add(InputLayer);
            AL.Add(OutputLayer);
            AL.Add(AvHeight);
            AL.Add(AvWidth);
            FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate);
            BinaryFormatter BF = new BinaryFormatter();            
            BF.Serialize(FS, AL);
            FS.Close();
        }

        public void LoadNetwork(string FileName, ref int AvHeight, ref int AvWidth)
        {
            FileStream FS = new FileStream(FileName, FileMode.Open);
            BinaryFormatter BF = new BinaryFormatter();
            ArrayList AL = (ArrayList)BF.Deserialize(FS);
            string Ver = (string)AL[0];
            if (Ver != "2Layer")
            {
                throw new Exception("The Loaded Network Does Not Belong To 2 Layer Network But Belongs to " + Ver);
            }
            OutputSet = (ArrayList)AL[1];
            PreInputLayer = (PreInput[])AL[2];
            InputLayer = (Input[])AL[3];
            OutputLayer = (Output[])AL[4];
            AvHeight = (int)AL[5];
            AvWidth = (int)AL[6];
            FS.Close();
        }

        private void InitializeNetwork()
        {
            int i, j;
            Random rand = new Random();
            for (i = 0; i < PreInputNum; i++)
            {
                PreInputLayer[i].Weights = new double[InputNum];
                for (j = 0; j < InputNum; j++)
                {
                    PreInputLayer[i].Weights[j] = 0.01 + ((double)rand.Next(0, 5) / 100);
                }
            }

            for (i = 0; i < InputNum; i++)
            {
                InputLayer[i].Weights = new double[OutputNum];
                for (j = 0; j < OutputNum; j++)
                {
                    InputLayer[i].Weights[j] = 0.01 + ((double)rand.Next(0, 5) / 100); 
                }
            }
        }

        private void BackPropagate()
        {
            int i, j;
            double total;

            for (i = 0; i < InputNum; i++)
            {
                total = 0.0;
                for (j = 0; j < OutputNum; j++)
                {
                    total += InputLayer[i].Weights[j] * OutputLayer[j].Error;
                }
                InputLayer[i].Error = total;
            }

            for (i = 0; i < InputNum; i++)
            {
                for (j = 0; j < PreInputNum; j++)
                {
                    PreInputLayer[j].Weights[i] +=
                        LearningRate * InputLayer[i].Error * PreInputLayer[j].Value;
                }
            }

            for (i = 0; i < OutputNum; i++)
            {
                for (j = 0; j < InputNum; j++)
                {
                    InputLayer[j].Weights[i] +=
                        LearningRate * OutputLayer[i].Error * InputLayer[j].Output;
                }
            }               
        }

        private void ForwardPropagate(double[] pattern, string output)
        {
            int i, j;
            double total = 0.0;

            for (i = 0; i < PreInputNum; i++)
            {
                PreInputLayer[i].Value = pattern[i];
            }

            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);
            }

            for (i = 0; i < OutputNum; i++)
            {
                total = 0.0;
                for (j = 0; j < InputNum; j++)
                {
                    total += InputLayer[j].Output * InputLayer[j].Weights[i];
                }

                OutputLayer[i].InputSum = total;
                OutputLayer[i].output = F(total);
                OutputLayer[i].Target = (((string)OutputSet[i]) == output ? 1.0 : 0.0);
                OutputLayer[i].Error = (OutputLayer[i].Target - OutputLayer[i].output) * (OutputLayer[i].output) * (1 - OutputLayer[i].output);
            }
        }      
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions