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?

asked 2017-05-25 03:29:36 -0500

Umar Farooq gravatar image

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: image description

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

edit retag flag offensive close merge delete

Comments

what are the SVM params for the training ? (you need to do a regression(SVM::EPS_SVR), not classification)

berak gravatar imageberak ( 2017-05-25 03:49:16 -0500 )edit

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.

Umar Farooq gravatar imageUmar Farooq ( 2017-05-25 04:38:17 -0500 )edit

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. :/

Umar Farooq gravatar imageUmar Farooq ( 2017-05-25 05:26:11 -0500 )edit

I am guessing something is going wrong in the non max suppression, always leading to a central rectangle. Can you give us the code?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-05-31 07:47:14 -0500 )edit

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? :/

Umar Farooq gravatar imageUmar Farooq ( 2017-05-31 21:59:00 -0500 )edit

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?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-06-01 02:37:33 -0500 )edit