opencv - 使用 opencv 构建自定义 svm 内核矩阵

标签 opencv machine-learning classification svm

我必须训练一个支持向量机模型,我想使用自定义内核矩阵,而不是预设的内核矩阵(如 RBF、Poly 等)。 我如何使用 opencv 的机器学习库来做到这一点(如果可能的话)?

谢谢!

最佳答案

AFAICT,OpenCV 不直接支持 SVM 的自定义内核。看起来像 LIBSVM,这是 OpenCV 为此使用的底层库,并没有提供一种特别简单的方法来定义自定义内核。因此,许多使用 LIBSVM 的包装器也不提供此功能。似乎有一些,例如用于 python 的 scikit:scikit example of SVM with custom kernel

您还可以查看一个完全不同的库,例如 SVMlight .它直接支持自定义内核。另请查看 this SO question .那里的答案包括一些 SVM 库,以及简短的评论。

如果您有令人信服的理由留在 OpenCV 中,您可以通过使用内核类型 CvSVM::LINEAR 并在训练 SVM 之前将自定义内核应用于数据来实现它。我对这个方向是否会富有成果有点模糊,所以我希望对 SVM 有更多经验的人可以插话并发表评论。如果它可以通过选择“线性”作为您的内核来使用“预计算内核”,那么看看this answer有关如何进行的更多想法。

您也可以考虑包含 LIBSVM 并直接调用它,而不使用 OpenCV。参见 FAQ #418 for LIBSVM ,其中简要介绍了如何执行自定义内核:

Q: I would like to use my own kernel. Any example? In svm.cpp, there are two subroutines for kernel evaluations: k_function() and kernel_function(). Which one should I modify ?

An example is "LIBSVM for string data" in LIBSVM Tools.

The reason why we have two functions is as follows. For the RBF kernel exp(-g |xi - xj|^2), if we calculate xi - xj first and then the norm square, there are 3n operations. Thus we consider exp(-g (|xi|^2 - 2dot(xi,xj) +|xj|^2)) and by calculating all |xi|^2 in the beginning, the number of operations is reduced to 2n. This is for the training. For prediction we cannot do this so a regular subroutine using that 3n operations is needed. The easiest way to have your own kernel is to put the same code in these two subroutines by replacing any kernel.

不过,最后一个选项听起来有点麻烦。我推荐 scikit 或 SVMlight。祝你好运!

关于opencv - 使用 opencv 构建自定义 svm 内核矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752402/

相关文章:

c++ - 如何将用于演示的运动历史图像制作成一张图像?

opencv - OpenCV中(CountNonZero)和(Moment M00)和(ContourArea)有什么区别?

machine-learning - 神经网络 - 为什么每个人都有不同的 XOR 方法

python - Python 中的一维马氏距离

machine-learning - LS-SVM训练: Out of memory

python - 检测图像中间的颜色(OpenCV、Python)

.net - AWS AmazonComprehend 示例在 .net C# 中运行时遇到问题

machine-learning - 如何提高神经网络输出?

python - Scikit分类报告——改变显示结果的格式

c++ - SANITYCHECK和maxUsedValInHistogramData