Click here to Skip to main content
15,887,434 members
Articles / Artificial Intelligence / Neural Networks

Multiple convolution neural networks approach for online handwriting recognition

Rate me:
Please Sign up or sign in to vote.
4.95/5 (37 votes)
9 Apr 2013CPOL8 min read 76.2K   25.1K   74  
The research focuses on the presentation of word recognition technique for an online handwriting recognition system which uses multiple component neural networks (MCNN) as the exchangeable parts of the classifier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using ANN.Perceptron;
namespace ANN.Perceptron.Network
{
    public class PatternRecognition:ForwardPropagation
    {
        ANN.Perceptron.Common.BaseControl parentControl;
        public PatternRecognition(ConvolutionNetwork neuronNet, ANN.Perceptron.Common.BaseControl ct)
            : base(neuronNet)
        {
            network = neuronNet;
            Letters = network.TagetOutputs;
            parentControl = ct;
            
        }
        public void ForwardpropagationThread(byte[] imageData,int imageWidth,int imageHeight,out Char label)
        {
            int nInput = network.Layers.First().NeuronCount;
            int nOutput = network.Layers.Last().NeuronCount;
            double[] inputVector = new double[nInput];  
            double[] targetOutputVector = new double[nOutput];
            double[] actualOutputVector = new double[nOutput];
            //
            Parallel.For(0, nInput, ii =>
            {
                inputVector[ii] =(double) 255/128 - 1.0;
                // one is white, -one is black
            });
            Parallel.For(0, nOutput, i =>
            {
                targetOutputVector[i] = 0.0;
                actualOutputVector[i] = 0.0;
            });
            //
          
            label = new char();

            Size smallestSize = new Size();
            if (network.InputDesignedPatternSize.Width >= imageWidth)
            {

                smallestSize.Width = imageWidth;
            }
            else
            {

                smallestSize.Width = network.InputDesignedPatternSize.Width;
            }
            if (network.InputDesignedPatternSize.Height >= imageHeight)
            {

                smallestSize.Height = imageHeight;
            }
            else
            {

                smallestSize.Height = network.InputDesignedPatternSize.Height;
            }

            //change byte input to double input;
            //pad to designedpatternwidth*designedpatternheight, convert to double precision

            /*TODO: Check potentially-changing upper bound expression "smallestSize.Height" which is now called only *once*,
            to ensure the new Parallel.For call matches behavior in the original for-loop
           (where this upper bound expression had previously been evaluated at the start of *every* loop iteration).*/
            Parallel.For(0, smallestSize.Height,ParallelOption, ii =>
            {
                for (int jj = 0; jj < smallestSize.Width; jj++)
                {
                    inputVector[jj + network.InputDesignedPatternSize.Width * ii] = (double)((int)(byte)imageData[jj + imageWidth * ii]) / 128.0 - 1.0; // one is white, -one is black
                }
            });

            CalculateNeuralNet(inputVector, nInput, actualOutputVector, nOutput, null, false);
            int iBestIndex = 0;
            double maxValue = -99.0;
            for (int ii = 0; ii < nOutput; ++ii)
            {
                if (actualOutputVector[ii] > maxValue)
                {
                    iBestIndex = ii;
                    maxValue = actualOutputVector[ii];
                }
            }
            //network have unknown output
            if (Letters.Count+1 == nOutput)
            {
                if (iBestIndex == nOutput - 1)
                {
                    label = network.UnknownOuput;
                }
                else
                {
                    label = Letters[iBestIndex];
                }
            }
            else
            {
                //network does not have unknown output
                label = Letters[iBestIndex];
            }
            string s = "Recognition process is completed";
            if (parentControl != null)
            {
                parentControl.Invoke(parentControl.DelegateAddObject, new Object[] { 0, s });
            }
        }
    }
}

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
Vietnam Maritime University
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions