ANN MLP (Neural Network) in CSharp

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search

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

Emgu CV 3.x

Click to view source code

using System.Drawing;
using Emgu.CV.Structure;
using Emgu.CV.ML;
using Emgu.CV.ML.Structure;

...

int trainSampleCount = 100;

#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);
#endregion

using(Matrix<int> layerSize = new Matrix<int>(new int[] { 2, 5, 1 }))
using(Mat layerSizeMat = layerSize.Mat)

using (TrainData td = new TrainData(trainData, MlEnum.DataLayoutType.RowSample, trainClasses))
using (ANN_MLP network = new ANN_MLP())
{ 
   network.SetLayerSizes(layerSizeMat);
   network.SetActivationFunction(ANN_MLP.AnnMlpActivationFunction.SigmoidSym, 0, 0);
   network.TermCriteria = new MCvTermCriteria(10, 1.0e-8);
   network.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop, 0.1, 0.1);
   network.Train(td, (int) Emgu.CV.ML.MlEnum.AnnMlpTrainingFlag.Default);

#if !NETFX_CORE
   String fileName = Path.Combine(Path.GetTempPath(), "ann_mlp_model.xml");
   network.Save(fileName);
   if (File.Exists(fileName))
      File.Delete(fileName);
#endif

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

Emgu CV 2.x

Click to view source code

using System.Drawing;
using Emgu.CV.Structure;
using Emgu.CV.ML;
using Emgu.CV.ML.Structure;

...

int trainSampleCount = 100;

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

Result

ANN MLP.png