Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Neural Networks with CVANN_MLP

I am programming in C++ with Visual Studio 2012 and Opencv 2.4.6. I have a set of training images for which I have calculated the feature vectors. These feature vectors should become the input of my neural network, realized with the class CvANN_MLP. Every feature vector is composed of 60 attributes, 59 are the "inputs" of the neural network, and the last is the "output", that can be only 1 or 0. I have realized this neural network:

CvANN_MLP machineBrain;

double td[NUMERO_ESEMPI_TOTALE][60]; 

CvMat* trainData = cvCreateMat(NUMERO_ESEMPI_TOTALE, 59, CV_32FC1); 

CvMat* trainClasses = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 

CvMat* sampleWts = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 
//The matrix representation of our ANN. We'll have four layers.
CvMat* neuralLayers = cvCreateMat(4, 1, CV_32SC1);
CvMat trainData1, trainClasses1, neuralLayers1, sampleWts1;

cvGetRows(trainData, &trainData1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(sampleWts, &sampleWts1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(neuralLayers, &neuralLayers1, 0, 4);



cvSet1D(&neuralLayers1, 0, cvScalar(59));
cvSet1D(&neuralLayers1, 1, cvScalar(3));
cvSet1D(&neuralLayers1, 2, cvScalar(3));
cvSet1D(&neuralLayers1, 3, cvScalar(1));



for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){
    for(int j=0;j<59;j++){
        td[i][j] = featureVect[i][j];
    }
    if(i<45){
        td[i][59] = 0; //è una bocca!
    }else{
        td[i][59] = 1; //non è una bocca!
    }
}

//Mettiamo insieme i training data
for (int i=0; i<NUMERO_ESEMPI_TOTALE; i++){
    //I 59 input 
    for(int j=0;j<59;j++){
        cvSetReal2D(&trainData1, i, 0, td[i][j]);
    }
    //Output
    cvSet1D(&trainClasses1, i, cvScalar(td[i][59]));
    //I pesi (vengono tutti settati a 1)
    cvSet1D(&sampleWts1, i, cvScalar(1));
}


machineBrain.create(neuralLayers);
cout<<"Rete creata"<<endl;

//Train it with our data.
machineBrain.train(trainData,trainClasses,sampleWts,0,CvANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,100000,/*1.0*/0.01/*riprovare 0.01*/),CvANN_MLP_TrainParams::BACKPROP,0.001,0.05));
cout<<"Rete addestrata"<<endl;

 Mat pred(num_test_sample, 1, CV_32FC1);
Mat pred1(num_test_sample, 1, CV_32FC1);
for(int i=0;i<NUMERO_ESEMPI_TEST; i++){
    float _sample[59];
    CvMat sample = cvMat(1, 59, CV_32FC1, _sample);
    float _predout[1];
    CvMat predout = cvMat(1, 1, CV_32FC1, _predout);
    for(int j=0;j<59;j++){
        sample.data.fl[j] = featureVectTest[i][j];
    }
    machineBrain.predict(&sample, &predout);
    cout<<endl<<predout.data.fl[i]<<endl;//risultato predizione!
    pred.at<float>(i,0)=predout.data.fl[i];
    pred1.at<float>(i,0)=predout.data.fl[i];
    file<<"Value Image "<<i<<": "<<predout.data.fl[i]<<"\n";
}

The values that are returned are of this type:

 Value Image 0: 0.475639
 Value Image 1: 0
 Value Image 2: 4.2039e-044
 Value Image 3: 1.4013e-045
 Value Image 4: -7.88636e-016
 Value Image 5: 1.31722e-043
 Value Image 6: 4.2039e-044
 Value Image 7: 1.4013e-045
 Value Image 8: 0.0154511
 Value Image 9: 0.00100189
 Value Image 10: 0.00161414
 Value Image 11: 0.0449422
 Value Image 12: 7.5433
 Value Image 13: 65.8052
 Value Image 14: 24.301
 Value Image 15: 19.7311
 Value Image 16: 0.985553
 Value Image 17: 0.965309
 Value Image 18: 0.971295

So I haven't results of 0 or 1. Is it correct? If not, what is the mistake in my code?

click to hide/show revision 2
retagged

updated 2013-10-02 13:38:00 -0500

berak gravatar image

Neural Networks with CVANN_MLP

I am programming in C++ with Visual Studio 2012 and Opencv 2.4.6. I have a set of training images for which I have calculated the feature vectors. These feature vectors should become the input of my neural network, realized with the class CvANN_MLP. Every feature vector is composed of 60 attributes, 59 are the "inputs" of the neural network, and the last is the "output", that can be only 1 or 0. I have realized this neural network:

CvANN_MLP machineBrain;

double td[NUMERO_ESEMPI_TOTALE][60]; 

CvMat* trainData = cvCreateMat(NUMERO_ESEMPI_TOTALE, 59, CV_32FC1); 

CvMat* trainClasses = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 

CvMat* sampleWts = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 
//The matrix representation of our ANN. We'll have four layers.
CvMat* neuralLayers = cvCreateMat(4, 1, CV_32SC1);
CvMat trainData1, trainClasses1, neuralLayers1, sampleWts1;

cvGetRows(trainData, &trainData1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(sampleWts, &sampleWts1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(neuralLayers, &neuralLayers1, 0, 4);



cvSet1D(&neuralLayers1, 0, cvScalar(59));
cvSet1D(&neuralLayers1, 1, cvScalar(3));
cvSet1D(&neuralLayers1, 2, cvScalar(3));
cvSet1D(&neuralLayers1, 3, cvScalar(1));



for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){
    for(int j=0;j<59;j++){
        td[i][j] = featureVect[i][j];
    }
    if(i<45){
        td[i][59] = 0; //è una bocca!
    }else{
        td[i][59] = 1; //non è una bocca!
    }
}

//Mettiamo insieme i training data
for (int i=0; i<NUMERO_ESEMPI_TOTALE; i++){
    //I 59 input 
    for(int j=0;j<59;j++){
        cvSetReal2D(&trainData1, i, 0, td[i][j]);
    }
    //Output
    cvSet1D(&trainClasses1, i, cvScalar(td[i][59]));
    //I pesi (vengono tutti settati a 1)
    cvSet1D(&sampleWts1, i, cvScalar(1));
}


machineBrain.create(neuralLayers);
cout<<"Rete creata"<<endl;

//Train it with our data.
machineBrain.train(trainData,trainClasses,sampleWts,0,CvANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,100000,/*1.0*/0.01/*riprovare 0.01*/),CvANN_MLP_TrainParams::BACKPROP,0.001,0.05));
cout<<"Rete addestrata"<<endl;

 Mat pred(num_test_sample, 1, CV_32FC1);
Mat pred1(num_test_sample, 1, CV_32FC1);
for(int i=0;i<NUMERO_ESEMPI_TEST; i++){
    float _sample[59];
    CvMat sample = cvMat(1, 59, CV_32FC1, _sample);
    float _predout[1];
    CvMat predout = cvMat(1, 1, CV_32FC1, _predout);
    for(int j=0;j<59;j++){
        sample.data.fl[j] = featureVectTest[i][j];
    }
    machineBrain.predict(&sample, &predout);
    cout<<endl<<predout.data.fl[i]<<endl;//risultato predizione!
    pred.at<float>(i,0)=predout.data.fl[i];
    pred1.at<float>(i,0)=predout.data.fl[i];
    file<<"Value Image "<<i<<": "<<predout.data.fl[i]<<"\n";
}

The values that are returned are of this type:

 Value Image 0: 0.475639
 Value Image 1: 0
 Value Image 2: 4.2039e-044
 Value Image 3: 1.4013e-045
 Value Image 4: -7.88636e-016
 Value Image 5: 1.31722e-043
 Value Image 6: 4.2039e-044
 Value Image 7: 1.4013e-045
 Value Image 8: 0.0154511
 Value Image 9: 0.00100189
 Value Image 10: 0.00161414
 Value Image 11: 0.0449422
 Value Image 12: 7.5433
 Value Image 13: 65.8052
 Value Image 14: 24.301
 Value Image 15: 19.7311
 Value Image 16: 0.985553
 Value Image 17: 0.965309
 Value Image 18: 0.971295

So I haven't results of 0 or 1. Is it correct? If not, what is the mistake in my code?