基于支持向量机(SVM)的人脸识别

数据集加载与可视化

from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60) 
# Check out sample images
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 5)
for i, axi in enumerate(ax.flat):
    axi.imshow(faces.images[i], cmap='bone')
    axi.set(xticks=[], yticks=[], xlabel=faces.target_names[faces.target[i]])
图片[1]-基于支持向量机(SVM)的人脸识别-点头深度学习网站

使用 PCA 和 SVC 构建模型

from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
pca = PCA(n_components=150, whiten=True, random_state=42)
svc = SVC(kernel='rbf', class_weight='balanced')

model = make_pipeline(pca, svc)
# train, test
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(faces['data'], faces['target'], test_size=0.2, random_state=42)

使用网格搜索 CV 查找最佳模型

from sklearn.model_selection import GridSearchCV

param_grid ={
    'svc__C': [1, 5, 10, 15],
    'svc__gamma': [0.0001, 0.0005, 0.001, 0.005],
}
grid = GridSearchCV(model, param_grid=param_grid, cv=5)
%time grid.fit(X_train, y_train)
图片[2]-基于支持向量机(SVM)的人脸识别-点头深度学习网站
grid.best_params_
{'svc__C': 1, 'svc__gamma': 0.005}
grid.best_estimator_
图片[3]-基于支持向量机(SVM)的人脸识别-点头深度学习网站

使用最佳模型进行预测

final_model = grid.best_estimator_
y_pred = final_model.predict(X_test)

可视化数据

fig, ax = plt.subplots(4, 6)
for i, axi in enumerate(ax.flat):
    axi.imshow(X_test[i].reshape(62, 47), cmap='bone')
    axi.set(xticks=[], yticks=[])
    axi.set_ylabel(faces.target_names[y_pred[i]].split()[-1], 
                  color='black' if y_pred[i] == y_test[i] # correct label
                  else 'red') # incorrect label

fig.suptitle('Predicted Names; Incorrected Labels in Red', size=14);

模型评估指标

from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred, 
                            target_names=faces.target_names))
                   precision    recall  f1-score   support

     Ariel Sharon       0.90      0.75      0.82        12
     Colin Powell       0.72      0.94      0.81        51
  Donald Rumsfeld       0.88      0.88      0.88        25
    George W Bush       0.97      0.88      0.92        98
Gerhard Schroeder       0.88      0.71      0.79        21
      Hugo Chavez       0.85      0.73      0.79        15
Junichiro Koizumi       1.00      1.00      1.00        10
       Tony Blair       0.90      0.92      0.91        38

         accuracy                           0.87       270
        macro avg       0.89      0.85      0.86       270
     weighted avg       0.89      0.87      0.88       270
from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_pred)
cm = cm.T

print(cm)
import seaborn as sns
sns.heatmap(data=cm, 
            square=True, 
            annot=True, 
            cbar=False,
            xticklabels=faces.target_names,
            yticklabels=faces.target_names
           );

plt.xlabel('True Labels')
plt.ylabel('Predicted Labels');
图片[4]-基于支持向量机(SVM)的人脸识别-点头深度学习网站

项目资源下载

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容