使用opencv构建自定义的svm内核矩阵 [英] Build a custom svm kernel matrix with opencv

查看:123
本文介绍了使用opencv构建自定义的svm内核矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to train a Support Vector Machine model and I'd like to use a custom kernel matrix, instead of the preset ones (like RBF, Poly, ecc.). How can I do that (if is it possible) with opencv's machine learning library?

谢谢!

推荐答案

AFAICT,OpenCV不直接支持SVM的自定义内核.看起来LIBSVM是OpenCV为此使用的基础库,它没有提供特别简单的方法来定义自定义内核.因此,许多使用LIBSVM的包装器也不提供此功能.似乎有一些适用于python的scikit:具有自定义内核的SVM的scikit示例

AFAICT, custom kernels for SVM aren't supported directly in OpenCV. It looks like LIBSVM, which is the underlying library that OpenCV uses for this, doesn't provide a particularly easy means of defining custom kernels. So, many of the wrappers that use LIBSVM don't provide this either. There seem to be a few, e.g. scikit for python: scikit example of SVM with custom kernel

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

You could also take a look at a completely different library, like SVMlight. It supports custom kernels directly. Also take a look at this SO question. The answers there include a handful of SVM libraries, along with brief reviews.

如果有令人信服的理由留在OpenCV中,则可以通过使用内核类型CvSVM::LINEAR并在训练SVM之前将自定义内核应用于数据来实现此目的.我对这个方向是否会产生成果感到有些模糊,所以我希望对SVM有更多经验的人可以加入并发表评论.如果通过选择线性"作为内核,可以使用预计算内核",请查看

If you have compelling reasons to stay within OpenCV, you might be able to accomplish it by using kernel type CvSVM::LINEAR and applying your custom kernel to the data before training the SVM. I'm a little fuzzy on whether this direction would be fruitful, so I hope someone with more experience with SVM can chime in and comment. If it is possible to use a "precomputed kernel" by choosing "linear" as your kernel, then take a look at this answer for more ideas on how to proceed.

您还可以考虑包括LIBSVM并直接调用它,而不使用OpenCV.请参阅关于LIBSVM的常见问题#418 ,谈到如何做自定义内核:

You might also consider including LIBSVM and calling it directly, without using OpenCV. See FAQ #418 for LIBSVM, which briefly touches on how to do custom kernels:

问:我想使用自己的内核.有什么例子吗?在svm.cpp中,有两个用于内核评估的子例程:k_function()和kernel_function().我应该修改哪一个?

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 ?

一个示例是LIBSVM工具中的用于字符串数据的LIBSVM".

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

我们具有两个功能的原因如下.对于RBF内核exp(-g | xi-xj | ^ 2),如果我们先计算xi-xj,然后计算范数平方,则将进行3n次运算.因此,我们考虑exp(-g(| xi | ^ 2-2dot(xi,xj)+ | xj | ^ 2)),并通过在开始时计算所有| xi | ^ 2,将运算次数减少为2n.这是为了训练.为了进行预测,我们无法执行此操作,因此需要使用3n操作的常规子例程.拥有自己的内核的最简单方法是通过替换任何内核,将相同的代码放入这两个子例程中.

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.祝您好运!

That last option sounds like a bit of a pain, though. I'd recommend scikit or SVMlight. Best of luck to you!

这篇关于使用opencv构建自定义的svm内核矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆