OpenCV SVM Classifier not working across platform

Viewed 66

Using Python / OpenCV SVM + Local Binary Pattern (uniform into 59 bins) to train images into classes of -1 (class #1) with ~600 dataset and 1 (class #2) with ~250 dataset. There is also a testing/prediction function to test images on saved model file.

On the C++ side, I only have a testing interface which loads the above created model, calculates the LBP (uniform) of the image and runs prediction.

The above code was completed and tested to work uniformly across both languages/platforms. Then I re-trained the model with more data to improve model accuracy and re-ran tests in Python, which worked as expected. With the (updated) model in C++, all test data is being predicted to one class (class #1) while the same test work as expected in Python. I have cross checked that the LBP function is still returning same values in Python and C++.

This is how the SVM file is available here

Data passed to predict

Python ==> 1 is returned

hist32=[[145. 0. 3. 4. 1. 1. 7. 3. 3. 14. 26. 0. 4. 12. 16. 9. 2. 3. 8. 24. 14. 3. 1. 5. 11. 13. 2. 1. 0. 3. 0. 9. 39. 9. 0. 1. 4. 14. 17. 3. 0. 0. 9. 32. 7. 1. 1. 25. 3. 0. 0. 10. 0. 0. 0. 1. 0. 0. 47.]] 

C++ ==> -1 is returned (should be +1)

[2.0318828e-43, 0, 4.2038954e-45, 5.6051939e-45, 1.4012985e-45, 1.4012985e-45, 9.8090893e-45, 4.2038954e-45, 4.2038954e-45, 1.9618179e-44, 3.643376e-44, 0, 5.6051939e-45, 1.6815582e-44, 2.2420775e-44, 1.2611686e-44, 2.8025969e-45, 4.2038954e-45, 1.1210388e-44, 3.3631163e-44, 1.9618179e-44, 4.2038954e-45, 1.4012985e-45, 7.0064923e-45, 1.5414283e-44, 1.821688e-44, 2.8025969e-45, 1.4012985e-45, 0, 4.2038954e-45, 0, 1.2611686e-44, 5.465064e-44, 1.2611686e-44, 0, 1.4012985e-45, 5.6051939e-45, 1.9618179e-44, 2.3822074e-44, 4.2038954e-45, 0, 0, 1.2611686e-44, 4.4841551e-44, 9.8090893e-45, 1.4012985e-45, 1.4012985e-45, 3.5032462e-44, 4.2038954e-45, 0, 0, 1.4012985e-44, 0, 0, 0, 1.4012985e-45, 0, 0, 6.5861028e-44]
1 Answers

Found the issue while re-framing the question.

The issue was to do with data type of hist. Since I was using a lookup and manually computing the histogram, the values with float type would be completely different than with int.

Before

 lbp = Mat::zeros(1, 59, CV_32F);
 // fill the mat
 lbp = lbp.reshape(1, 1);

now

 lbp = Mat::zeros(59, 1, CV_8U);
 // fill the mat

 lbp.convertTo(lbp, CV_32F);

Related