SVM weights to HOGDescriptor [closed]

asked 2014-04-01 07:12:40 -0600

Hansg91 gravatar image

updated 2014-04-01 08:18:52 -0600


I am working on a detection algorithm and I got a working SVM classifier that uses HOG features calculated with a HOGDescriptor object. If I manually test this classifier (ie. feed it with positive and negative examples that have not been used for training), it classifies them fine. The step where I go from my SVM classifier to the HOGDescriptor is where I assume it goes wrong, it doesn't identify a single object correctly. I have no idea where to start how to debug this..

Here is my code:

Any help is greatly appreciated!

Best regards, Hans


To make things clearer so you don't have to go through the entire code, this is how I read in data from the stored SVM classifier:

# load the stored SVM
f = open('model.svm', 'r')
model = yaml.load(f)
rho = model['my_svm']['decision_functions'][0]['rho']
svm_detector = np.array(model['my_svm']['support_vectors'])[0,:]
hog.setSVMDetector(np.hstack([svm_detector, rho]))

There is only one support vector in my classifier, I assumed the SVM weights I need to give to HOGDescriptor.setSVMDetector are these weights with appended the threshold (stored as 'rho' in the yaml file). The HOGDescriptor is initialised as follows :

self.hog = cv2.HOGDescriptor((64,64), (16,16), (8,8), (8,8), 9)

Detection is then simply done using :

locations, weights = hog.detectMultiScale(image)

I didn't quite understand what the parameters in detectMultiScale do exactly and there isn't much documentation regarding their effects. I supplied the SVM with objects of size 64x64 which didn't differ much from the default size of 64x128 so I kept the rest of the parameters the same. I tried 'playing' around with them, but did not achieve much better results.

The summary of my issue now is: If I manually cut out unseen positive/negative examples, my SVM classifier of OpenCV correctly classifies its labels (using SVM.predict). However if I use this classifier inside a HOGDescriptor with mostly default parameters and use HOGDescriptor.detectMultiScale, I get unexpected results (ie. only a few detections at false locations). I have two assumptions, either my parameters are incorrect or my process of getting the SVM weights from the SVM classifier to the HOGDescriptor is false.

For more information, don't hesitate to ask.


After reading through this question, I noticed I need to take the negative of my weights when going from SVM -> HOGDescriptor. This improved my results a lot. I used a very small training set, so I didn't expect it to be flawless, this seems to be what I had expected.

Results using negative weights

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by StevenPuttemans
close date 2014-04-02 01:42:46.355481


I do not understand how people can actually expect that if they drop their complete project, others will manually go through it to find a solution ... please put some effort in your question.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 07:23:26 -0600 )edit

This isn't my complete project, I had already taken the effort to size it down to what it is now. I wanted to show at least how I trained the classifier and that it was indeed working using cross validation, but that it failed to work on the multi scale detection. What do you suggest?

Hansg91 gravatar imageHansg91 ( 2014-04-01 07:29:36 -0600 )edit

I suggest you create a topic like we suggest to do it. Add the needed content right here in this topic, instead of using external links ... Also it is not clear to me where your problem is now exactly. You say the classification goes ok manually, but then all at ones you say it goes wrong. This is contradictory for me...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 07:31:52 -0600 )edit

Thank you!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 07:40:06 -0600 )edit

I updated it twice, to add more detailed information. Sorry about before, I was trying to be brief and precise as to not create an incredibly large post, but I understand it wouldn't help my cause. I would really like to do anything I can to fix this, so let me know if there is anything else I can add and I will do my best to do so.

Hansg91 gravatar imageHansg91 ( 2014-04-01 07:44:39 -0600 )edit

Ah I think I know of your problem. The SVM classifier doesn't supply your classifier in the same format as the HOG classifier expects. I have been pruning around this code also, maybe this topic of me could lead to some more insights in the problem? It should be something with getting the SVM to its primal form.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 07:47:34 -0600 )edit

Thank you for your reply! I looked through your topic and noticed that when you convert it to a primal form, you use '-myalpha'. I added a minus sign, and I get much better results now! I had only 24 positive and 36 negative examples, so I didn't expect perfect results , but what I have now seems promising. I will try to increase my dataset now, see how that affects my results. Thanks for your help!

Hansg91 gravatar imageHansg91 ( 2014-04-01 08:19:11 -0600 )edit

If you at a certain moment find solutions to my questions, being the weight factors, feel free to leave a reply! As to the minus sign, it is something which feels unnaturally to me, but it does get me correct results in dummy cases. Found it in another forum. Maybe a bad implementation in the ML module?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 08:38:30 -0600 )edit

If I come across something like that I will let you know. It does indeed sounds like some bad implementation. It would be one thing if the algorithms came from different packages, but they should both have been adapted for the 'opencv standard'.

Hansg91 gravatar imageHansg91 ( 2014-04-01 09:25:59 -0600 )edit