I'm trying to train my own detector for use with OpenCV::HOGDescriptor but I'm having trouble making the existing HOGDescriptor work with my newly trained SVM.
I have calculated HOG features for positive and negative training images, labeled them and trained the SVM using CvSVM. The parameters I have used are:
CvSVMParams params;
params.svm_type =CvSVM::EPS_SVR;
params.kernel_type = CvSVM::LINEAR;
params.C = 0.01;
params.p = 0.5;
Then I calculate Primal Form of the support vectors so that I only get one vector instead of many and set the calculated support vector using HOGDescriptor.setSVMDetector(vector);
When I use CvSVM.predict() I am able to correctly classify objects with the SVM, but HOGDescriptor.detect() or detectMultiScale() always returns a lot of positive matches and does not give accurate predictions.
CvSVM.predict() uses the original support vectors for classification so there might be something wrong with the way I'm calculating primal form.
Is there anyone who has trained their own detector who can point me in the right direction?
From what I read in Dalal's paper about HOG detector, he suggest that to remove false positives, we need to retrain our model. Retraining is done by applying preliminary model (your model which gives lot of false positives), then detect objects in all negative sample images. All of returned rectangles would definitely false positives.
Then, add all of these false positives to your negative sample images (negative dataset), do training once again. The resulting model, as suggested in the paper, will return much less false positives.
Unfortunately though, I tried that (re-training), but the resulting model just does not recognize anything, even on positive image samples. But I think it is worth a try because that was what suggested in the inventor's paper about HOG detector
I wrote a child class of CvSVM to extract primal form after a linear svm is trained. Positive samples are labeled 1 and negative samples are labeled -1. It is strange that I have to put negative sign in front of alphas and leaving the sign of rho unchanged in order to get correct results from HogDescriptor.
LinearSVM.h
LinearSVM.cc
I was struggling with the same problem. Searching forums I have found, that the detector cannot be trained using CvSVM (I don't know the reason). I used LIBSVM for training the the detector. Here is the code to extract the detector for HOGDescriptor.setSVMDetector( w): For data details see LIBSVM documentation/header. I did all the training in C++, filling the LIBSVM training data from CV to LIBSVM; the code below extracts the detector vector needed for cv::HOGDescriptor. The w parameter is
std::vector<float> w
Hope this helps...