我的目标是使用多类线性SVM(带有内核)对图像进行分类。我想编写自己的SVM分类器
我正在使用MATLAB,并使用提供的图像集训练线性SVM。
我有大约20个班级,每个班级5个图像(总共100个图像),我正在使用一对一策略。
每个图像都是(112,92)矩阵。这意味着112 * 92 = 10304值。
我正在使用quadprog(H,f,A,C)
来解决SVM中的二次方程(y = w'x + b)。对quadprog
的一次调用会返回一张图片的大小为10304的w
向量。这意味着我必须拨打quadprog
100次。
问题是一次quadprog
调用需要35秒才能执行。这意味着100张图像需要3500秒。这可能是由于涉及的载体和矩阵的大小。
我想减少quadprog
的执行时间。有没有办法做到这一点?
答案 0 :(得分:0)
First of all, when you do classification using SVM, you usually extract a feature (like HOG) of an image, so that the dimensionality of space on which SVM has to operate gets reduced. You are using raw pixel values, which generates a 10304-D vector. That is not good. Use some standard feature.
Secondly, you do not call quadprog
100 times. You call only once. The concept behind the optimization is, you want to find a weight vector w
and a bias b
such that it satisfies w'x_i+b=y_i
for all images (i.e. all x_i). Note that i
will go from 1 to the number of examples in your training set, but w
and b
stay the same. If you wanted a (w,b)
that will satisfy only one x
, you do not need any fancy optimization. So stack your x
in a big matrix of dimension NxD
, y
will be a vector of Nx1
, and then call quadprog
. It will take a longer time than 35 seconds, but you do it only once. This is called training an SVM
. While testing for a new image, you just extract its feature, and do w*x+b
to get its class.
Third, unless you are doing this as an exercise to understand SVMs, quadprog
is not the best way to solve this problem. You need to use some other techniques which work well with large data, for example, Sequential Minimal Optimization.
How can one linear hyperplane classify more than 2 classes: For multi-class classification, SVMs usually use two popular approaches:
C
class problem, you train C(C-1)/2
classifiers and at test time, you test that many classifiers and choose the class which receives most votes.From LIBSVM FAQs:
It is one-against-one. We chose it after doing the following comparison: C.-W. Hsu and C.-J. Lin. A comparison of methods for multi-class support vector machines , IEEE Transactions on Neural Networks, 13(2002), 415-425.
"1-against-the rest" is a good method whose performance is comparable to "1-against-1." We do the latter simply because its training time is shorter.
However, also note that a naive implementation of one-vs-one may not be practical for large-scale problems. LIBSVM website also lists this shortcoming and provides an extension.
LIBLINEAR does not support one-versus-one multi-classification, so we provide an extension here. If k is the number of classes, we generate k(k-1)/2 models, each of which involves only two classes of training data. According to Yuan et al. (2012), one-versus-one is not practical for large-scale linear classification because of the huge space needed to store k(k-1)/2 models. However, this approach may still be viable if model vectors (i.e., weight vectors) are very sparse. Our implementation stores models in a sparse form and can effectively handle some large-scale data.