Python scikit-learn 学习笔记—PCA+SVM人脸识别,scikit-learnsvm


  人脸识别是一项实用的技术。但是这种技术总是感觉非常神秘,在sklearn中看到了人脸识别的example,代码网址如下:

http://scikit-learn.org/0.13/auto_examples/applications/face_recognition.html#example-applications-face-recognition-py

  首先介绍一些PCA和SVM的功能,PCA叫做主元分析,它可以从多元事物中解析出主要影响因素,揭示事物的本质,简化复杂的问题。计算主成分的目的是将高维数据投影到较低维空间。

  PCA 主要 用于数据降维,对于一系列例子的特征组成的多维向量,多维向量里的某些元素本身没有区分性,比如某个元素在所有的例子中都为1,或者与1差距不大,那么这个元素本身就没有区分性,用它做特征来区分,贡献会非常小。所以我们的目的是找那些变化大的元素,即方差大的那些维,而去除掉那些变化不大的维,从而使特征留下的都是精品,而且计算量也变小了。

 SVM叫做支持向量机,之前的博客有所涉及有。SVM方法是通过一个非线性映射p,把样本空间映射到一个高维乃至无穷维的特征空间中,使得在原来的样本空间中非线性可分的问题转化为在特征空间中的线性可分的问题。下面的博客也有相关讲解:

http://blog.csdn.net/viewcode/article/details/12840405

  再看看实验采用的数据集,数据集叫做Labeled Faces in the Wild。大约200M左右。整个有10000张图片,5700个人,1700人有两张或以上的照片。相关的网址:http://vis-www.cs.umass.edu/lfw/index.html

  最后看一下代码的实现吧

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

n_samples, h, w = lfw_people.images.shape

X = lfw_people.data
n_features = X.shape[1]

y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]

print "Total dataset size:"
print "n_samples: %d" % n_samples
print "n_features: %d" % n_features
print "n_classes: %d" % n_classes
这一段负责下载数据,并且把数据的维度显示出来。
<span style="font-size:14px;">n_components = 150

print "Extracting the top %d eigenfaces from %d faces" % (
    n_components, X_train.shape[0])
t0 = time()
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
print "done in %0.3fs" % (time() - t0)

eigenfaces = pca.components_.reshape((n_components, h, w))

print "Projecting the input data on the eigenfaces orthonormal basis"
t0 = time()
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print "done in %0.3fs" % (time() - t0)</span>
这一段就是条用了PCA的算法,PCA的reference网址:

http://scikit-learn.org/0.13/modules/generated/sklearn.decomposition.RandomizedPCA.html#sklearn.decomposition.RandomizedPCA

<span style="font-family:Microsoft YaHei;font-size:14px;">print "Fitting the classifier to the training set"
t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
              'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='auto'), param_grid)
clf = clf.fit(X_train_pca, y_train)
print "done in %0.3fs" % (time() - t0)
print "Best estimator found by grid search:"
print clf.best_estimator_</span>
真正用于训练的数据不多。

这一段调用了SVM的算法,还用了相关的网格搜索寻找最佳的参数C和gamma,SVM的reference

http://scikit-learn.org/0.13/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC

<span style="font-size:14px;">print "Predicting the people names on the testing set"
t0 = time()
y_pred = clf.predict(X_test_pca)
print "done in %0.3fs" % (time() - t0)

print classification_report(y_test, y_pred, target_names=target_names)
print confusion_matrix(y_test, y_pred, labels=range(n_classes))

def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
    """Helper function to plot a gallery of portraits"""
    pl.figure(figsize=(1.8 * n_col, 2.4 * n_row))
    pl.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
    for i in range(n_row * n_col):
        pl.subplot(n_row, n_col, i + 1)
        pl.imshow(images[i].reshape((h, w)), cmap=pl.cm.gray)
        pl.title(titles[i], size=12)
        pl.xticks(())
        pl.yticks(())

def title(y_pred, y_test, target_names, i):
    pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
    true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
    return 'predicted: %s\ntrue:      %s' % (pred_name, true_name)

prediction_titles = [title(y_pred, y_test, target_names, i)
                     for i in range(y_pred.shape[0])]

plot_gallery(X_test, prediction_titles, h, w)

eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces, eigenface_titles, h, w)

pl.show()</span>
剩下的就是测试一下,并且把识别的图简单的Po出来,效果如下:

一个是识别的图,一个是特征图。赶紧去试一试吧~

相关内容