简体   繁体   English

随机森林不在opencv python(cv2)中工作

[英]Random Forest not working in opencv python (cv2)

I can't seem to correctly pass in the parameters to train a Random Forest classifier in opencv from python. 我似乎无法正确传递参数来从python中训练opencv中的随机森林分类器。

I wrote an implementation in C++ which worked correctly, but do not get the same results in python. 我在C ++中编写了一个正常工作的实现,但在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 我在这里找到了一些示例代码: http//fossies.org/linux/misc/opencv-2.4.7.ta​​r.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. 这似乎表明你应该传入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);

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++. 我可以说分类器正在接受正确的训练,但它并不像我在C ++中使用相同参数训练的那样准确。 I'm fairly certain that the parameters are getting acknowledged, because I get different results when I tweak the values. 我相当肯定参数已被确认,因为当我调整值时,我会得到不同的结果。

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: 我看了一下openCV实现:

http://www.code.opencv.org/svn/gsoc2012/denoising/trunk/opencv-2.4.2/modules/ml/src/rtrees.cpp http://www.code.opencv.org/svn/gsoc2012/denoising/trunk/opencv-2.4.2/modules/ml/src/rtrees.cpp

Given my parameters, it should output a forest with 1000 trees. 根据我的参数,它应该输出一个有1000棵树的森林。 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. 我尝试将max_num_of_trees_in_the_forest参数设置为各种疯狂值,并且它没有改变OpenCV的行为。

Thoughts? 思考?

Not sure if this will help much, but I believe: 不确定这是否会有所帮助,但我相信:

n_active_vars=0

should be 应该

nactive_vars=0

Also, you may wish to try experimenting with the term_crit parameter. 此外,您可能希望尝试使用term_crit参数进行试验。 For example, try adding: 例如,尝试添加:

term_crit=(cv2.TERM_CRITERIA_MAX_ITER,1000,1)

into your dictionary. 进入你的字典。

I believe this will set the criteria to terminate when 1000 trees are added into the forest. 我相信这将设置在1000棵树添加到森林中时终止的标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM