SVM weights parameter not set through Python
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
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'
Ah great, some proof ! :) How is this generated though? My knowledge of the opencv python bindings is very limited.
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 silentlyThank 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?
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 -- 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.
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
can't be. it's a different interface !