HOG setSVMDetector slow [closed]

asked 2016-07-18 07:35:47 -0600

isaacperez gravatar image

updated 2016-07-19 01:46:40 -0600

I'm using HOG with custom svm. I've used https://github.com/opencv/opencv/blob... but my feature vector has 2268 features and get_svm_detector() give me 2269 support vectors, this don't give me error but hog.detectMultiScale is very slow ( ~ 6 s), why?

With svmLight I get 2268 support vectors and hog.detectMultiScale takes about ~64 ms.

I Add the code:

Ptr<SVM> svm;
Ptr<TrainData> trainData = TrainData::loadFromCSV("./trainData.csv");    

svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, (int)1000, 1e-3));

svm->trainAuto(trainData,10,SVM::getDefaultGrid(SVM::C),SVM::getDefaultGrid(SVM::GAMMA),SVM::getDefaultGrid(SVM::P),SVM::getDefaultGrid(SVM::NU),SVM::getDefaultGrid(SVM::COEF),SVM::getDefaultGrid(SVM::DEGREE),true );

svm->save("model.yml");

In other class:

HOGDescriptor hog;

Ptr<SVM> svm = StatModel::load<SVM>( "model.yml" );
Ptr<SVM> svm = StatModel::load<SVM>( "clasificador.yml" );

vector< float > hog_detector;

// get the support vectors
Mat sv = svm->getSupportVectors();
const int sv_total = sv.rows;

// get the decision function
Mat alpha, svidx;
double rho = svm->getDecisionFunction(0, alpha, svidx);

CV_Assert( alpha.total() == 1 && svidx.total() == 1 && sv_total == 1 );
CV_Assert( (alpha.type() == CV_64F && alpha.at<double>(0) == 1.) ||
           (alpha.type() == CV_32F && alpha.at<float>(0) == 1.f) );
CV_Assert( sv.type() == CV_32F );

hog_detector.clear();
hog_detector.resize(sv.cols + 1);
memcpy(&hog_detector[0], sv.ptr(), sv.cols*sizeof(hog_detector[0]));
hog_detector[sv.cols] = (float)-rho;

hog.setSVMDetector( hog_detector );

// Then, it take me about 6 s!!
hog.detectMultiScale(img, detections, thresholdDetection, winStride, padding);
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-27 11:30:10.850338

Comments

  • The SVM vector includes the rho value, so yes, it will have one more element that the feature vector
  • What are you exactly doing? 6 seconds is way too much based on my experience. Are you re-training each time? Are you in release mode? What are your multiscale params? And you input image size? Please, post code and examples
LorenaGdL gravatar imageLorenaGdL ( 2016-07-18 12:09:40 -0600 )edit

Ok. Done !

isaacperez gravatar imageisaacperez ( 2016-07-19 01:46:13 -0600 )edit

It seems ok. Once again, please post the multiscale params and input image size. That is likely the problem (if you're sure to be in release mode)

LorenaGdL gravatar imageLorenaGdL ( 2016-07-19 01:53:09 -0600 )edit

Hi LorenaGdL I have seen http://stackoverflow.com/questions/21... If I put rho as an argument in HOGDescriptor::detectMultiScale call it take me about 64 ms so it's ok but I don't understand why

isaacperez gravatar imageisaacperez ( 2016-07-19 02:06:54 -0600 )edit

I see that if I don't put rho in setSVMDetector and I put in detectMultiScale in hitThreshold with -3.8 it take me ~6 s and for example 4 take me ~64 ms... WTF?

isaacperez gravatar imageisaacperez ( 2016-07-19 03:17:40 -0600 )edit

You're shifting the hyperplane, so your number of detections will differ. I guess the differences in time are related to grouping, but still it way too much difference... If you provide your trained SVM model and test image, I may take a look if I have time

LorenaGdL gravatar imageLorenaGdL ( 2016-07-19 05:50:27 -0600 )edit

Thanks for your help! I found that it give me 39802 detections without activate mean-shift!

isaacperez gravatar imageisaacperez ( 2016-07-20 03:31:09 -0600 )edit