2013-10-04 65 views
1

我創建了一個CvANN_MLP類的神經網絡,與2.4.6版本的Opencv庫一起工作。我Cv_ANN MLP網絡:神經網絡爲測試樣本提供了相同的結果

Mat trainingData(NUMERO_ESEMPI_TOTALE, 59, CV_32FC1); 
Mat trainingClasses(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 

for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){ 
    for(int j=0;j<59;j++){ 
     trainingData.at<float>(i,j) = featureVect[i][j]; 
    } 
} 

for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){ 
    trainingClasses.at<float>(i,0) = featureVect[i][59]; 
} 

Mat testData (NUMERO_ESEMPI_TEST , 59, CV_32FC1); 
Mat testClasses (NUMERO_ESEMPI_TEST , 1, CV_32FC1); 
for(int i=0;i<NUMERO_ESEMPI_TEST;i++){ 
    for(int j=0;j<59;j++){ 
     testData.at<float>(i,j) = featureVectTest[i][j]; 
    } 
} 

//0 bocca, 1 non bocca. 
testClasses.at<float>(0,0) = 1; 
testClasses.at<float>(1,0) = 0; 
testClasses.at<float>(2,0) = 1; 
testClasses.at<float>(3,0) = 1; 
testClasses.at<float>(4,0) = 0; 
testClasses.at<float>(5,0) = 1; 
testClasses.at<float>(6,0) = 0; 
testClasses.at<float>(7,0) = 1; 
testClasses.at<float>(8,0) = 1; 
testClasses.at<float>(9,0) = 0; 
testClasses.at<float>(10,0) = 0; 
testClasses.at<float>(11,0) = 1; 
testClasses.at<float>(12,0) = 0; 
testClasses.at<float>(13,0) = 0; 
testClasses.at<float>(14,0) = 0; 
testClasses.at<float>(15,0) = 0; 
testClasses.at<float>(16,0) = 0; 
testClasses.at<float>(17,0) = 0; 
testClasses.at<float>(18,0) = 0; 
testClasses.at<float>(19,0) = 1; 
testClasses.at<float>(20,0) = 1; 
testClasses.at<float>(21,0) = 0; 
testClasses.at<float>(22,0) = 1; 
testClasses.at<float>(23,0) = 0; 
testClasses.at<float>(24,0) = 1; 
testClasses.at<float>(25,0) = 0; 
testClasses.at<float>(26,0) = 0; 
testClasses.at<float>(27,0) = 1; 
testClasses.at<float>(28,0) = 1; 
testClasses.at<float>(29,0) = 1;  

Mat layers = Mat(3, 1, CV_32SC1); 
layers.row(0) = Scalar(59); 
layers.row(1) = Scalar(3); 
layers.row(2) = Scalar(1); 

CvANN_MLP mlp; 
CvANN_MLP_TrainParams params; 
CvTermCriteria criteria; 
criteria.max_iter = 100; 
criteria.epsilon = 0.0000001; 
criteria.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS; 
params.train_method = CvANN_MLP_TrainParams :: BACKPROP; 
params.bp_dw_scale = 0.05 ; 
params.bp_moment_scale = 0.05 ; 
params.term_crit = criteria ; 
mlp.create(layers); 
// train 
mlp.train(trainingData,trainingClasses,Mat(),Mat(),params); 

Mat response(1, 1, CV_32FC1); 
Mat predicted(testClasses.rows, 1, CV_32F); 
Mat pred(NUMERO_ESEMPI_TEST, 1, CV_32FC1); 
Mat pred1(NUMERO_ESEMPI_TEST, 1, CV_32FC1); 
for(int i = 0; i < testData . rows ; i++){ 
    Mat response(1, 1, CV_32FC1); 
    Mat sample = testData.row(i); 
    mlp.predict(sample,response); 
    predicted.at<float>(i ,0) = response.at <float>(0,0); 
    pred.at<float>(i,0)=predicted.at<float>(i ,0); 
    pred1.at<float>(i,0)=predicted.at<float>(i ,0); 
    file<<"Value Image "<<i<<": "<<predicted.at<float>(i ,0)<<"\n"; 
    //cout<<"Value Image "<<i<<": "<<predicted.at<float>(i ,0)<<endl; 
} 

的問題是,這個網絡還給我對每個測試樣本相同的結果。我不知道爲什麼。我的網絡將一組具有59個輸入值和1個輸出值的特徵向量作爲輸入。 你能幫我嗎?

回答

1

我有類似的問題。由於mlp的創建函數的默認參數發生問題。它沒有成功創建一個S形函數,並且它不能在這種條件下訓練,你會得到相同的結果。因此,解決辦法是使用這樣科瑞功能:

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

我的問題是在這裏:OpenCV Neural Network Sigmoid Output

+0

如果這個問題被關閉的重複? – Potatoswatter

+0

問題不完全相同,但問題來源和解決方案相同。 – yutasrobot

+0

這足以在本網站上標記爲重複。 – Potatoswatter