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