Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problems with training a two output layer ANN_MLP from a CSV

Hi, I'm trying to train a ANN_MLP with two layer output, I generated a CVS file and I read it with the function TrainData::loadFromCSV() , I have the following CSV file for recognize number 1 and 0:

0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1
1,1,1,1,1,0,0,1,1,0,0,1,,1,1,1,1,0

In Opencv2 I noticed that at the final of the line de CSV files have a ;, but in the function loadFromCSV() it seams that the output argument is a one-dimmension array

That's my sample code

#include <iostream>
#include <opencv2/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char *argv[])
{
    Ptr<ANN_MLP> nnetwork = ANN_MLP::create();
    cout << "Leyendo Datos" << endl;
    Ptr<TrainData> datos = TrainData::loadFromCSV("/home/josejacomeb/QT/MLP_DosCapas/mlp_2capas.csv",ROW_SAMPLE);
    vector<int> layerSizes = { 16, //Numero de Entradas
                               32,   //Capa Oculta
                               2  //Capa de Salida, para 2 Numeros
                             };
    cout << datos->getSamples()<< endl;
    nnetwork->setLayerSizes(layerSizes);
    nnetwork->setActivationFunction(ANN_MLP::SIGMOID_SYM,0.6,1);
    //Entrenamiento
    nnetwork->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,100000,0.0000001));
    nnetwork->setTrainMethod(ANN_MLP::BACKPROP);
    cout << "Creando la RNA" << endl;
    nnetwork->train(datos);
    printf( "Entrenado \n");
    FileStorage fs("/home/josejacomeb/QT/MLP_DosCapas/parametros.xml",FileStorage::WRITE);
    nnetwork->write(fs);
    fs.release();
    cout << "Escribiendo XML" << endl;
}

So I've got the following error:

Opencv Error: Bad argument (output training data should be a floating-point matrix with  the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /build/opencv/src/opencv-3.2.0/modules/ml/src/ann_mlp.cpp

I read in this forum that:

the train responses for an ann differ a bit from the usual opencv ml approach. if have 2 output neurons in your ann, need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

How can I create a 2 dimmension array from TrainData::loadFromCSV() for train the ANN_MLP?

I use Opencv 3.2 and GCC6

Problems with training a two output layer ANN_MLP from a CSVCSV File

Hi, I'm trying to train a ANN_MLP with two layer output, I generated a CVS file and I read it with the function TrainData::loadFromCSV() , I have the following CSV file for recognize number 1 and 0:

0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1
1,1,1,1,1,0,0,1,1,0,0,1,,1,1,1,1,0

In Opencv2 I noticed that at the final of the line de CSV files have a ;, but in the function loadFromCSV() it seams that the output argument is a one-dimmension array

That's my sample code

#include <iostream>
#include <opencv2/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char *argv[])
{
    Ptr<ANN_MLP> nnetwork = ANN_MLP::create();
    cout << "Leyendo Datos" << endl;
    Ptr<TrainData> datos = TrainData::loadFromCSV("/home/josejacomeb/QT/MLP_DosCapas/mlp_2capas.csv",ROW_SAMPLE);
    vector<int> layerSizes = { 16, //Numero de Entradas
                               32,   //Capa Oculta
                               2  //Capa de Salida, para 2 Numeros
                             };
    cout << datos->getSamples()<< endl;
    nnetwork->setLayerSizes(layerSizes);
    nnetwork->setActivationFunction(ANN_MLP::SIGMOID_SYM,0.6,1);
    //Entrenamiento
    nnetwork->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,100000,0.0000001));
    nnetwork->setTrainMethod(ANN_MLP::BACKPROP);
    cout << "Creando la RNA" << endl;
    nnetwork->train(datos);
    printf( "Entrenado \n");
    FileStorage fs("/home/josejacomeb/QT/MLP_DosCapas/parametros.xml",FileStorage::WRITE);
    nnetwork->write(fs);
    fs.release();
    cout << "Escribiendo XML" << endl;
}

So I've got the following error:

Opencv Error: Bad argument (output training data should be a floating-point matrix with  the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /build/opencv/src/opencv-3.2.0/modules/ml/src/ann_mlp.cpp

I read in this forum that:

the train responses for an ann differ a bit from the usual opencv ml approach. if have 2 output neurons in your ann, need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

How can I create a 2 dimmension array from TrainData::loadFromCSV() for train the ANN_MLP?

I use Opencv 3.2 and GCC6

click to hide/show revision 3
No.3 Revision

Problems with training a two output layer ANN_MLP from a CSV File

Hi, I'm trying to train a ANN_MLP with two layer output, I generated a CVS file and I read it with the function TrainData::loadFromCSV() , I have the following CSV file for recognize number 1 and 0:

0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1
1,1,1,1,1,0,0,1,1,0,0,1,,1,1,1,1,0

In Opencv2 I noticed that at the final of the line de CSV files have a ;, but in the function loadFromCSV() it seams that the output argument is a one-dimmension array

That's my sample code

#include <iostream>
#include <opencv2/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main(int argc, char *argv[])
{
    Ptr<ANN_MLP> nnetwork = ANN_MLP::create();
    cout << "Leyendo Datos" << endl;
    Ptr<TrainData> datos = TrainData::loadFromCSV("/home/josejacomeb/QT/MLP_DosCapas/mlp_2capas.csv",ROW_SAMPLE);
    vector<int> layerSizes = { 16, //Numero de Entradas
                               32,   //Capa Oculta
                               2  //Capa de Salida, para 2 Numeros
                             };
    cout << datos->getSamples()<< endl;
    nnetwork->setLayerSizes(layerSizes);
    nnetwork->setActivationFunction(ANN_MLP::SIGMOID_SYM,0.6,1);
    //Entrenamiento
    nnetwork->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS,100000,0.0000001));
    nnetwork->setTrainMethod(ANN_MLP::BACKPROP);
    cout << "Creando la RNA" << endl;
    nnetwork->train(datos);
    printf( "Entrenado \n");
    FileStorage fs("/home/josejacomeb/QT/MLP_DosCapas/parametros.xml",FileStorage::WRITE);
    nnetwork->write(fs);
    fs.release();
    cout << "Escribiendo XML" << endl;
}

So I've got the following error:

Opencv Error: Bad argument (output training data should be a floating-point matrix with  the number of rows equal to the number of training samples and the number of columns equal to the size of last (output) layer) in prepare_to_train, file /build/opencv/src/opencv-3.2.0/modules/ml/src/ann_mlp.cpp

I read in this forum that:

the train responses for an ann differ a bit from the usual opencv ml approach. if have 2 output neurons in your ann, need 2 output neurons for each training feature too, not a single "class label" (like with e.g. an SVM).

How can I create a 2 dimmension array from TrainData::loadFromCSV() for train the ANN_MLP?

I use Opencv 3.2 and GCC6