user3342666
user3342666

Reputation: 93

Training CvSVM to detect humans in images using HOGDescriptor

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.

Upvotes: 1

Views: 955

Answers (1)

skm
skm

Reputation: 5659

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

Upvotes: 1

Related Questions