将训练有素的SVM从scikit-learn导入到OpenCV [英] Import trained SVM from scikit-learn to OpenCV
问题描述
我正在将使用支持向量机的算法从Python(使用scikit-learn)移植到C ++(使用OpenCV的机器学习库).
I'm porting an algorithm that uses a Support Vector Machine from Python (using scikit-learn) to C++ (using the machine learning library of OpenCV).
我可以使用受过训练的Python支持的SVM,并且可以将XML文件中的SVM模型参数导入OpenCV.由于scikit-learn和OpenCV的SVM实现都是基于LibSVM的,因此我认为应该可以在OpenCV中使用经过训练的scikit SVM的参数.
I have access to the trained SVM in Python, and I can import SVM model parameters from an XML file into OpenCV. Since the SVM implementation of both scikit-learn and OpenCV is based on LibSVM, I think it should be possible to use the parameters of the trained scikit SVM in OpenCV.
下面的示例显示了一个XML文件,该文件可用于在OpenCV中初始化SVM:
The example below shows an XML file which can be used to initialize an SVM in OpenCV:
<?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中的分类器对象):
I would now like to fill this XML file with values from the trained scikit-learn SVM. But I'm not sure how the parameters of scikit-learn and OpenCV correspond. Here is what I have so far (clf
is the classifier object in Python):
-
<kernel><gamma>
对应于clf.gamma
-
<C>
对应于clf.C
-
<term_criteria><epsilon>
对应于clf.tol
-
<support_vectors>
对应于clf.support_vectors_
<kernel><gamma>
corresponds toclf.gamma
<C>
corresponds toclf.C
<term_criteria><epsilon>
corresponds toclf.tol
<support_vectors>
corresponds toclf.support_vectors_
到目前为止,这是正确的吗?现在,这些是我不太确定的项目:
Is this correct so far? Now here are the items I'm not really sure:
-
<term_criteria><iterations>
怎么样? -
<decision_functions><_><rho>
对应于clf.intercept_
吗? -
<decision_functions><_><alpha>
对应于clf.dual_coef_
吗?在这里我不确定,因为scikit-learn文档说"dual_coef_
包含产品y i i α i ".看起来OpenCV仅期望α i ,而不期望y i α i .
- What about
<term_criteria><iterations>
? - Does
<decision_functions><_><rho>
correspond toclf.intercept_
? - Does
<decision_functions><_><alpha>
correspond toclf.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
和iterations
,它们已用于训练优化问题中.您可以将它们设置为自己喜欢的号码,也可以忽略它们.
You don't need epsilon
and iterations
anymore, those are used in the training optimization problem. You can set them to your favorite number or ignore them.
移植支持向量可能需要一些摆弄,因为 scikit学习和.例如,您的示例中的XML没有稀疏格式.
Porting the support vectors may require some fiddling, as indexing may be different between scikit-learn and opencv. The XML in your example has no sparse format for example.
其他参数:
-
rho
应该对应于intercept_
,但是您可能需要更改符号. - scikit的
dual_coef_
对应于标准 libsvm中的sv_coef
模型(即alpha_i * y_i).
rho
should correspond tointercept_
, but you may need to change sign.- scikit's
dual_coef_
corresponds tosv_coef
in standard libsvm models (which is alpha_i*y_i).
如果 opencv 抱怨您提供的值对于alpha
,在移植时,请使用的dual_coef_
(例如均为肯定).这些是SVM模型的真实alpha值.
If opencv complains about the values you provide for alpha
when porting, use absolute values of scikit-learn's dual_coef_
(e.g. all positive). These are the true alpha values of an SVM model.
这篇关于将训练有素的SVM从scikit-learn导入到OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!