1
votes

I am new to opencv. I am trying to extract the features of images using HOGDescriptor in opencv. I am trying to train an SVM using Opencv2.2 which would be able to detect humans in images. I am using INRIA training samples containing 614 positives & 1218 negatives.

Problem: I am not getting good results. The accuracy is 70% when i am testing the SVM with the training samples. Can anyone help me how to adjust the parameters of SVM for unequal negatives & positives. The parameters for my SVM are:

CvMat *m=cvCreateMat(2,1,CV_32FC1);
cvmSet(m,0,0,1);
cvmSet(m,1,0,1);

CvSVMParams params;

params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
params.class_weights=m;
params.C=1000;

The entire code for the SVM training is:

void svm_train(char *list) {
int num_files=1832;

 int features=1620;

  float des;

int val=0;
int file_num=0;
int total=num_files*features;
int count=0;
Mat training_mat(num_files,features,CV_32FC1);
float label[1832];
for(int i=0;i<614;i++)
    label[i]= 1.0;
for(int j=614;j<1832;j++)
    label[j]= -1.0;
Mat labels(num_files,1,CV_32FC1,label);
    char *s;
fstream inputfile(list,ios::in);


while(count<=total)
{  

                if(val<=(features-1))
         {inputfile>>des;

    training_mat.at<float>(file_num,val)= des;
           val++;

        }
        else
        {
            val=0;
            file_num++;

        }
        count++;
}
count--;
cout<<count;




CvMat *m=cvCreateMat(2,1,CV_32FC1);
cvmSet(m,0,0,1);
cvmSet(m,1,0,1);
CvSVMParams params;
params.svm_type=CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
 params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
  params.class_weights=m;

 inputfile.close();
CvSVM svm;
pg=svm.get_default_grid(CvSVM::C);

         params.C=1000;
     fstream filelist("result1.txt",ios::app);
     filelist<<params.C;
     filelist<<"\t1218";
     filelist<<"\t\t614";

     svm.train(training_mat,labels,Mat(),Mat(),params);
     svm.save("svm_train.xml");
    filelist.close();

}

Here list is intialised the filename which stores the features extacted from the training samples i.e. negative & positive. The total no. of features for each image= 1620.

1
Try a different kernel type? - Ashalynd
I have just 2 classes i.e. humans or not humans so i think i have to use linear kernel only.... Since i dont have equal positive & negative samples that is why i thought i have to set the parameters.... Please help – - user3342666
Kernel type doesn't have much to do with the number of possible classes, it has more to do with how the classified examples are distributed. May be linear kernel simply can't divide them well enough. But I agree with the answer, it's more or less try-until-succeed. Playing with the type of kernel is one valid option. - Ashalynd
I actually dont have much idea about the kernel type... If you can explain me what type of kernel can be used,I'll greatly appreciate it. In the opencv documentation also the idea is very vague. - user3342666
Just read some docs about support vector machines, for example cs.columbia.edu/~kathy/cs4701/documents/jason_svm_tutorial.pdf, wiki en.wikipedia.org/wiki/Support_vector_machine or something from that page: kernel-machines.org/tutorials - Ashalynd

1 Answers

1
votes

I don't think that there is hard and fast rule to set the parameters. It is more & less based upon hit and trial under certain limit.

But you should read the following link which is about setting the parameters written by the developers of SVM. It is for not that mathematical...have a look

http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf