溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于svm和pca的人臉識別案例分析

發(fā)布時(shí)間:2020-05-23 13:27:12 來源:網(wǎng)絡(luò) 閱讀:1448 作者:季沉Trace 欄目:大數(shù)據(jù)
數(shù)據(jù)集介紹

LFW (Labeled Faces in the Wild) 人臉數(shù)據(jù)庫是由美國馬薩諸塞州立大學(xué)阿默斯特分校計(jì)算機(jī)視覺實(shí)驗(yàn)室整理完成的數(shù)據(jù)庫,主要用來研究非受限情況下的人臉識別問題。LFW 數(shù)據(jù)庫主要是從互聯(lián)網(wǎng)上搜集圖像,而不是實(shí)驗(yàn)室,一共含有13000 多張人臉圖像,每張圖像都被標(biāo)識出對應(yīng)的人的名字,其中有1680 人對應(yīng)不只一張圖像,即大約1680個(gè)人包含兩個(gè)以上的人臉。LFW數(shù)據(jù)集主要測試人臉識別的準(zhǔn)確率。

代碼實(shí)現(xiàn)
from time import time               #記錄時(shí)間
import logging                      #打印程序的運(yùn)行日志
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split   #劃分訓(xùn)練集和測試集
from sklearn.datasets import fetch_lfw_people           #導(dǎo)入數(shù)據(jù)集(名人)
from sklearn.grid_search import GridSearchCV            #調(diào)試函數(shù)的參數(shù)
from sklearn.metrics import classification_report       #顯示分類報(bào)告,顯示主要的分類指標(biāo),準(zhǔn)確率,召回率以及F1得分
from sklearn.metrics import confusion_matrix            #對真實(shí)類別和預(yù)測類別做出判斷,用矩陣形式表示出來
from sklearn.decomposition import RandomizedPCA         #pca降維
from sklearn.svm import SVC                             #svm的svc方程
from sklearn.cluster.tests.test_k_means import n_samples

#訓(xùn)練集:sklearn自帶的人臉圖片數(shù)據(jù)集
lfw_people = fetch_lfw_people(min_faces_per_person=70,resize=0.4)
n_samples,h,w = lfw_people.images.shape     #實(shí)例數(shù)目、h、w
x = lfw_people.data                         #所有的訓(xùn)練數(shù)據(jù),1288張圖片,每張圖片1850個(gè)特征值
n_features = x.shape[1]                     #特征向量的維度1850
y =lfw_people.target                        #對應(yīng)的人臉標(biāo)記
target_names = lfw_people.target_names      #需要識別的人名字
n_class = target_names.shape[0]             #幾個(gè)人需要識別

print("total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_class: %d" % n_class)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25)
n_components = 150
t0 = time()
#隨機(jī)將高維的特征向量降低為低維的,先建立模型
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))            #將人臉特征轉(zhuǎn)化為特征向量集
print('projecting the input data on the eigenfaces orthonomal basis')
t0 = time()
#進(jìn)行數(shù)據(jù)模型降維,降成了150
x_train_pca = pca.transform(x_train)
x_test_pca = pca.transform(x_test)
print("done in %0.3fs" % (time() - t0))
print("fitting the classfier to the training set")
t0 = time()
#C 是對錯誤部分的懲罰;gamma 合成點(diǎn)
para_grid = {'C':[1e3,5e3,1e4,5e4,1e5],'gamma':[0.0001,0.0005,0.0001,0.005,0.01,0.1],}
#rbf處理圖像較好,C和gamma組合,窮舉出最好的一個(gè)組合 使用GridSearchCV進(jìn)行自由組合,最終確定合適的組合
clf = GridSearchCV(SVC(kernel='rbf'),para_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_)                                       #最好的模型的信息
print("predict the people's name on the test 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_class)))

def plot_gallery(images,titles,h,w,n_row = 3,n_col = 4):
    plt.figure(figsize=(1.8*n_col,2.4*n_row))
    plt.subplots_adjust(bottom = 0,left = .01,right = .99,top = .90,hspace = .35)
    for i in range(n_row * n_col):
        plt.subplot(n_row,n_col,i+1)
        plt.imshow(images[i].reshape((h,w)),cmap=plt.cm.gray)
        plt.title(titles[i],size = 12)
        plt.xticks()
        plt.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 \nture: %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_title = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces,eigenface_title,h,w)
plt.show()
結(jié)果
total dataset size:
n_samples: 1288
n_features: 1850
n_class: 7
done in 0.270s
projecting the input data on the eigenfaces orthonomal basis
done in 0.040s
fitting the classfier to the training set
done in 30.796s
best estimator found by grid search:
SVC(C=1000.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma=0.005, kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
predict the people's name on the test set
done in 0.125s
                                precision    recall  f1-score   support

Ariel Sharon                0.91        0.67      0.77        15
Colin Powell                0.80        0.82      0.81        45
Donald Rumsfeld        0.96        0.61      0.75        41
George W Bush          0.79        0.96      0.87       144
Gerhard Schroeder     0.95        0.63      0.76        30
Hugo Chavez              1.00        0.79      0.88        19
Tony Blair                    0.86        0.89      0.88        28
avg / total                    0.85        0.84      0.83       322
混淆矩陣
[[ 10   2   0   3   0   0   0]
 [  1  37   0   7   0   0   0]
 [  0   1  25  13   1   0   1]
 [  0   4   1 138   0   0   1]
 [  0   1   0   8  19   0   2]
 [  0   1   0   3   0  15   0]
 [  0   0   0   3   0   0  25]]

基于svm和pca的人臉識別案例分析

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI