使用 Python/Numpy 的 dlib SVM 的最小示例

Minimal Example for dlib SVM using Python/Numpy

我需要在 C++ 构建目标系统中部署 SVM。因此,我想使用带有 python/numpy 的 dlib 训练 SVM,将其序列化并在目标系统中进行评估。

dlib 的 python 文档对我来说相当晦涩,所以任何人都可以帮我解决这个最小的例子吗?

import dlib

# My data in numpy
feature_column_1 = np.array([-1, -2, -3, 1, 2, 3])
feature_column_2 = np.array([1, 2, 3, -1, -2, -3])
labels = np.array([True, True, True, False, False, False])

# Features
feature_vectors = dlib.vectors()
for feature_column in [feature_column_1, feature_column_2]:
    feature_vectors.append(dlib.vector(feature_column.tolist()))

# Labels
labels_array = dlib.array(labels.tolist())

# Train
svm = dlib.svm_c_trainer_linear()
svm.train(feature_vectors, labels_array)

# Test
y_probibilities = svm.predict(labels_array_new)

我在训练中遇到以下错误:

---> 18 svm.train(vectors, array)

ValueError: Invalid inputs

不是一个完整的答案,但有一些评论:

(1)

您观察到的错误至少部分是由于检查 here

    for (long r = 0; r < x_labels.nr(); ++r)
    {
        if (x_labels(r) != -1 && x_labels(r) != 1)
            return false;

表示:labels = np.array([True, True, True, False, False, False])错误,labels = np.array([1, 1, 1, -1, -1, -1])正确

(2)

通常,在大多数 MLlib 中,数据格式为 (n_samples, n_features),其中行是观察值。

当打印出你的特征向量时,这看起来是正确的,但是你的代码,在改变 (1) 之后仍然会抛出同样的错误,除非你把它当作反过来:所以另一种解释是:你有 2 个样本,每个样本有 6 个特征。使用这个假设,y 需要有 2 个值。 Et voilà...至少它在训练!

没有抛出错误的代码(我忽略了它真正在做什么)

import dlib
import numpy as np

# My data in numpy
feature_column_1 = np.array([-1, -2, -3, 1, 2, 3])
feature_column_2 = np.array([1, 2, 3, -1, -2, -3])
labels = np.array([-1, 1])  # +1/-1 & size == 2

# Features
feature_vectors = dlib.vectors()
for feature_column in [feature_column_1, feature_column_2]:
    feature_vectors.append(dlib.vector(feature_column.tolist()))

# Labels
labels_array = dlib.array(labels.tolist())

# Train
svm = dlib.svm_c_trainer_linear()
svm.be_verbose = 10

svm.train(feature_vectors, labels_array)
print('k')

(3)

dlib 的 python-API 没有 predict() 函数。 C++-API 有一些用于预测的 learned_function 属性,但是 this and maybe this 表示你必须自己做(可能使用 c_class1 和 co. 因为我不能将其他任何东西映射到 API-doc 的候选者可用的东西。

编辑: 我的预测是错误的,正如评论中提到的 dlib 的 maintainer/developer!

我真的很不喜欢这个状态,会用别的东西! (或改进 dlib).

不确定如何解释您的环境约束但是:

  • libsvm / liblinear 仍然是此类任务的最先进技术,它们是为从 C++ 使用而构建的!
  • sklearn 是迄今为止从 python 做到这一点的最好方法(内部使用上面的库)

当我将你的 for 循环更改为此时(我没有 dlib):

for feature_column in [feature_column_1, feature_column_2]:
    print(feature_column.tolist())

我得到这个结果

[-1, -2, -3, 1, 2, 3]
[1, 2, 3, -1, -2, -3]

我想您的数据应该如下所示:

[-1, 1]
[-2, 2]
[-2, 2]
[1, -1]
[2, -2]
[3, -3]

您可以通过这段代码得到什么:

features = [[-1,1],[-2,2],[-2,2],[1,-1],[2,-2],[3,-3]]
for feature in features:
    feature_vectors.append(dlib.vector(feature_column.tolist()))

我刚刚为 dlib 添加了一个官方示例。我很惊讶地发现它不包括在内。可在此处获取:https://github.com/davisking/dlib/blob/master/python_examples/svm_binary_classifier.py。以下是相关详情:

import dlib
import pickle    

x = dlib.vectors()
y = dlib.array()

# Make a training dataset.  Here we have just two training examples.  Normally
# you would use a much larger training dataset, but for the purpose of example
# this is plenty.  For binary classification, the y labels should all be either +1 or -1.
x.append(dlib.vector([1, 2, 3, -1, -2, -3]))
y.append(+1)

x.append(dlib.vector([-1, -2, -3, 1, 2, 3]))
y.append(-1)


# Now make a training object.  This object is responsible for turning a
# training dataset into a prediction model.  This one here is a SVM trainer
# that uses a linear kernel.  If you wanted to use a RBF kernel or histogram
# intersection kernel you could change it to one of these lines:
#  svm = dlib.svm_c_trainer_histogram_intersection()
#  svm = dlib.svm_c_trainer_radial_basis()
svm = dlib.svm_c_trainer_linear()
svm.be_verbose()
svm.set_c(10)

# Now train the model.  The return value is the trained model capable of making predictions.
classifier = svm.train(x, y)

# Now run the model on our data and look at the results.
print("prediction for first sample:  {}".format(classifier(x[0])))
print("prediction for second sample: {}".format(classifier(x[1])))


# classifier models can also be pickled in the same was as any other python object.
with open('saved_model.pickle', 'wb') as handle:
    pickle.dump(classifier, handle)

但是,如果您想使用 C++,您应该只使用 C++。 Dlib 主要是一个 C++ 库,而不是 python 库。 dlib 的全部意义在于为想要进行机器学习的人提供一个很好的 C++ API。所以你最好只使用 C++ 进行训练。 dlib 和完整的 C++ API 文档附带了 99 个完整的 C++ 示例。例如,这里有一个相关的例子 http://dlib.net/svm_c_ex.cpp.html

我真的应该强调 dlib 的 C++ API 比 python API 灵活得多。真的,dlib 的目的是使机器学习在 C++ 中变得容易,dlib 的 python API 是事后才想到的。事实上,dlib 有很多特性是使用诸如 C++ 模板之类的东西表达的,这些特性在 Python 中可能没有关联(例如,因为 python 没有像 C++ 模板那样的东西)所以这些特性不是暴露于 python。所以说真的,如果你想使用 C++,那就使用 C++。 如果您知道如何编写 C++,就没有理由使用 Python API