SVM weights parameter not set through Python

asked 2014-04-15 10:03:41 -0500

Hansg91 gravatar image

Hey,

I am using the SVM implementation in OpenCV (python) and I am running into some trouble setting the class weights parameter. The attribute is called class_weights inside the CVSvmParams class, so I'd expect this to remain the same in the Python interface. I try to assign the parameters as follows:

params = dict( kernel_type = cv2.SVM_LINEAR, 
               svm_type = cv2.SVM_C_SVC,
               C = 1,
               class_weights = [0.1, 0.9],)

However the class_weights variable does not seem to get set inside OpenCV. I added a line to log the value of the _params.class_weights pointer value in CvSVM::train and it prints 0x0.

If I try a similar thing in c++ it does get set. Any idea what is going wrong or what I can further investigate? The wrapper function for CvSvm::train() is the last step I could identify between Python and c++, is there a function before CvSvm::train() that I can check for faults?

Best regards, Hans

edit retag flag offensive close merge delete

Comments

seems you're right about this.

somewhere in the bottom half of pyopencv_generated_types.h , there is a :

static bool pyopencv_to(PyObject* src, CvSVMParams& dst, const char* name)

which handles the svm params, and there's no key for 'class_weights'

berak gravatar imageberak ( 2014-04-15 11:22:01 -0500 )edit

Ah great, some proof ! :) How is this generated though? My knowledge of the opencv python bindings is very limited.

Hansg91 gravatar imageHansg91 ( 2014-04-16 03:49:47 -0500 )edit

modules/python/src2/gen2.py generates it.

(hdr_parser.py looks at the c++ headers, makes a huge mess of {[])()()()}, and passes that to gen2.py). you can just run it on a console to see.. (no fear)

my guess is, that the wrappers can't handle CvMat* as arguments ( your innocent class_weights = [0.1, 0.9] boils down to such a thing when translating to c/c++) and just skip it silently

berak gravatar imageberak ( 2014-04-16 04:00:22 -0500 )edit

Thank you, I checked it out. It seems only properties with the CV_PROP_RW flag get exported. If I add that flag to the CvMat attribute I get a linking error. Same thing when I switch it to a cv::Mat object. Are there no other properties with Mat's that can be used in python?

Hansg91 gravatar imageHansg91 ( 2014-04-16 04:54:05 -0500 )edit

Hello, is there any news 4 years after ? Does someone know what is the state of this issue ? It seems to still not working for me (Python 3.6, OpenCV 3.4)

alarty gravatar imagealarty ( 2019-02-19 04:29:30 -0500 )edit

@alarty -- please do not post answers here, if you have a question or comment, thank you.

then, the ml interfaces have changed significantly since3.0, adding to a question from 2014 might no more be appropriate.

berak gravatar imageberak ( 2019-02-19 04:37:14 -0500 )edit

Where should I post my question if I have exactly the same issue as this post ? This is why, even if a lot of things may have changed, I faced the same issue as 2014, even with a version >3.0. Thanks

alarty gravatar imagealarty ( 2019-02-19 07:21:36 -0500 )edit

I faced the same issue as 2014, even with a version >3.0.

can't be. it's a different interface !

berak gravatar imageberak ( 2019-02-20 05:19:23 -0500 )edit