2345網(wǎng)址導(dǎo)航手機上網(wǎng)導(dǎo)航下載seo網(wǎng)絡(luò)推廣教程
前言
🔥 優(yōu)質(zhì)競賽項目系列,今天要分享的是
基于深度學(xué)習(xí)的人臉識別系統(tǒng)
該項目較為新穎,適合作為競賽課題方向,學(xué)長非常推薦!
🧿 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate
機器學(xué)習(xí)-人臉識別過程
基于傳統(tǒng)圖像處理和機器學(xué)習(xí)技術(shù)的人臉識別技術(shù),其中的流程都是一樣的。
機器學(xué)習(xí)-人臉識別系統(tǒng)都包括:
- 人臉檢測
- 人臉對其
- 人臉特征向量化
- 人臉識別
人臉檢測
人臉檢測用于確定人臉在圖像中的大小和位置,即解決“人臉在哪里”的問題,把真正的人臉區(qū)域從圖像中裁剪出來,便于后續(xù)的人臉特征分析和識別。下圖是對一張圖像的人臉檢測結(jié)果:
人臉對其
同一個人在不同的圖像序列中可能呈現(xiàn)出不同的姿態(tài)和表情,這種情況是不利于人臉識別的。
所以有必要將人臉圖像都變換到一個統(tǒng)一的角度和姿態(tài),這就是人臉對齊。
它的原理是找到人臉的若干個關(guān)鍵點(基準點,如眼角,鼻尖,嘴角等),然后利用這些對應(yīng)的關(guān)鍵點通過相似變換(Similarity
Transform,旋轉(zhuǎn)、縮放和平移)將人臉盡可能變換到標準人臉。
下圖是一個典型的人臉圖像對齊過程:
這幅圖就更加直觀了:
人臉特征向量化
這一步是將對齊后的人臉圖像,組成一個特征向量,該特征向量用于描述這張人臉。
但由于,一幅人臉照片往往由比較多的像素構(gòu)成,如果以每個像素作為1維特征,將得到一個維數(shù)非常高的特征向量, 計算將十分困難;而且這些像素之間通常具有相關(guān)性。
所以我們常常利用PCA技術(shù)對人臉描述向量進行降維處理,保留數(shù)據(jù)集中對方差貢獻最大的人臉特征來達到簡化數(shù)據(jù)集的目的
PCA人臉特征向量降維示例代碼:
?
#coding:utf-8
from numpy import *
from numpy import linalg as la
import cv2
import osdef loadImageSet(add):FaceMat = mat(zeros((15,98*116)))j =0for i in os.listdir(add):if i.split('.')[1] == 'normal':try:img = cv2.imread(add+i,0)except:print 'load %s failed'%iFaceMat[j,:] = mat(img).flatten()j += 1return FaceMatdef ReconginitionVector(selecthr = 0.8):# step1: load the face image data ,get the matrix consists of all imageFaceMat = loadImageSet('D:\python/face recongnition\YALE\YALE\unpadded/').T# step2: average the FaceMatavgImg = mean(FaceMat,1)# step3: calculate the difference of avgimg and all image data(FaceMat)diffTrain = FaceMat-avgImg#step4: calculate eigenvector of covariance matrix (because covariance matrix will cause memory error)eigvals,eigVects = linalg.eig(mat(diffTrain.T*diffTrain))eigSortIndex = argsort(-eigvals)for i in xrange(shape(FaceMat)[1]):if (eigvals[eigSortIndex[:i]]/eigvals.sum()).sum() >= selecthr:eigSortIndex = eigSortIndex[:i]breakcovVects = diffTrain * eigVects[:,eigSortIndex] # covVects is the eigenvector of covariance matrix# avgImg 是均值圖像,covVects是協(xié)方差矩陣的特征向量,diffTrain是偏差矩陣return avgImg,covVects,diffTraindef judgeFace(judgeImg,FaceVector,avgImg,diffTrain):diff = judgeImg.T - avgImgweiVec = FaceVector.T* diffres = 0resVal = inffor i in range(15):TrainVec = FaceVector.T*diffTrain[:,i]if (array(weiVec-TrainVec)**2).sum() < resVal:res = iresVal = (array(weiVec-TrainVec)**2).sum()return res+1if __name__ == '__main__':avgImg,FaceVector,diffTrain = ReconginitionVector(selecthr = 0.9)nameList = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15']characteristic = ['centerlight','glasses','happy','leftlight','noglasses','rightlight','sad','sleepy','surprised','wink']for c in characteristic:count = 0for i in range(len(nameList)):# 這里的loadname就是我們要識別的未知人臉圖,我們通過15張未知人臉找出的對應(yīng)訓(xùn)練人臉進行對比來求出正確率loadname = 'D:\python/face recongnition\YALE\YALE\unpadded\subject'+nameList[i]+'.'+c+'.pgm'judgeImg = cv2.imread(loadname,0)if judgeFace(mat(judgeImg).flatten(),FaceVector,avgImg,diffTrain) == int(nameList[i]):count += 1print 'accuracy of %s is %f'%(c, float(count)/len(nameList)) # 求出正確率
人臉識別
這一步的人臉識別,其實是對上一步人臉向量進行分類,使用各種分類算法。
比如:貝葉斯分類器,決策樹,SVM等機器學(xué)習(xí)方法。
從而達到識別人臉的目的。
這里分享一個svm訓(xùn)練的人臉識別模型:
?
from __future__ import print_functionfrom time import timeimport loggingimport matplotlib.pyplot as pltfrom sklearn.cross_validation import train_test_splitfrom sklearn.datasets import fetch_lfw_peoplefrom sklearn.grid_search import GridSearchCVfrom sklearn.metrics import classification_reportfrom sklearn.metrics import confusion_matrixfrom sklearn.decomposition import RandomizedPCAfrom sklearn.svm import SVCprint(__doc__)# Display progress logs on stdoutlogging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')################################################################################ Download the data, if not already on disk and load it as numpy arrayslfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)# introspect the images arrays to find the shapes (for plotting)n_samples, h, w = lfw_people.images.shape# for machine learning we use the 2 data directly (as relative pixel# positions info is ignored by this model)X = lfw_people.datan_features = X.shape[1]# the label to predict is the id of the persony = lfw_people.targettarget_names = lfw_people.target_namesn_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)################################################################################ Split into a training set and a test set using a stratified k fold# split into a training and testing setX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)################################################################################ Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled# dataset): unsupervised feature extraction / dimensionality reductionn_components = 80print("Extracting the top %d eigenfaces from %d faces"% (n_components, X_train.shape[0]))t0 = time()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))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.3fs" % (time() - t0))################################################################################ Train a SVM classification modelprint("Fitting the classifier to the training set")t0 = time()param_grid = {'C': [1,10, 100, 500, 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.3fs" % (time() - t0))print("Best estimator found by grid search:")print(clf.best_estimator_)print(clf.best_estimator_.n_support_)################################################################################ Quantitative evaluation of the model quality on the test setprint("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)))################################################################################ Qualitative evaluation of the predictions using matplotlibdef plot_gallery(images, titles, h, w, n_row=3, n_col=4):"""Helper function to plot a gallery of portraits"""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)# Show the feature faceplt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)plt.title(titles[i], size=12)plt.xticks(())plt.yticks(())# plot the result of the prediction on a portion of the test setdef 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\ntrue: %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)# plot the gallery of the most significative eigenfaceseigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]plot_gallery(eigenfaces, eigenface_titles, h, w)plt.show()
深度學(xué)習(xí)-人臉識別過程
不同于機器學(xué)習(xí)模型的人臉識別,深度學(xué)習(xí)將人臉特征向量化,以及人臉向量分類結(jié)合到了一起,通過神經(jīng)網(wǎng)絡(luò)算法一步到位。
深度學(xué)習(xí)-人臉識別系統(tǒng)都包括:
- 人臉檢測
- 人臉對其
- 人臉識別
人臉檢測
深度學(xué)習(xí)在圖像分類中的巨大成功后很快被用于人臉檢測的問題,起初解決該問題的思路大多是基于CNN網(wǎng)絡(luò)的尺度不變性,對圖片進行不同尺度的縮放,然后進行推理并直接對類別和位置信息進行預(yù)測。另外,由于對feature
map中的每一個點直接進行位置回歸,得到的人臉框精度比較低,因此有人提出了基于多階段分類器由粗到細的檢測策略檢測人臉,例如主要方法有Cascade CNN、
DenseBox和MTCNN等等。
MTCNN是一個多任務(wù)的方法,第一次將人臉區(qū)域檢測和人臉關(guān)鍵點檢測放在了一起,與Cascade
CNN一樣也是基于cascade的框架,但是整體思路更加的巧妙合理,MTCNN總體來說分為三個部分:PNet、RNet和ONet,網(wǎng)絡(luò)結(jié)構(gòu)如下圖所示。
人臉識別
人臉識別問題本質(zhì)是一個分類問題,即每一個人作為一類進行分類檢測,但實際應(yīng)用過程中會出現(xiàn)很多問題。第一,人臉類別很多,如果要識別一個城鎮(zhèn)的所有人,那么分類類別就將近十萬以上的類別,另外每一個人之間可獲得的標注樣本很少,會出現(xiàn)很多長尾數(shù)據(jù)。根據(jù)上述問題,要對傳統(tǒng)的CNN分類網(wǎng)絡(luò)進行修改。
我們知道深度卷積網(wǎng)絡(luò)雖然作為一種黑盒模型,但是能夠通過數(shù)據(jù)訓(xùn)練的方式去表征圖片或者物體的特征。因此人臉識別算法可以通過卷積網(wǎng)絡(luò)提取出大量的人臉特征向量,然后根據(jù)相似度判斷與底庫比較完成人臉的識別過程,因此算法網(wǎng)絡(luò)能不能對不同的人臉生成不同的特征,對同一人臉生成相似的特征,將是這類embedding任務(wù)的重點,也就是怎么樣能夠最大化類間距離以及最小化類內(nèi)距離。
Metric Larning
深度學(xué)習(xí)中最先應(yīng)用metric
learning思想之一的便是DeepID2了。其中DeepID2最主要的改進是同一個網(wǎng)絡(luò)同時訓(xùn)練verification和classification(有兩個監(jiān)督信號)。其中在verification
loss的特征層中引入了contrastive loss。
Contrastive
loss不僅考慮了相同類別的距離最小化,也同時考慮了不同類別的距離最大化,通過充分運用訓(xùn)練樣本的label信息提升人臉識別的準確性。因此,該loss函數(shù)本質(zhì)上使得同一個人的照片在特征空間距離足夠近,不同人在特征空間里相距足夠遠直到超過某個閾值。(聽起來和triplet
loss有點像)。
最后
🧿 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate