標籤:python pca Face Service
關於這部分主要是想在python下實驗一下主成分分析(PCA)演算法以及簡單的Face Service。曾經詳述過matlab下的PCA以及SVM演算法進行Face Service技術,參考如下:
主成分分析法-簡單Face Service(一)
主成分分析-簡單Face Service(二)
PCA實驗人臉庫-Face Service(四)
PCA+支援向量機-Face Service(五)
主成分分析(PCA)演算法主要是對高維資料進行降維,最大限度的找到資料間的相互關係,在機器學習、資料採礦上很有用。在機器學習領域演算法眾多,貼一個:
大神部落格索引
關於PCA的核心思想與原理介紹上述已經給出。而對與在Face Service上的應用需要說明一下,首先是人臉資料庫下載(上述也有)。其次是對人臉資料進行準備。最後使用PCA處理。這裡只貼出修改到python下以後的完整代碼,其中只對ORL資料庫進行了實驗:
# -*- coding: utf-8 -*-"""Created on Sat Jul 25 09:41:24 2015@author: on2way"""import osimport operatorfrom numpy import *import matplotlib.pyplot as pltimport cv2# define PCAdef pca(data,k): data = float32(mat(data)) rows,cols = data.shape#取大小 data_mean = mean(data,0)#對列求均值 data_mean_all = tile(data_mean,(rows,1)) Z = data - data_mean_all T1 = Z*Z.T #使用矩陣計算,所以前面mat D,V = linalg.eig(T1) #特徵值與特徵向量 V1 = V[:,0:k]#取前k個特徵向量 V1 = Z.T*V1 for i in xrange(k): #特徵向量歸一化 L = linalg.norm(V1[:,i]) V1[:,i] = V1[:,i]/L data_new = Z*V1 # 降維後的資料 return data_new,data_mean,V1#covert image to vectordef img2vector(filename): img = cv2.imread(filename,0) #read as ‘gray‘ rows,cols = img.shape imgVector = zeros((1,rows*cols)) #create a none vectore:to raise speed imgVector = reshape(img,(1,rows*cols)) #change img from 2D to 1D return imgVector#load dataSetdef loadDataSet(k): #choose k(0-10) people as traintest for everyone ##step 1:Getting data set print "--Getting data set---" #note to use ‘/‘ not ‘\‘ dataSetDir = ‘C:/Users/myself/.spyder2/machine_learning/att_faces‘ #顯示檔案夾內容 choose = random.permutation(10)+1 #隨機排序1-10 (0-9)+1 train_face = zeros((40*k,112*92)) train_face_number = zeros(40*k) test_face = zeros((40*(10-k),112*92)) test_face_number = zeros(40*(10-k)) for i in xrange(40): #40 sample people people_num = i+1 for j in xrange(10): #everyone has 10 different face if j < k: filename = dataSetDir+‘/s‘+str(people_num)+‘/‘+str(choose[j])+‘.pgm‘ img = img2vector(filename) train_face[i*k+j,:] = img train_face_number[i*k+j] = people_num else: filename = dataSetDir+‘/s‘+str(people_num)+‘/‘+str(choose[j])+‘.pgm‘ img = img2vector(filename) test_face[i*(10-k)+(j-k),:] = img test_face_number[i*(10-k)+(j-k)] = people_num return train_face,train_face_number,test_face,test_face_number# calculate the accuracy of the test_facedef facefind(): # Getting data set train_face,train_face_number,test_face,test_face_number = loadDataSet(3) # PCA training to train_face data_train_new,data_mean,V = pca(train_face,30) num_train = data_train_new.shape[0] num_test = test_face.shape[0] temp_face = test_face - tile(data_mean,(num_test,1)) data_test_new = temp_face*V #得到測試臉在特徵向量下的資料 data_test_new = array(data_test_new) # mat change to array data_train_new = array(data_train_new) true_num = 0 for i in xrange(num_test): testFace = data_test_new[i,:] diffMat = data_train_new - tile(testFace,(num_train,1)) sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis=1) sortedDistIndicies = sqDistances.argsort() indexMin = sortedDistIndicies[0] if train_face_number[indexMin] == test_face_number[i]: true_num += 1 accuracy = float(true_num)/num_test print ‘The classify accuracy is: %.2f%%‘%(accuracy * 100)
將這個指令碼存起來為PCA,使用也很簡單的直接調用裡面的facefind函數就可以了。
ORL人臉庫包含40個人的每個人10張人臉樣本,比如上述選擇每個人中隨機3張作為訓練集,其他的作為測試集,並且降維到30維,得到的準確率結果:
import PCAPCA.facefind()
--Getting data set---The classify accuracy is: 91.43%
每次啟動並執行結果都不一樣,因為每次的訓練集樣本隨機選的不同。當然你也可以修改每個人訓練集樣本的個數(肯定越多準確率越高),也可以修改降維數。這些在上述的matlab版本中都有做過實驗,可以看看那裡的對比實驗。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
python下PCA演算法與Face Service