Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Large pattern recognition system using multi neural networks

, 31 May 2012
Tutorials of using multi neural networks for large pattern recognition system, handwriting recognition system
capital_letter_v2.zip
capital letter v2.nnt
digit_v2.zip
digit v2.nnt
Drawing_samples.zip
Drawing samples
angle and beast.dtl
draw bitmap.dtl
draw image hum 1234.dtl
hell vs heaven.dtl
hello how are you.dtl
hope good.dtl
Multi neurons.dtl
need a pHd Scholarship.dtl
quick and slow.dtl
Quick and snow show.dtl
sample.dtl
thank you.dtl
wellcome to VIMARU.dtl
work hard happy.dtl
Handwriting_recognition_system_demo.zip
Handwriting recognition system demo
Config
DocToolkit.dll
DrawTools.dll
Neurons.dll
NNControl.dll
UNIPENviewer.exe
UPImage.dll
Handwriting_recognition_system_source.zip
Handwriting recognition system source
DocToolkit
bin
Debug
Release
DocToolkit.dll
DocToolkit.csproj.user
DocToolkit.snk
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
DocToolkit.dll
TempPE
DrawTools
about.bmp
App.ico
bin
Debug
Release
DocToolkit.dll
DrawTools.dll
DrawTools.csproj.user
ellipse.bmp
Ellipse.cur
line.bmp
Line.cur
new.bmp
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
DrawTools.dll
DrawTools.DrawArea.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
open.bmp
pencil.bmp
Pencil.cur
pointer.bmp
PolyHandle.cur
rectangle.bmp
Rectangle.cur
Resources
save.bmp
Neurons.dll
NNControl
bin
Debug
Neurons.pdb
UP-NeuralTraining.dll
UP-NeuralTraining.pdb
UPImage.pdb
Release
DocToolkit.dll
DrawTools.dll
Neurons.dll
Neurons.pdb
NNControl.dll
NNControl.pdb
UPImage.dll
UPImage.pdb
Common
NNTesting
NNTraining
obj
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties.Resources.Designer.cs.dll
UP-NeuralTraining.dll
UP-NeuralTraining.pdb
UPControl.Common.BaseControl.resources
UPControl.Common.UPTemplateControl.resources
UPControl.FlashForm.resources
UPControl.NNTraining.UP_NNTrainingControl.resources
UPControl.TrainingParametersForm.resources
UPControl.UPViewer.UpImageViewer.resources
UP_NeuralTraining.FlashForm.resources
UP_NeuralTraining.TrainingParametersForm.resources
UP_NeuralTraining.UP_NNTrainingControl.resources
Release
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NNControl.Common.UPTemplateControl.resources
NNControl.dll
NNControl.FlashForm.resources
NNControl.NNTesting.NNTestingControl.resources
NNControl.NNTraining.ConvolutionForm.resources
NNControl.NNTraining.CreateNetworkForm.resources
NNControl.NNTraining.FullConnectedForm.resources
NNControl.NNTraining.InputLayerForm.resources
NNControl.NNTraining.OutputLayerForm.resources
NNControl.NNTraining.UP_NNTrainingControl.resources
NNControl.pdb
NNControl.Properties.Resources.resources
NNControl.TrainingParametersForm.resources
NNControl.UPViewer.UpImageViewer.resources
ResolveAssemblyReference.cache
TempPE
Properties.Resources.Designer.cs.dll
Properties
Resources
btnBack.png
btnDrag.png
btnFitToScreen.png
btnNext.png
btnOpen.png
btnPreview.png
btnRotate270.png
btnRotate90.png
btnSelect.png
btnZoomIn.png
btnZoomOut.png
circle.png
clear.png
color_line.png
cry.png
document-new.png
Drag.cur
draw_line.png
ellipse.png
export.png
file.png
fingerprint-recognition.png
folder-open.png
folder.png
folders_explorer.png
Grab.cur
home.png
label-link.png
pointer.png
rectangle.png
save_accept.png
script_(stop).gif
smile.png
stock_draw-line.png
Stop sign.png
Upload.png
user-group-new.png
UPViewer
UNIPENviewer
UNIPENviewer.suo
bin
Debug
Config
Neurons.pdb
UNIPENviewer.vshost.exe
UNIPENviewer.vshost.exe.manifest
UPImage.pdb
Release
Config
DocToolkit.dll
DrawTools.dll
Neurons.dll
Neurons.pdb
NNControl.dll
NNControl.pdb
UNIPENviewer.exe
UNIPENviewer.pdb
UNIPENviewer.vshost.exe
UNIPENviewer.vshost.exe.manifest
UPImage.dll
UPImage.pdb
obj
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
UNIPENviewer.MainForm.resources
UNIPENviewer.Properties.Resources.resources
x86
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
UNIPENviewer.exe
UNIPENviewer.Form1.resources
UNIPENviewer.pdb
UNIPENviewer.Properties.Resources.resources
Properties
Settings.settings
UPImage.dll
lower_case_letter_v2.zip
lower case letter v2.nnt
wellcome_to_vimaru.zip
vi du.dtl
wellcom to VIMARU.dtl
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using NeuralNetworkLibrary;
namespace HandwrittenRecognition
{
    class NNTestPatterns : NeuralNetworkLibrary.NNForwardPropagation
    {
     
        #region Parametters
           
           private MnistDatabase _MnistDataSet;
           private uint _iMisNum;
           private uint _iNextPattern;
           Mainform _form;
        #endregion
        public NNTestPatterns(NeuralNetwork neuronNet, MnistDatabase testtingSet, Preferences preferences, bool testingDataReady,
                            ManualResetEvent eventStop,
                            ManualResetEvent eventStopped,
                            HandwrittenRecognition.Mainform form, List<Mutex> mutexs)
        {
            m_currentPatternIndex = 0;
            _bDataReady = testingDataReady;
            _NN = neuronNet;
            _iNextPattern = 0;
            m_EventStop = eventStop;
            m_EventStopped = eventStopped;
            _form = form;
            m_HiPerfTime = new HiPerfTimer();
            m_nImages = (uint) testtingSet.m_pImagePatterns.Count;
 
            //Initialize Gaussian Kernel
            m_Preferences = preferences;
            GetGaussianKernel(preferences.m_dElasticSigma);
            _MnistDataSet = testtingSet;
            m_Mutexs = mutexs;
        }
        public NNTestPatterns(NeuralNetwork neuronNet, Preferences preferences, 
                            HandwrittenRecognition.Mainform form, List<Mutex> mutexs)
        {
            m_currentPatternIndex = 0;
            _bDataReady = true;
            _NN = neuronNet;
            _iNextPattern = 0;
            m_EventStop = null;
            m_EventStopped = null;
            _form = form;
            m_HiPerfTime = new HiPerfTimer();
            m_nImages = 0;
            _iMisNum = 0;

            //Initialize Gaussian Kernel
            m_Preferences = preferences;
            GetGaussianKernel(preferences.m_dElasticSigma);
            _MnistDataSet = null;
            m_Mutexs = mutexs;
        }
        public void PatternsTestingThread(int iPatternNum)
        {
            // thread for backpropagation training of NN
            //
            // thread is "owned" by the doc, and accepts a pointer to the doc
            // continuously backpropagates until m_bThreadAbortFlag is set to TRUE  	
            double[] inputVector = new double[841];  // note: 29x29, not 28x28
            double[] targetOutputVector = new double[10];
            double[] actualOutputVector = new double[10];
            //
            for (int i = 0; i < 841; i++)
            {
                inputVector[i] = 0.0;
            }
            for (int i = 0; i < 10; i++)
            {
                targetOutputVector[i] = 0.0;
                actualOutputVector[i] = 0.0;

            }
            //
            byte label = 0;
            int ii, jj;


            var memorizedNeuronOutputs = new NNNeuronOutputsList();
            //prepare for training

            m_HiPerfTime.Start();

            while (_iNextPattern < iPatternNum)
            {
                m_Mutexs[1].WaitOne();

                byte[] grayLevels = new byte[m_Preferences.m_nRowsImages * m_Preferences.m_nColsImages];
                //iSequentialNum = m_MnistDataSet.GetCurrentPatternNumber(m_MnistDataSet.m_bFromRandomizedPatternSequence);
                _MnistDataSet.m_pImagePatterns[(int)_iNextPattern].pPattern.CopyTo(grayLevels, 0);
                label = _MnistDataSet.m_pImagePatterns[(int)_iNextPattern].nLabel;
                if (label < 0) label = 0;
                if (label > 9) label = 9;

                // pad to 29x29, convert to double precision

                for (ii = 0; ii < 841; ++ii)
                {
                    inputVector[ii] = 1.0;  // one is white, -one is black
                }

                // top row of inputVector is left as zero, left-most column is left as zero 

                for (ii = 0; ii < MyDefinations.g_cImageSize; ++ii)
                {
                    for (jj = 0; jj < MyDefinations.g_cImageSize; ++jj)
                    {
                        inputVector[1 + jj + 29 * (ii + 1)] = (double)((int)(byte)grayLevels[jj + MyDefinations.g_cImageSize * ii]) / 128.0 - 1.0;  // one is white, -one is black
                    }
                }

                // desired output vector

                for (ii = 0; ii < 10; ++ii)
                {
                    targetOutputVector[ii] = -1.0;
                }
                targetOutputVector[label] = 1.0;
                // forward calculate through the neural net

                CalculateNeuralNet(inputVector, 841, actualOutputVector, 10, memorizedNeuronOutputs, false);

                int iBestIndex = 0;
                double maxValue = -99.0;

                for (ii = 0; ii < 10; ++ii)
                {
                    if (actualOutputVector[ii] > maxValue)
                    {
                        iBestIndex = ii;
                        maxValue = actualOutputVector[ii];
                    }
                }
                string s = "";
                if (iBestIndex != label)
                {

                    _iMisNum++;
                    s = "Pattern No:" + _iNextPattern.ToString() + " Recognized value:" + iBestIndex.ToString() + " Actual value:" + label.ToString() ;
                    if (_form != null)
                        _form.Invoke(_form._DelegateAddObject, new Object[] { 6, s });


                }
                else
                {
                    s = _iNextPattern.ToString() + ", Mis Nums:" + _iMisNum.ToString();
                    if (_form != null)
                        _form.Invoke(_form._DelegateAddObject, new Object[] { 7, s });
                }
                // check if thread is cancelled
                if (m_EventStop.WaitOne(0, true))
                {
                    // clean-up operations may be placed here
                    // ...
                    s = String.Format("Mnist Testing thread: {0} stoped", Thread.CurrentThread.Name);
                    // Make synchronous call to main form.
                    // MainForm.AddString function runs in main thread.
                    // To make asynchronous call use BeginInvoke
                    if (_form != null)
                    {
                        _form.Invoke(_form._DelegateAddObject, new Object[] { 8, s });
                    }

                    // inform main thread that this thread stopped
                    m_EventStopped.Set();
                    m_Mutexs[1].ReleaseMutex();
                    return;
                }
                _iNextPattern++;
                m_Mutexs[1].ReleaseMutex();
            }
            {
                string s= String.Format("Mnist Testing thread: {0} stoped", Thread.CurrentThread.Name);
                _form.Invoke(_form._DelegateAddObject, new Object[] { 8, s });
            }
        }
        public void PatternRecognitionThread(int iPatternNo)
        {
            // thread for backpropagation training of NN
            //
            // thread is "owned" by the doc, and accepts a pointer to the doc
            // continuously backpropagates until m_bThreadAbortFlag is set to TRUE  	
            double[] inputVector = new double[841];  // note: 29x29, not 28x28
            double[] targetOutputVector = new double[10];
            double[] actualOutputVector = new double[10];
            //
            for (int i = 0; i < 841; i++)
            {
                inputVector[i] = 0.0;
            }
            for (int i = 0; i < 10; i++)
            {
                targetOutputVector[i] = 0.0;
                actualOutputVector[i] = 0.0;

            }
           

            byte label = 0;
            int ii, jj;
           

            var memorizedNeuronOutputs = new NNNeuronOutputsList();
            //prepare for training
            _iNextPattern = 0;
            _iMisNum = 0;

           
                m_Mutexs[1].WaitOne();
                if (_iNextPattern == 0)
                {
                    m_HiPerfTime.Start();
                }
                byte[] grayLevels = new byte[m_Preferences.m_nRowsImages * m_Preferences.m_nColsImages];
                _MnistDataSet.m_pImagePatterns[iPatternNo].pPattern.CopyTo(grayLevels, 0);
                label = _MnistDataSet.m_pImagePatterns[iPatternNo].nLabel;
                _iNextPattern++;
                
                if (label < 0) label = 0;
                if (label > 9) label = 9;

                // pad to 29x29, convert to double precision

                for (ii = 0; ii < 841; ++ii)
                {
                    inputVector[ii] = 1.0;  // one is white, -one is black
                }

                // top row of inputVector is left as zero, left-most column is left as zero 

                for (ii = 0; ii < MyDefinations.g_cImageSize; ++ii)
                {
                    for (jj = 0; jj < MyDefinations.g_cImageSize; ++jj)
                    {
                        inputVector[1 + jj + 29 * (ii + 1)] = (double)((int)(byte)grayLevels[jj + MyDefinations.g_cImageSize * ii]) / 128.0 - 1.0;  // one is white, -one is black
                    }
                }

                // desired output vector

                for (ii = 0; ii < 10; ++ii)
                {
                    targetOutputVector[ii] = -1.0;
                }
                targetOutputVector[label] = 1.0;
                // forward calculate through the neural net

                CalculateNeuralNet(inputVector, 841, actualOutputVector, 10, memorizedNeuronOutputs, false);
                int iBestIndex = 0;
                double maxValue = -99.0;

                for (ii = 0; ii < 10; ++ii)
                {
                    if (actualOutputVector[ii] > maxValue)
                    {
                        iBestIndex = ii;
                        maxValue = actualOutputVector[ii];
                    }
                }
                
                string s = iBestIndex.ToString();
                _form.Invoke(_form._DelegateAddObject, new Object[] { 2, s });
                // check if thread is cancelled
                m_Mutexs[1].ReleaseMutex();
               
        }
        public void PatternRecognitionThread(byte[] grayLevels)
        {
            // thread for backpropagation training of NN
            //
            // thread is "owned" by the doc, and accepts a pointer to the doc
            // continuously backpropagates until m_bThreadAbortFlag is set to TRUE  	
            double[] inputVector = new double[841];  // note: 29x29, not 28x28
            double[] targetOutputVector = new double[10];
            double[] actualOutputVector = new double[10];
            //
            for (int i = 0; i < 841; i++)
            {
                inputVector[i] = 0.0;
            }
            for (int i = 0; i < 10; i++)
            {
                targetOutputVector[i] = 0.0;
                actualOutputVector[i] = 0.0;

            }
            //
            
            byte label = 0;
            int ii, jj;


            var memorizedNeuronOutputs = new NNNeuronOutputsList();
           

            m_Mutexs[1].WaitOne();
            if (_iNextPattern == 0)
            {
                m_HiPerfTime.Start();
            }
            if (label < 0) label = 0;
            if (label > 9) label = 9;

            // pad to 29x29, convert to double precision

            for (ii = 0; ii < 841; ++ii)
            {
                inputVector[ii] = 1.0;  // one is white, -one is black
            }

            // top row of inputVector is left as zero, left-most column is left as zero 

            for (ii = 0; ii < MyDefinations.g_cImageSize; ++ii)
            {
                for (jj = 0; jj < MyDefinations.g_cImageSize; ++jj)
                {
                    inputVector[1 + jj + 29 * (ii + 1)] = (double)((int)(byte)grayLevels[jj + MyDefinations.g_cImageSize * ii]) / 128.0 - 1.0;  // one is white, -one is black
                }
            }

            // desired output vector

            for (ii = 0; ii < 10; ++ii)
            {
                targetOutputVector[ii] = -1.0;
            }
            targetOutputVector[label] = 1.0;
            // forward calculate through the neural net

            CalculateNeuralNet(inputVector, 841, actualOutputVector, 10, memorizedNeuronOutputs, false);
            int iBestIndex = 0;
            double maxValue = -99.0;

            for (ii = 0; ii < 10; ++ii)
            {
                if (actualOutputVector[ii] > maxValue)
                {
                    iBestIndex = ii;
                    maxValue = actualOutputVector[ii];
                }
            }

            string s = iBestIndex.ToString();
            _form.Invoke(_form._DelegateAddObject, new Object[] { 1, s });
            // check if thread is cancelled
            
            m_Mutexs[1].ReleaseMutex();

        }
    }   
}

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)

Share

About the Author

Vietdungiitb
Vietnam Maritime University
Vietnam Vietnam
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 1 Jun 2012
Article Copyright 2012 by Vietdungiitb
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid