神经网络mlp麻烦

时间:2013-10-02 15:36:28

标签: c++ opencv neural-network

我使用Visual Studio 2012和Opencv 2.4.6在C ++中编程。 我有一组训练图像,我已经计算了特征向量。这些特征向量应该成为我的神经网络的输入,用类CvANN_MLP实现。 每个特征向量由60个属性组成,59个是神经网络的“输入”,最后一个是“输出”,可以只有1或0。 我已经意识到这个神经网络:

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";
}

返回的值属于以下类型:

 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

所以我没有0或1的结果。这是正确的吗?如果没有,我的代码中的错误是什么?

1 个答案:

答案 0 :(得分:0)

我对C接口知之甚少,但我在这里遇到了类似的c ++接口问题:

OpenCV Neural Network Sigmoid Output

Mlp的创建函数默认参数不起作用,它们应该是:

mlp.create(layers,CvANN_MLP :: SIGMOID_SYM,1,1)

但正如我所说,我不知道c界面。由于C没有默认参数,可能还有另一个函数来给出sigmoid的alpha和beta参数。

顺便说一句,OpenCV没有众所周知的sigmoid实现,范围为0和1.正如文档中所述,它介于-1和1之间。