Shape Classification


Tip
It is possible to train an ANN using a sequence of shapes. Each shape can be represented as a sequence of numbers. These numbers can be applied directly to an ANN. However, it is always recommended to process the shape data before using it for training. This processing can be simple mapping operations, or it can be very complicated to highlight those properties that are important for the experiment (see figure below). For instance, the data can be in the time domain or in the frequency domain.

Classification

Problem 1
Create a New Project called CardiRose (You must select: Multi-layer Network and Classification in the New Project Dialog) to classify two shapes. Edit the BuildTrainSet.lab to build an appropriate training set for the classification of two shapes: the Cardioid and the Rose. The input training set must include the shapes in different phases uniformly distributed : 1000 Cardioid shapes and 1000 Rose shapes. The classifier must be able to tolerate inputs contaminated with 10% of noise. The figure below shows the training process using perfect shapes and noisy shapes. Be sure to scale both shapes to have the same amplitude.

CardRose

Solution 1
After editing the file, RunRunclick the button to execute the code. If you do not have any errors, the training set will be generated and displayed on the variable list and the file list. Click on the file to see its contents. The first 1000 rows of the training set are the Cardioid shape; the second 1000 rows of the training set are the Rose shape. The training set target has two columns, the first column is for class 1 (Cardioid) and the second column is for class 2 (Rose).

CardiRose\BuildTrainSet.lab
int numCases = 1000;
int numInputs = 64;
double deltaInput = 6.28/numInputs;
double delta = 6.28/(numCases-1);
int i;
int j;
double phase = 0.0;
Matrix noise;
//________________________________ Class 1: cardioid
Matrix inputCardioid;
inputCardioid.Create(numCases, numInputs);
Matrix targetCardioid;
targetCardioid.Create(numCases, 2);
double theta = 0.0;

for(i=0; i<numCases; i++)
{
     phase = i*delta;
     for (j=0; j<numInputs; j++)
     {
          theta = j*deltaInput;
          inputCardioid[i][j] = (1.0+cos(theta + phase));
     }
     targetCardioid[i][0] = 1;
}
inputCardioid.Save();
//____ contaminate the cardioid
noise.CreateRandom(numCases, numInputs, 0.0, 2.0);
inputCardioid = 0.9*inputCardioid+ 0.1*noise;

//________________________________ Class 2: rose
Matrix inputRose;
inputRose.Create(numCases, numInputs);
Matrix targetRose;
targetRose.Create(numCases, 2);

for(i=0; i<numCases; i++)
{
     phase = i*delta;
     for (j=0; j<numInputs; j++)
     {
          theta = j*deltaInput;
          inputRose[i][j] = sin(3.0*theta + phase) + 1.0;
     }
     targetRose[i][1] = 1;
}
inputRose.Save();
//____ contaminate the rose
noise.CreateRandom(numCases, numInputs, 0.0, 2.0);
inputRose= 0.9*inputRose+ 0.1*noise;

//_______________________________ Create the training set input
Matrix trainSetInput;
trainSetInput.AppendDown(inputCardioid);
trainSetInput.AppendDown(inputRose);
trainSetInput.Save();

//_______________________________ Create the training set target
Matrix trainSetTarget;
trainSetTarget.AppendDown(targetCardioid);
trainSetTarget.AppendDown(targetRose);
trainSetTarget.Save();





input1

input2

trainSetInput1

trainSetInput2

trainSetTarget

Problem 2
Edit the BuilValidSet.lab file to build an appropriate validation set for problem 1 using random phase for the shapes. Include 175 Cardioids and 150 Roses.

Solution 2
After editing the file, RunRun click the button to execute the code. If you do not have any errors, the validation set will be generated and displayed on the variable list and the file list. Use the scroll bar at the top to visualize each validation case. Review the output of the network to verify that the classification is performed correctly.

CardiRose\BuildValidSet.lab
int numCasesCardioid = 150;
int numCasesRose = 175;
int numInputs = 64;
double deltaInput = 6.28/numInputs;

int i;
int j;
double phase = 0.0;
Matrix noise;
//________________________________ Class 1: cardioid
Matrix inputCardioid;
inputCardioid.Create(numCasesCardioid , numInputs);
Matrix targetCardioid;
targetCardioid.Create(numCasesCardioid , 2);
double theta = 0.0;

for(i=0; i<numCasesCardioid ; i++)
{
     phase = rand(6.28);
     for (j=0; j<numInputs; j++)
     {
          theta = j*deltaInput;
          inputCardioid[i][j] = (1.0+cos(theta + phase));
     }
     targetCardioid[i][0] = 1;
}
//____ contaminate the cardioid
noise.CreateRandom(numCasesCardioid, numInputs, 0.0, 2.0);
inputCardioid = 0.9*inputCardioid+ 0.1*noise;

//________________________________ Class 2: rose
Matrix inputRose;
inputRose.Create(numCasesRose , numInputs);
Matrix targetRose;
targetRose.Create(numCasesRose , 2);

for(i=0; i<numCasesRose ; i++)
{
     phase = rand(6.28);
     for (j=0; j<numInputs; j++)
     {
          theta = j*deltaInput;
          inputRose[i][j] = sin(3.0*theta + phase) + 1.0;
     }
     targetRose[i][1] = 1;
}
//____ contaminate the rose
noise.CreateRandom(numCasesRose , numInputs, 0.0, 2.0);
inputRose= 0.9*inputRose+ 0.1*noise;

//_______________________________ Create the validation set input
Matrix validSetInput;
validSetInput.AppendDown(inputCardioid);
validSetInput.AppendDown(inputRose);
validSetInput.Save();

//_______________________________ Create the validation set target
Matrix validSetTarget;
validSetTarget.AppendDown(targetCardioid);
validSetTarget.AppendDown(targetRose);
validSetTarget.Save();





Problem 3
Edit the Train.lab file to design and train an ANN for the classification of the shapes.

Solution 3
After editing the file, RunRun click the button to execute the code. If you do not have any errors, the ANN will be trained. Wait until the code execution stops. Double click the network in the variable list to see the network. Then, select the trainSetInput.csv file and, the trainSetTarget.csv file. You will be able to see the network behavior in real time by moving the scrollbar at the right.

CardiRose\Train.lab
//_________________________ Network Setup
LayerNet net;
net.Create(64, 12, 0, 2);

int i = 0;
//____________________________ Input Scaling
for(i = 0; i<64; i++)
{
     net.SetInScaler(i, 0, 2); // Input values are from 0 to 2
}
//____________________________ Output Scaling
for(i = 0; i<2; i++)
{
     net.SetOutScaler(i, 0.0, 1.0); // Output values are from 0 to 1
}

//________________________ Load and set the training set
Matrix trainSetInput;
trainSetInput.Load();
Matrix trainSetTarget;
trainSetTarget.Load();
net.SetTrainSet(trainSetInput, trainSetTarget, false);

//________________________ Train
net.TrainSimAnneal(100, 100, 15, 0.01, false, 4, 1.0e-12);
net.TrainConjGrad(2500,1.0e-12);

//_____________________________ Save the trained network
net.Save();

Tip
In the figure below the input of the network is a Cardioid, observe that the value in output 1 is one while the value in output 2 in zero as expected.

NetSimulation

Problem 4
Edit the CheckTraining.lab file to check the training: (a) Compute the confusion matrix using the training set. (b) Plot the error for each network output. (c) Save the confusion matrix as a vector image (trainConf.emf).

Solution 4 (a)
After editing the file, RunRun click the button to execute the code. If you do not have any errors, the number of errors will be displayed in the variable list. Click the variable called trainConf to view the confusion matrix for the training set, it should have zero errors as shown below.

CardiRose\CheckTraining.lab
//_________________________________________ Load the Training Set
Matrix trainSetInput;
trainSetInput.Load();
Matrix trainSetTarget;
trainSetTarget.Load();
//_________________________________________ Load the ANN
LayerNet net;
net.Load();
//_________________________________________ Run
Matrix output = net.Run(trainSetInput);
//_________________________________________ Compute the Confusion Matrix
Matrix trainConf = ConfusionMatrix(output, trainSetTarget, 0.5);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConf

Problem 5
Edit the Validation.lab file to perform the validation of the ANN. (a) Compute the confusion matrix using the validation set. (b) Plot the error for each network output. (c) Save the confusion matrix as a vector image (validConf.emf).

Solution 5 (a)
After editing the file, RunRun click the button to execute the code. You will be asked to save the file, set the file name to Validation.lab. If you do not have any errors, the number of errors will be displayed in the variable list. Click the variable called validConf to view the confusion matrix for the validation set.

CardiRose\Validation.lab
//_________________________________________ Load the validation set
Matrix validSetInput;
validSetInput.Load();
Matrix validSetTarget;
validSetTarget.Load();
//_________________________________________ Load the ANN
LayerNet net;
net.Load();
//_________________________________________ Run
Matrix output = net.Run(validSetInput);
//_________________________________________ Compute the confusion matrix
Matrix validConf = ConfusionMatrix(output, validSetTarget, 0.5);
validConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(validConf.GetSum()) - toint(validConf.GetDiagonalSum());

validConf

Problem 6
Generate a report in Microsoft Word. Write some conclusions in the report focusing on the problems that were faced during the simulation and how these problems were or could be solved.

© Copyright 2000-2019 Wintempla selo. All Rights Reserved. Sep 05 2019. Home