Why does my custom HOG detector always draws a rectangle on the center of image even when I have obtained good accuracies on Test data?
I am trying to make a custom HOG detector. Before working on my dataset, I am trying to verify the results on INRIA dataset. I have extracted features from hog.compute function and then used those features to train an SVM classifier.
Using the svm.predict command, I tested the accuracies on test data provided by INRIA. I got 97.3% accuracy on pos test data, and 99.6% on neg test data (neg images were resized to 64x128. I tried with cropping to 64x128 as well and the accuracy was 95.4%)
I used the following code to find the support vectors and rho to provide to the setSVMdetector function:
(rho, alpha, supportVectorIndices) = svm.getDecisionFunction(0) supportVectors = svm.getSupportVectors().ravel() supportVectors = np.append(supportVectors, -rho) pickle.dump(supportVectors, open("svm.pickle", 'wb')) print("FINISHED!")
However, when I run the code, the rectangle is always drawn in the center before or after the NMS. Check here:
Below is the code provided by OpenCV for HOG detector. I have just made a change in the setSVMDetector function where I am providing my trained support vectors now.
hog = cv2.HOGDescriptor() svm = pickle.load(open("svmcoeff.pickle", 'rb')) hog.setSVMDetector(np.array(svm))
for i in range(1,9): image = cv2.imread('test-'+str(i)+'.png') image = imutils.resize(image, width=min(300, image.shape[1])) orig = image.copy() (rects, weights) = hog.detectMultiScale(image)
for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255),2) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None,overlapThresh=0.65)
for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) cv2.imshow("Before NMS", orig) cv2.imshow("After NMS", image) key = cv2.waitKey(0) if key == 27: continue
what are the SVM params for the training ? (you need to do a regression(SVM::EPS_SVR), not classification)
Hi, thanks for your reply. I was using these parameters for SVM training: svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_LINEAR) svm.setGamma(0.0001) svm.setC(10)
Now, I tried with regression too: svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_EPS_SVR) svm.setKernel(cv2.ml.SVM_LINEAR) svm.setP(0.1)
The support vectors are again not even close to the default support vectors and the rectangle is still being drawn in the center.
https://github.com/opencv/opencv/blob...
svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_EPS_SVR) svm.setKernel(cv2.ml.SVM_LINEAR) svm.setGamma(0) svm.setC(10) svm.setNu(0.5) svm.setP(0.1) svm.setC(0.01) svm.setTermCriteria((cv2.TermCriteria_MAX_ITER+cv2.TermCriteria_EPS, 1000, 1e-3))
Unfortunately, this didn't work either. :/
I am guessing something is going wrong in the non max suppression, always leading to a central rectangle. Can you give us the code?
Hi, thanks for your reply. The problem is same even if I don't apply the non max suppression. I have already provided the codes. I think, the problem is with SVM training and/or Support vectors extraction. Can you please lookup those parts and maybe suggestion something? :/
I am used to the C++ interface. Is it possible saving and reading to and from the pickle file destroys the makeup of the model?