深度学习基础03---支持向量机SVM人脸识别实例

it2026-01-06  10

这篇文章介绍一下支持向量机SVM针对线性不可分情况的应用,以人脸识别为例。

引入python相关模块

from __future__ import print_function from time import time#计时板块 import logging#打印出进展信息板块 import matplotlib.pyplot as plt#绘图板块 from sklearn.model_selection import train_test_split from sklearn.datasets import fetch_lfw_people #from sklearn.grid_search import GridSearchCV from sklearn.model_selection import GridSearchCV from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix from sklearn.decomposition import PCA #from sklearn.decomposition import RandomizedPCA from sklearn.svm import SVC#核心库

数据集中基本信息

logging.basicConfig(level=logging.INFO,format='%(asctime)s %(message)s')#打印具体信息 lfw_people=fetch_lfw_people(min_faces_per_person=70,resize=0.4)#下载人脸数据库,类似于字典结构的对象 n_samples,h,w=lfw_people.images.shape#返回数据集实例个数,h和w X=lfw_people.data#特征向量的矩阵 n_features=X.shape[1]#返回矩阵的列数,当shape[2]时返回行数 y=lfw_people.target#对应的人脸标记 target_names=lfw_people.target_names#返回name 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)

输出结果: 纬度达到了1850,太高了,所以后期我们需要降维

创建分类器

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25)#划分训练集与测试集 n_components=150#组成元素数量,这里设定为150 print('Extracting the top %d eigenfaces from %d faces'%(n_components,X_train.shape[0])) t0=time()#初始时间 pca=PCA(n_components=n_components,whiten=True).fit(X_train)#PCA降维 print('done in %0.3f'%(time()-t0)) engenfaces=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.3f'%(time()-t0)) print('Fitting the classifier to the training set') to=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='balanced'),param_grid)#分类器,权重为自动 clf=clf.fit(X_train_pca,y_train)#建模 print('done in %0.3f'%(time()-t0)) print('Best estimatoc found by grid search:') print(clf.best_estimator_)#基本信息

测试模型准确率

print('Predicting people''s names 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_classes)))#混淆矩阵,打印正确结果

定义函数打印可视化结果

#######定义函数打印可视化结果 def plot_gallery(images,titles,h,w,n_rows=3,n_col=4): 'Helper function to plot a gallery of portraits' plt.figure(figsize=(1.8*n_col,2.4*n_rows))#建立一张图为背景 plt.subplots_adjust(bottom=0,left=0.01,right=0.99,top=0.90,hspace=0.35) for i in range(n_rows*n_col): plt.subplot(n_rows,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_red,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) engenface_titles=['eigenface %d'%i for i in range(engenfaces.shape[0])] plot_gallery(engenfaces,engenface_titles,h,w) plt.show()

fun,预测准确率达到了96%

最新回复(0)