SVM weights to HOGDescriptor [closed]
Hey,
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: https://dl.dropboxusercontent.com/u/40610835/HOG%20Detection.html
Any help is greatly appreciated!
Best regards, Hans
Update:
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.
Update2:
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.
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.
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?
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...
Thank you!
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.
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.
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!
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?
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'.