随机森林在opencv python(cv2)中不起作用 [英] Random Forest not working in opencv python (cv2)
问题描述
我似乎无法正确地传递参数来从python的opencv中训练随机森林分类器.
I can't seem to correctly pass in the parameters to train a Random Forest classifier in opencv from python.
我用C ++编写了一个可以正常工作的实现,但是在python中却没有得到相同的结果.
I wrote an implementation in C++ which worked correctly, but do not get the same results in python.
I found some sample code here: http://fossies.org/linux/misc/opencv-2.4.7.tar.gz:a/opencv-2.4.7/samples/python2/letter_recog.py
似乎表明您应该在字典中传递参数.这是我正在使用的代码:
which seems to indicate that you should pass in the parameters in a dict. Here is the code I am using:
rtree_params = dict(max_depth=11, min_sample_count=5, use_surrogates=False, max_categories=15, calc_var_importance=False, n_active_vars=0, max_num_of_trees_in_the_forest=1000, termcrit_type=cv2.TERM_CRITERIA_MAX_ITER)
classifier = cv2.RTrees()
classifier.train(train_data, cv2.CV_ROW_SAMPLE, label_data, params=rtree_params);
我可以告诉您分类器正在正确地训练,但是它不如我在C ++中使用相同参数训练的分类器那么准确.我相当确定这些参数已经得到确认,因为在调整值时会得到不同的结果.
I can tell that the classifier is getting trained correctly, but it is not nearly as accurate as the one I trained with the same parameters in C++. I'm fairly certain that the parameters are getting acknowledged, because I get different results when I tweak the values.
我确实注意到,当我将分类器输出到文件时,它只有一棵树.我很确定这是问题所在.我看了一下openCV的实现:
I did notice that when I output the classifier to a file, it only has one tree. I'm pretty sure this is the problem. I looked at the openCV implementation:
http://www.code.opencv.org/svn/gsoc2012/denoising/trunk/opencv-2.4.2/modules/ml/src/rtrees.cpp
给出我的参数,它应该输出一个有1000棵树的森林.我尝试将max_num_of_trees_in_the_forest
参数设置为各种疯狂的值,但这并没有改变OpenCV的行为.
Given my parameters, it should output a forest with 1000 trees. I tried setting the max_num_of_trees_in_the_forest
arguments to all sorts of crazy values, and it didn't change OpenCV's behaviour.
有想法吗?
推荐答案
不确定是否会有所帮助,但我相信:
Not sure if this will help much, but I believe:
n_active_vars=0
应该是
nactive_vars=0
此外,您可能希望尝试使用term_crit参数. 例如,尝试添加:
Also, you may wish to try experimenting with the term_crit parameter. For example, try adding:
term_crit=(cv2.TERM_CRITERIA_MAX_ITER,1000,1)
进入字典.
我相信这将为在森林中添加1000棵树时终止设置标准.
I believe this will set the criteria to terminate when 1000 trees are added into the forest.
这篇关于随机森林在opencv python(cv2)中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!