Training models with artificial neural networks

ANN can be used to train a model using a set of sample input and output vectors. ANN is a highly popular machine learning algorithm and the basis of many modern artificial intelligence algorithms that are used to train models for classification and correlation. Especially in computer vision, the ANN algorithm can be used along with a wide range of feature-description algorithms to learn about images of objects, or even faces of different people, and then used to detect them in images.

You can use the ANN_MLP class (which stands for artificial neural networks—multi-layer perceptron) in OpenCV to implement ANN in your applications. The usage of this class is quite similar to that of the SVM class, so we're going to give a simple example to learn the differences and how it's used in practice, and we'll leave the rest for you to discover by yourself.

Creating the training samples dataset is exactly the same for all machine learning algorithms in OpenCV, or, to be precise, for all subclasses of the StatsModel class. The ANN_MLP class is no exception to this, so, just like with the SVM class, first we need to create a TrainData object that contains all the sample and response data that we need to use when training our ANN model, as seen here:

SampleTypes layout = ROW_SAMPLE; 
data = TrainData::create(samples, 
                         layout, 
                         responses); 

samples and responses, in the preceding code, are both Mat objects that contain a number of rows that equals the number of all the training data we have in our dataset. As for the number of columns in them, let's recall that the ANN algorithm can be used to learn the relationship between vectors of input and output data. This means that the number of columns in the training input data, or samples, can be different from the number of columns in the training output data, or responses. We'll refer to the number of columns in samples as the number of features, and to the number of columns in responses as the number of classes. Simply put, we're going to learn the relationship of features to classes using a training dataset.

After taking care of the training dataset, we need to create an ANN_MLP object using the following code:

Ptr<ANN_MLP> ann = ANN_MLP::create(); 

We have skipped all the customizations and used the default set of parameters. In the case that you need to use a fully customized ANN_MLP object, you need to set the activation function, termination criteria, and various other parameters in the ANN_MLP class. To learn more about this, make sure to refer to the OpenCV documentation and online resources about artificial neural networks.

Setting the correct layer sizes in the ANN algorithm requires experience and depends on the use case, but it can also be set using a few trial-and-error sessions. Here's how you can set the number and size of each layer in the ANN algorithm, and the ANN_MLP class to be specific:

Mat_<int> layers(4,1); 
layers(0) = featureCount;    // input layer 
layers(1) = classCount * 4;  // hidden layer 1 
layers(2) = classCount * 2;  // hidden layer 2 
layers(3) = classCount;      // output layer 
ann->setLayerSizes(layers); 

In the preceding code, the number of rows in the layers object refers to the number of layers we want to have in our ANN. The first element in the layers object should contain the number of features in our dataset, and the last element in the layers object should contain the number of classes. Recall that the number of features equals the column count of samples, and the number of classes equals the column count of responses. The rest of the elements in the layers object contain the sizes of hidden layers.

Training the ANN model is done by using the train method, as seen in the following example:

if(!ann->train(data)) 
{ 
    cout << "training failed" << endl; 
    return -1; 
} 

After the training is completed, we can use the save and load methods in exactly the same way as we saw before, to save the model for later use, or reload it from a saved file.

Using the model with the ANN_MLP class is also quite similar to the SVM class. Here's an example:

Mat_<float> input(1, featureCount); 
Mat_<float> output(1, classCount); 
// fill the input Mat 
ann->predict(input, output); 

Choosing the right machine learning algorithm for each problem requires experience and knowledge about where the project is going to be used. SVM is quite simple, and suitable when we need to work with the classification of data and in the segmentation of groups of similar data, whereas ANN can be easily used to approximate a function between sets of input and output vectors (regression). Make sure to try out different machine learning problems to better understand where and when to use a specific algorithm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.197