SVM train error [closed]

asked 2014-10-06 10:30:00 -0600

jamesnzt gravatar image

I tried to train SVM with some data. but an exception is thrown at svm.train line.

#include "opencv2/opencv.hpp"
#include<iostream>
#include<stdio.h>

using namespace cv;
using namespace std;

int main()
{
    //int image_regions=4;//number of samples;
    //Mat training_mat(image_regions,7,CV_32FC1);
    //Mat labels(image_regions,1,CV_32FC1);
    double hu[4][7];


    hu[0][0]=0.00120898;
    hu[0][1]=5.11818e-08;
    hu[0][2]=3.55067e-10;
    hu[0][3]=6.66231e-11;
    hu[0][4]=8.45525e-21;
    hu[0][5]=3.247e-15;
    hu[0][6]=-5.78858e-21;


    hu[1][0]=0.000857154;
    hu[1][1]=1.52204e-07;
    hu[1][2]=1.95253e-10;
    hu[1][3]=2.13237e-11;
    hu[1][4]=7.62358e-22;
    hu[1][5]=5.54739e-15;
    hu[1][6]=-1.14541e-21;



    hu[2][0]=0.000880923;
    hu[2][1]=3.07455e-07;
    hu[2][2]=1.11048e-11;
    hu[2][3]=5.6902e-14;
    hu[2][4]=-1.53374e-26;
    hu[2][5]=8.70844e-18;
    hu[2][6]=4.25523e-26;
    // negative data
    hu[3][0]=0.00180891;
    hu[3][1]=2.42715e-006;
    hu[3][2]=2.77416e-010;
    hu[3][3]=5.19146e-011;
    hu[3][4]=3.15921e-022;
    hu[3][5]=6.22216e-021;
    hu[3][6]=2.29176e-313;


    for(int i=0;i<4;i++)
        {
            for(int j=0;j<7;j++)
                cout<<hu[i,j]<<'\n';
            cout<<"--------------------------------------------------- "<<'\n';
    }

Mat training_mat(4,7,CV_32FC1);
Mat labels(4,7,CV_32FC1);



    for(int l=0;l<4;l++)
    {
        for(int k=0;k<7;k++)
            training_mat.at<float>(l,k) = hu[l][k];
    }


    for(int l=0;l<4;l++)
    {
        for(int k=0;k<7;k++)
            if(l==3)
                labels.at<float>(l,k)=-1.0;
            else 
                labels.at<float>(l,k)=1.0;
    }





CvSVM svm;
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type=CvSVM::LINEAR;
params.term_crit=cvTermCriteria(CV_TERMCRIT_ITER,1000,1e-6);
svm.train(training_mat,labels,Mat(),Mat(),params);

waitKey(0);

}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by jamesnzt
close date 2015-02-17 00:08:05.834150

Comments

still a hu[i,j] left in your code ;)

berak gravatar imageberak ( 2014-10-06 10:45:36 -0600 )edit

so, you got 4 hu-moments. (3 positive, one negative)

a label for each of them, - that would be a 4x1 Mat, not a 4x7 one.


float l[4] = {-1,-1,-1,1}; Mat labels(4,1,CV_32F, l); // done.

berak gravatar imageberak ( 2014-10-06 10:49:48 -0600 )edit

Thanks @berak I corrected and tried it works. The function svm.predict(Mat) always returns -1 even if i gives the same training data value. Is this problem is due to values ?

jamesnzt gravatar imagejamesnzt ( 2014-10-06 11:24:53 -0600 )edit

definitely try with more data(like 100 each)

then, - SVMParams, that's where the real work starts.

berak gravatar imageberak ( 2014-10-06 11:30:49 -0600 )edit

does svm.save(); svm.load(); save the trained svm as a separate file? if so is there any extension for that? does we train SVM in a separate program and load the file in our project program?

jamesnzt gravatar imagejamesnzt ( 2014-10-08 00:21:04 -0600 )edit

you can use any extension you like. (if it is xml, it it will be xml, in all other cases yaml. yml.gz works, too !)

train SVM in a separate program ? - you can do that, if you want.

berak gravatar imageberak ( 2014-10-08 00:28:47 -0600 )edit

it worked thanks @berak. i trained SVM in separate program and saved it. I load it in main program. But still now the result is not correct it is always returning -1. I am using linear kernal, will switching to other types gives better?

jamesnzt gravatar imagejamesnzt ( 2014-10-08 04:21:51 -0600 )edit

@berak is there any parameter in SVM to find how closely it classify/matches? if so how can i get that displayed in my program?

jamesnzt gravatar imagejamesnzt ( 2014-10-13 10:55:06 -0600 )edit
1

if i am using SVM for classification what are the parameters that i can use to find the efficiency of my code?

jamesnzt gravatar imagejamesnzt ( 2014-10-13 11:06:17 -0600 )edit
1

damn, lot of good questions, i wish i had a good answer.

for my own tries there, i resolved to have a properly labelled database, split into train & test sets, run tests with different parameters, see, which works best.

pretty similar to SVM::train_auto() , actually.

berak gravatar imageberak ( 2014-10-13 11:27:47 -0600 )edit