Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Set number of output nodes of Artificial Neural Network to a value other than 1

I am working on a project where I need to classify images using Artificial Neural Network (ANN). I have successfully trained ANN with 1 output node as shown in code below. How can I set number of output nodes to another value other than 1 (e.g. 50). Kind regards

trainANN(Mat hists, vector<int> labels)
{
    int cols = hists.cols;//size of input layer must be equal to this number of cols
    int rows = hists.rows;
    Mat responses(labels);//create a matrix of responses from vector labels
    responses.convertTo(responses,  CV_32FC1);//convert responses (labels) from int to float
    Ptr<TrainData> trainData = TrainData::create(hists, ROW_SAMPLE, responses);//create training data
    Ptr<ANN_MLP> ann = ml::ANN_MLP::create();
    vector<int> layers = { cols,1000,500,1 };//layers
    ann->setLayerSizes(layers);//set the layer sizes(SIZES)
    ann->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
    cout << "Training ANN..." << endl;
    ann->train(trainData);
    bool trained = ann->isTrained();
    if (trained)
    {
        cout << "Artificial Neural Network Trained"<<endl;
        ann->save(".\\Trained Models\\ANN.xml");
    }
}