OpenCV 3.4 Assertion failed (checkDetectorSize()) in setSVMDetector

asked 2018-04-01 11:43:55 -0500

kurenchuksergey gravatar image

updated 2018-04-01 12:50:59 -0500

I'm trying to train SVM with my own images and feed that to the HOGDescriptor in OpenCV. I find a similar question (question). I add -rho how it do author in that question. But setSVMDetector throw Exception train image - size 28*28

code train svm

 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
  Mat trainingLabels = new Mat();
  MatOfFloat temp = new MatOfFloat();
  File negativeDir = new File("/home/kurenchuksergey/HogSignTrainingData/negative");
  File positiveDir = new File("/home/kurenchuksergey/HogSignTrainingData/positive");
  int i = -1;
  float[][] hogdescriptor = new float[13992][]; //13992 - image count
  for(File file:negativeDir.listFiles()){
              Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
              hog.compute(con, temp);
              trainingLabels.push_back(Mat.zeros(new Size(1,1),CvType.CV_32S));
  for(File file:negativeDir.listFiles()){
        Mat con = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        hog.compute(con, temp);
        hogdescriptor[++i] = temp.toArray();
        trainingLabels.push_back(Mat.ones(new Size(1,1),CvType.CV_32S));

    Mat Labels = new Mat();

    Mat mat  = new Mat(hogdescriptor.length,hogdescriptor[0].length,CvType.CV_32FC1);
    for(int j = 0;j<hogdescriptor.length;j++)

    TrainData trainDataHog = TrainData.create(mat,Ml.ROW_SAMPLE,Labels);

    SVM svm = SVM.create();
    // 1.4 bug fix: old 1.4 ver gamma is 1
    svm.setTermCriteria(new TermCriteria(1, 20000, 0.0001));

And no exception, but when i try set svm to hog, i get Exception code where i'm use setSVMDetector:

 SVM svm1 = SVM.load("SVMHog");
 HOGDescriptor hog = new HOGDescriptor(new Size(28,28),new Size(8,8),new Size(4,4),new Size(4,4),9);
 Double rho = svm1.getDecisionFunction(0,new Mat(),new Mat());
 int size = (int)(svm1.getSupportVectors().total() + 1) * svm1.getSupportVectors().channels();
 float[] temp = new float[size];
 svm1.getSupportVectors().get(0, 0, temp);
 temp[temp.length - 1] = (float)-rho;
 MatOfFloat vector = new MatOfFloat(temp);

And exception:

OpenJDK 64-Bit Server VM warning: You have loaded library /home/kurenchuksergey/opencv/build/lib/ which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
OpenCV Error: Assertion failed (checkDetectorSize()) in setSVMDetector, file /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp, line 117
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/kurenchuksergey/opencv/modules/objdetect/src/hog.cpp:117: error: (-215) checkDetectorSize() in function 
at org.opencv.objdetect.HOGDescriptor.setSVMDetector_0(Native Method)
at org.opencv.objdetect.HOGDescriptor.setSVMDetector(
edit retag flag offensive close merge delete


please, the exact error msg.

berak gravatar imageberak ( 2018-04-01 11:57:31 -0500 )edit

47 line is hog.setSVMDetector(vector);

kurenchuksergey gravatar imagekurenchuksergey ( 2018-04-01 12:52:21 -0500 )edit

it probably won't solve it, but:

you have to use a LINEAR kernel for this, not RBF, also SVM.C_SVR_EPS, not SVC

berak gravatar imageberak ( 2018-04-03 02:17:55 -0500 )edit

Yes, you are right. problem is solved

kurenchuksergey gravatar imagekurenchuksergey ( 2018-04-03 04:54:15 -0500 )edit

@berak What to do then if autotrain suggest the best results on RBF?

AHF gravatar imageAHF ( 2019-08-15 06:11:17 -0500 )edit

you're training the wrong thing then

berak gravatar imageberak ( 2019-08-15 06:39:50 -0500 )edit

@berak I am training for detection of the eyebrow, and in negative images, I am adding lips cheeks eyes forehead, etc as -1 and +1 for the eyebrow area. Is it the right approach?

AHF gravatar imageAHF ( 2019-08-15 13:00:09 -0500 )edit

@AHF, yes, but it is all irrelevant. you still NEED a single, LINEAR SVM support vector to use with the hog detection, RBF is useless there.

berak gravatar imageberak ( 2019-08-15 13:21:50 -0500 )edit