Difference between revisions of "ANN MLP (Neural Network) in CSharp"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search
m (Reverted edits by Russel08 (Talk) to last revision by Emgucv)
Line 1: Line 1:
 +
----
 +
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 +
----
 +
=[http://ekygelymib.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 +
----
 +
=[http://ekygelymib.co.cc CLICK HERE]=
 +
----
 +
</div>
 
== System Requirement ==
 
== System Requirement ==
 
{| style="text-align:center" border="1px" cellpadding="10" cellspacing="0"
 
{| style="text-align:center" border="1px" cellpadding="10" cellspacing="0"
Line 18: Line 26:
  
 
== Source Code ==
 
== Source Code ==
<source lang="csharp">
+
&lt;source lang="csharp">
 
using System.Drawing;
 
using System.Drawing;
 
using Emgu.CV.Structure;
 
using Emgu.CV.Structure;
Line 29: Line 37:
  
 
#region Generate the traning data and classes
 
#region Generate the traning data and classes
Matrix<float> trainData = new Matrix<float>(trainSampleCount, 2);
+
Matrix&lt;float> trainData = new Matrix&lt;float>(trainSampleCount, 2);
Matrix<float> trainClasses = new Matrix<float>(trainSampleCount, 1);
+
Matrix&lt;float> trainClasses = new Matrix&lt;float>(trainSampleCount, 1);
  
Image<Bgr, Byte> img = new Image<Bgr, byte>(500, 500);
+
Image&lt;Bgr, Byte> img = new Image&lt;Bgr, byte>(500, 500);
  
Matrix<float> sample = new Matrix<float>(1, 2);
+
Matrix&lt;float> sample = new Matrix&lt;float>(1, 2);
Matrix<float> prediction = new Matrix<float>(1, 1);
+
Matrix&lt;float> prediction = new Matrix&lt;float>(1, 1);
  
Matrix<float> trainData1 = trainData.GetRows(0, trainSampleCount >> 1, 1);
+
Matrix&lt;float> trainData1 = trainData.GetRows(0, trainSampleCount >> 1, 1);
 
trainData1.SetRandNormal(new MCvScalar(200), new MCvScalar(50));
 
trainData1.SetRandNormal(new MCvScalar(200), new MCvScalar(50));
Matrix<float> trainData2 = trainData.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
+
Matrix&lt;float> trainData2 = trainData.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
 
trainData2.SetRandNormal(new MCvScalar(300), new MCvScalar(50));
 
trainData2.SetRandNormal(new MCvScalar(300), new MCvScalar(50));
  
Matrix<float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount >> 1, 1);
+
Matrix&lt;float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount >> 1, 1);
 
trainClasses1.SetValue(1);
 
trainClasses1.SetValue(1);
Matrix<float> trainClasses2 = trainClasses.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
+
Matrix&lt;float> trainClasses2 = trainClasses.GetRows(trainSampleCount >> 1, trainSampleCount, 1);
 
trainClasses2.SetValue(2);
 
trainClasses2.SetValue(2);
 
#endregion
 
#endregion
  
Matrix<int> layerSize = new Matrix<int>(new int[] { 2, 5, 1 });
+
Matrix&lt;int> layerSize = new Matrix&lt;int>(new int[] { 2, 5, 1 });
  
 
MCvANN_MLP_TrainParams parameters = new MCvANN_MLP_TrainParams();
 
MCvANN_MLP_TrainParams parameters = new MCvANN_MLP_TrainParams();
Line 60: Line 68:
 
   network.Train(trainData, trainClasses, null, null, parameters, Emgu.CV.ML.MlEnum.ANN_MLP_TRAINING_FLAG.DEFAULT);
 
   network.Train(trainData, trainClasses, null, null, parameters, Emgu.CV.ML.MlEnum.ANN_MLP_TRAINING_FLAG.DEFAULT);
  
   for (int i = 0; i < img.Height; i++)
+
   for (int i = 0; i &lt; img.Height; i++)
 
   {
 
   {
       for (int j = 0; j < img.Width; j++)
+
       for (int j = 0; j &lt; img.Width; j++)
 
       {
 
       {
 
         sample.Data[0, 0] = j;
 
         sample.Data[0, 0] = j;
Line 72: Line 80:
  
 
         // highlight the pixel depending on the accuracy (or confidence)
 
         // highlight the pixel depending on the accuracy (or confidence)
         img[i, j] = response < 1.5 ? new Bgr(90, 0, 0) : new Bgr(0, 90, 0);
+
         img[i, j] = response &lt; 1.5 ? new Bgr(90, 0, 0) : new Bgr(0, 90, 0);
 
       }
 
       }
 
   }
 
   }
Line 78: Line 86:
  
 
// display the original training samples
 
// display the original training samples
for (int i = 0; i < (trainSampleCount >> 1); i++)
+
for (int i = 0; i &lt; (trainSampleCount >> 1); i++)
 
{
 
{
 
   PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
 
   PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
Line 86: Line 94:
 
}
 
}
 
Emgu.CV.UI.ImageViewer.Show(img);
 
Emgu.CV.UI.ImageViewer.Show(img);
</source>
+
&lt;/source>
  
 
== Result ==
 
== Result ==
 
[[image:ANN_MLP.png]]
 
[[image:ANN_MLP.png]]

Revision as of 03:32, 24 November 2010



UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY


CLICK HERE


System Requirement

Component Requirement Detail
Emgu CV Version 1.5
Operation System Cross Platform

What is an Artificial Neural Network (ANN)

According to wikipedia,

An artificial neural network (ANN), usually called "neural network" (NN), is a mathematical model or computational model based on biological neural networks. It consists of an interconnected group of artificial neurons and processes information using a connectionist approach to computation. In most cases an ANN is an adaptive system that changes its structure based on external or internal information that flows through the network during the learning phase.

In more practical terms neural networks are non-linear statistical data modeling tools. They can be used to model complex relationships between inputs and outputs or to find patterns in data.

OpenCV implements Multi-Layer Perceptrons Neural Network, hence the name ANN_MLP.

Source Code

<source lang="csharp"> using System.Drawing; using Emgu.CV.Structure; using Emgu.CV.ML; using Emgu.CV.ML.Structure;

...

int trainSampleCount = 100;

  1. region Generate the traning data and classes

Matrix<float> trainData = new Matrix<float>(trainSampleCount, 2); Matrix<float> trainClasses = new Matrix<float>(trainSampleCount, 1);

Image<Bgr, Byte> img = new Image<Bgr, byte>(500, 500);

Matrix<float> sample = new Matrix<float>(1, 2); Matrix<float> prediction = new Matrix<float>(1, 1);

Matrix<float> trainData1 = trainData.GetRows(0, trainSampleCount >> 1, 1); trainData1.SetRandNormal(new MCvScalar(200), new MCvScalar(50)); Matrix<float> trainData2 = trainData.GetRows(trainSampleCount >> 1, trainSampleCount, 1); trainData2.SetRandNormal(new MCvScalar(300), new MCvScalar(50));

Matrix<float> trainClasses1 = trainClasses.GetRows(0, trainSampleCount >> 1, 1); trainClasses1.SetValue(1); Matrix<float> trainClasses2 = trainClasses.GetRows(trainSampleCount >> 1, trainSampleCount, 1); trainClasses2.SetValue(2);

  1. endregion

Matrix<int> layerSize = new Matrix<int>(new int[] { 2, 5, 1 });

MCvANN_MLP_TrainParams parameters = new MCvANN_MLP_TrainParams(); parameters.term_crit = new MCvTermCriteria(10, 1.0e-8); parameters.train_method = Emgu.CV.ML.MlEnum.ANN_MLP_TRAIN_METHOD.BACKPROP; parameters.bp_dw_scale = 0.1; parameters.bp_moment_scale = 0.1;

using (ANN_MLP network = new ANN_MLP(layerSize, Emgu.CV.ML.MlEnum.ANN_MLP_ACTIVATION_FUNCTION.SIGMOID_SYM, 1.0, 1.0)) {

  network.Train(trainData, trainClasses, null, null, parameters, Emgu.CV.ML.MlEnum.ANN_MLP_TRAINING_FLAG.DEFAULT);
  for (int i = 0; i < img.Height; i++)
  {
     for (int j = 0; j < img.Width; j++)
     {
        sample.Data[0, 0] = j;
        sample.Data[0, 1] = i;
        network.Predict(sample, prediction);
        // estimates the response and get the neighbors' labels
        float response = prediction.Data[0,0];
        // highlight the pixel depending on the accuracy (or confidence)
        img[i, j] = response < 1.5 ? new Bgr(90, 0, 0) : new Bgr(0, 90, 0);
     }
  }

}

// display the original training samples for (int i = 0; i < (trainSampleCount >> 1); i++) {

  PointF p1 = new PointF(trainData1[i, 0], trainData1[i, 1]);
  img.Draw(new CircleF(p1, 2), new Bgr(255, 100, 100), -1);
  PointF p2 = new PointF((int)trainData2[i, 0], (int)trainData2[i, 1]);
  img.Draw(new CircleF(p2, 2), new Bgr(100, 255, 100), -1);

} Emgu.CV.UI.ImageViewer.Show(img); </source>

Result

ANN MLP.png