SVM人脸识别

来源:互联网 发布:远程教育软件哪家好 编辑:程序博客网 时间:2024/06/05 16:05

SVM在中等维度的分类问题中,有较好的表现,其在某种程度上构建了一个简单的网络结构,类似于神经网络中的RBF神经网络。

人脸数据集是经典的分类和聚类问题中经常使用的数据集,维度相对不高,灰度图像,这里选用64*64的人脸图像,将其reshape从1*64^2的一维数组,共40类样本,每组10个。

通常在SVM解决较高维度问题时,需要将其适度降维,这里选用传统的线性降维方法PCA(KPCA同样适用,只是人脸数据集PCA已经够用,不用换成更复杂的非线性降维)。

  • 导入相关模块;
  • 获取数据;
  • 标准化(这里可以不用,图像数据一般已经是标准的)
  • 比较降维到不同的维度下,各自的效果对比(这时SVM的参数不变,选用RBF核函数)
  • 用网格搜索最佳的pca参数和SVM参数,效果演示。
以下为实验代码模块

(1)导入模块
# -*-encoding:utf-8-*-'''created by zwg in 2017-03-01'''import numpy,timefrom sklearn import datasetsfrom sklearn import svmfrom sklearn import decompositionfrom sklearn import manifoldfrom sklearn.cross_validation import train_test_split as ttsfrom sklearn import svm,neural_networkfrom sklearn.metrics import classification_report,precision_score,recall_score,f1_scorefrom sklearn import pipelinefrom sklearn.preprocessing import StandardScaler,MinMaxScalerfrom sklearn.grid_search import GridSearchCVfrom matplotlib import pyplotimport matplotlib.colors as colors

(2)获取数据

def get_data():    face_data=datasets.fetch_olivetti_faces()    #face_data=datasets.load_iris()    data=face_data.data    target=face_data.target    return data,target

(3)PCA函数

def pca(x,n):    pca_learner=decomposition.PCA(n_components=n)    x=pca_learner.fit_transform(x)    return x

(4)PCA & SVM(返回得分系数)

def pca_svm(pca_n=10,svm_C=1):    t1=time.time()    data,target=get_data()    #scale_learner=StandardScaler()    #data=scale_learner.fit_transform(data)    x_train,x_test,y_train,y_test=tts(data,target,random_state=33)    pca_learner=decomposition.PCA(n_components=pca_n)    x_train=pca_learner.fit_transform(x_train)    svm_learner=svm.SVC(C=svm_C)    svm_learner.fit(x_train,y_train)    x_test_pre=pca_learner.transform(x_test)    y_test_pre=svm_learner.predict(x_test_pre)    # report=classification_report(y_test,y_test_pre)    # print 'The Main Explanied: ',numpy.sum(pca_learner.explained_variance_ratio_)    # print report    # print x_test_pre.shape,y_test_pre.shape,y_test.shape    ac=svm_learner.score(x_test_pre,y_test)    p=precision_score(y_test,y_test_pre,average='weighted')    r=recall_score(y_test,y_test_pre,average='weighted')    f1=2.0/(1.0/p+1.0/r)    t=time.time()-t1    return ac,p,r,f1,t

(5)PCA降维到不同维度下效果比较

def pca_svm_time_score_compare():    ac_score=[]    p_score=[]    r_score=[]    f1_score=[]    tt=[]    stand=MinMaxScaler((20,30))    steps=numpy.arange(10,410,10)    for n in steps:        ac,p,r,f1,t=pca_svm(pca_n=n)        p_score.append(p)        f1_score.append(f1)        r_score.append(r)        ac_score.append(ac)        tt.append(t)    p_score_stand=stand.fit_transform(numpy.array(p_score).reshape((-1,1)))    r_score_stand=stand.fit_transform(numpy.array(r_score).reshape((-1,1)))    f1_score_stand=stand.fit_transform(numpy.array(f1_score).reshape((-1,1)))    ac_score_stand=stand.fit_transform(numpy.array(ac_score).reshape((-1,1)))    figure=pyplot.figure()            pyplot.subplot(2,1,1)    pyplot.scatter(steps,f1_score,label='f1-score',color='red',s=p_score_stand,alpha=0.7)    pyplot.scatter(steps,r_score,label='recall-score',color='blue',s=r_score_stand,alpha=0.7)    pyplot.scatter(steps,p_score,label='precision-score',color='yellow',s=f1_score_stand,alpha=0.7)    pyplot.scatter(steps,ac_score,label='accuracy-score',color='purple',s=ac_score_stand,alpha=0.7)    pyplot.xlabel('n-components')    pyplot.ylabel('score')    pyplot.legend()    pyplot.title('The Score Of SVM After PCA To N_components')    pyplot.subplot(2,1,2)    pyplot.plot(steps,tt,label='cost-time',color='black',marker='o')    # for i in range(len(tt)):        # pyplot.text(steps[i],ac_score[i],str(round(tt[i],1))+'s',fontdict=dict(size=10,weight='normal'))        # pyplot.plot([steps[i],steps[i]],[0,ac_score[i]],'--b')    pyplot.legend()    pyplot.xlabel('n-components')    pyplot.ylabel('time')    pyplot.show()

(6)网格搜索pca和SVM最佳参数,并进行可视化

# pca before svm fitting is betterdef pca_svm_pipeline():    #svm_C=numpy.linspace(0.5,10,10)    svm_C=[1]    pca_n_components=numpy.arange(5,200,10)    data,target=get_data()    x_train,x_test,y_train,y_test=tts(data,target,random_state=33)    #scale_learner=StandardScaler()    pca_learner=decomposition.PCA()    svm_learner=svm.SVC()    pipe=pipeline.Pipeline([('pca',pca_learner),('svm',svm_learner)])    gscv=GridSearchCV(pipe,                      {'pca__n_components':pca_n_components,'svm__C':svm_C},n_jobs=-1)    gscv.fit(x_train,y_train)    y_test_pre=gscv.predict(x_test)    report=classification_report(y_test,y_test_pre)    print gscv.best_params_    print report    target_pre=gscv.predict(data)    n1,n2=data.shape    figure=pyplot.figure()    L=numpy.zeros((40,))    xx=numpy.linspace(0,1,64)+13    yy=numpy.linspace(1,0,64)+13    xx,yy=numpy.meshgrid(xx,yy)    for i in xrange(n1):        k=target_pre[i]        g=L[k]        L[k]+=1        xx1=xx-k        yy1=yy-g        pyplot.contourf(xx1,yy1,data[i].reshape((64,64)),cmap='gray')        if target[i]!=target_pre[i]:            pyplot.scatter(numpy.mean(xx1),numpy.mean(yy1),marker='x',c='red',s=40)    pyplot.axis('off')    pyplot.grid('off')    pyplot.title('PCA & SVM Recongnize Faces')    pyplot.show()        


(7)调用

if __name__=='__main__':    pca_svm_pipeline()  #Grid Search and show the results    pca_svm_time_score_compare()  #Direct Search





结果:(1)降维至10~60多维时,效果最好。

结果:(2)最佳参数以及分类结果,pca降至35维,SCM参数C=1,

{'pca__n_components': 35, 'svm__C': 1}
             precision    recall  f1-score   support


          0       1.00      0.80      0.89         5
          1       1.00      1.00      1.00         1
          3       1.00      0.67      0.80         3
          4       1.00      1.00      1.00         1
          5       1.00      1.00      1.00         1
          6       1.00      1.00      1.00         3
          7       0.75      1.00      0.86         3
          8       1.00      1.00      1.00         1
          9       0.67      1.00      0.80         2
         10       1.00      1.00      1.00         1
         11       1.00      1.00      1.00         1
         12       0.50      0.50      0.50         2
         13       1.00      1.00      1.00         1
         14       1.00      1.00      1.00         3
         15       1.00      0.50      0.67         2
         17       1.00      1.00      1.00         2
         18       1.00      1.00      1.00         2
         19       1.00      1.00      1.00         3
         20       1.00      1.00      1.00         2
         21       1.00      1.00      1.00         2
         22       0.67      1.00      0.80         2
         23       1.00      0.50      0.67         2
         24       1.00      1.00      1.00         4
         25       1.00      1.00      1.00         1
         26       1.00      1.00      1.00         4
         27       1.00      1.00      1.00         4
         28       1.00      1.00      1.00         2
         29       1.00      1.00      1.00         4
         30       1.00      1.00      1.00         6
         31       1.00      1.00      1.00         1
         32       1.00      1.00      1.00         4
         33       1.00      1.00      1.00         4
         34       1.00      1.00      1.00         4
         35       1.00      1.00      1.00         3
         36       1.00      1.00      1.00         5
         37       1.00      1.00      1.00         2
         38       1.00      1.00      1.00         4
         39       0.75      1.00      0.86         3


avg / total       0.96      0.95      0.95       100



0 0
原创粉丝点击