我正在将使用支持向量机的算法从 Python(使用 scikit-learn)移植到 C++(使用 OpenCV 的机器学习库)。
我可以使用 Python 访问经过训练的 SVM,并且可以将 SVM 模型参数从 XML 文件导入 OpenCV。由于scikit-learn和OpenCV的SVM实现都是基于LibSVM的,所以我认为应该可以在OpenCV中使用经过训练的scikit SVM的参数。
下面的示例显示了一个可用于在 OpenCV 中初始化 SVM 的 XML 文件:
<?xml version="1.0"?>
<opencv_storage>
<my_svm type_id="opencv-ml-svm">
<svm_type>C_SVC</svm_type>
<kernel><type>RBF</type>
<gamma>0.058823529411764705</gamma></kernel>
<C>100</C>
<term_criteria><epsilon>0.0</epsilon>
<iterations>1000</iterations></term_criteria>
<var_all>17</var_all>
<var_count>17</var_count>
<class_count>2</class_count>
<class_labels type_id="opencv-matrix">
<rows>1</rows>
<cols>2</cols>
<dt>i</dt>
<data>
0 1</data></class_labels>
<sv_total>20</sv_total>
<support_vectors>
<_>
2.562423055146794554e-02 1.195797425735170838e-01
8.541410183822648050e-02 9.395551202204914520e-02
1.622867934926303379e-01 3.074907666176152077e-01
4.099876888234874062e-01 4.697775601102455179e-01
3.074907666176152077e-01 3.416564073529061440e-01
5.124846110293592716e-01 5.039432008455355660e-01
5.466502517646497639e-01 1.494746782168964394e+00
4.168208169705446942e+00 7.214937388193202183e-01
7.400275229357797802e-01</_>
<!-- omit 19 vectors to keep it short -->
</support_vectors>
<decision_functions>
<_>
<sv_count>20</sv_count>
<rho>-5.137523249549433402e+00</rho>
<alpha>
2.668992955678978518e+01 7.079767098112181145e+01
3.554240018130368384e+01 4.787014908624512088e+01
1.308470223155845069e+01 5.499185410034550614e+01
4.160483074010306126e+01 2.885504210853826379e+01
7.816431542954153144e+01 6.882061506693679576e+01
1.069534676985309574e+01 -1.000000000000000000e+02
-5.088050252552544350e+01 -1.101740897543916375e+01
-7.519686789702373630e+01 -3.893481464245511603e+01
-9.497774056452135483e+01 -4.688632332663718927e+00
-1.972745089701982835e+01 -8.169343841768861125e+01</alpha>
<index>
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
</index></_></decision_functions></my_svm>
</opencv_storage>
我现在想用经过训练的 scikit-learn SVM 中的值填充此 XML 文件。但我不确定scikit-learn和OpenCV的参数如何对应。这是我到目前为止所拥有的(clf
是Python中的分类器对象):
-
<kernel><gamma>
对应于clf.gamma
-
<C>
对应于clf.C
-
<term_criteria><epsilon>
对应于clf.tol
-
<support_vectors>
对应于clf.support_vectors_
到目前为止这是正确的吗?现在这里是我不太确定的项目:
- 关于什么
<term_criteria><iterations>
?
- Does
<decision_functions><_><rho>
相当于clf.intercept_
?
- Does
<decision_functions><_><alpha>
correspond to clf.dual_coef_
? Here I'm not sure because the scikit-learn documentation says "dual_coef_
which holds the product yiαi". It looks like OpenCV expects only αi, and not yiαi.
你不需要epsilon
and iterations
这些不再用于训练优化问题。您可以将它们设置为您最喜欢的号码或忽略它们。
移植支持向量可能需要一些调整,因为索引可能不同scikit学习 /questions/tagged/scikit-learn and opencv /questions/tagged/opencv。例如,您示例中的 XML 没有稀疏格式。
至于其他参数:
-
rho
应该对应于intercept_
,但您可能需要更改符号。
- scikit 的
dual_coef_
对应于sv_coef
在标准中libsvm /questions/tagged/libsvm模型(即 alpha_i*y_i)。
If opencv /questions/tagged/opencv抱怨你所提供的价值观alpha
移植时,使用绝对值scikit学习 /questions/tagged/scikit-learn's dual_coef_
(例如全部积极)。这些是 SVM 模型的真实 alpha 值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)