畢業(yè)設(shè)計 深度學(xué)習(xí)人臉識別系統(tǒng)
0 前言
?? 這兩年開始畢業(yè)設(shè)計和畢業(yè)答辯的要求和難度不斷提升,傳統(tǒng)的畢設(shè)題目缺少創(chuàng)新和亮點,往往達不到畢業(yè)答辯的要求,這兩年不斷有學(xué)弟學(xué)妹告訴學(xué)長自己做的項目系統(tǒng)達不到老師的要求。
為了大家能夠順利以及最少的精力通過畢設(shè),學(xué)長分享優(yōu)質(zhì)畢業(yè)設(shè)計項目,今天要分享的是
?? ?
??學(xué)長這里給一個題目綜合評分(每項滿分5分)
難度系數(shù):3分
工作量:3分
創(chuàng)新點:3分
畢設(shè)幫助,選題指導(dǎo),技術(shù)解答,歡迎打擾,見B站個人主頁
https://space.bilibili.com/33886978
1 機器學(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 os
def loadImageSet(add):
? ?FaceMat = mat(zeros((15,98*116)))
? ?j =0
? ?for i in os.listdir(add):
? ? ? ?if i.split('.')[1] == 'normal':
? ? ? ? ? ?try:
? ? ? ? ? ? ? ?img = CV2.imread(add+i,0)
? ? ? ? ? ?except:
? ? ? ? ? ? ? ?print 'load %s failed'%i
? ? ? ? ? ?FaceMat[j,:] = mat(img).flatten()
? ? ? ? ? ?j += 1
? ?return FaceMat
def ReconginitionVector(selecthr = 0.8):
? ?# step1: load the face image data ,get the matrix consists of all image
? ?FaceMat = loadImageSet('D:\python/face recongnition\YALE\YALE\unpadded/').T
? ?# step2: average the FaceMat
? ?avgImg = 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]
? ? ? ? ? ?break
? ?covVects = diffTrain * eigVects[:,eigSortIndex] # covVects is the eigenvector of covariance matrix
? ?# avgImg 是均值圖像,covVects是協(xié)方差矩陣的特征向量,diffTrain是偏差矩陣
? ?return avgImg,covVects,diffTrain
def judgeFace(judgeImg,FaceVector,avgImg,diffTrain):
? ?diff = judgeImg.T - avgImg
? ?weiVec = FaceVector.T* diff
? ?res = 0
? ?resVal = inf
? ?for i in range(15):
? ? ? ?TrainVec = FaceVector.T*diffTrain[:,i]
? ? ? ?if ?(array(weiVec-TrainVec)**2).sum() < resVal:
? ? ? ? ? ?res = ?i
? ? ? ? ? ?resVal = (array(weiVec-TrainVec)**2).sum()
? ?return res+1
if __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 = 0
? ? ? ?for 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 += 1
? ? ? ?print 'accuracy of %s is %f'%(c, float(count)/len(nameList)) ?# 求出正確率
人臉識別
這一步的人臉識別,其實是對上一步人臉向量進行分類,使用各種分類算法。
比如:貝葉斯分類器,決策樹,SVM等機器學(xué)習(xí)方法。
從而達到識別人臉的目的。
這里分享一個svm訓(xùn)練的人臉識別模型:
from __future__ import print_function
from time import time
import logging
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.datasets import fetch_lfw_people
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import RandomizedPCA
from sklearn.svm import SVC
print(__doc__)
# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
###############################################################################
# Download the data, if not already on disk and load it as numpy arrays
lfw_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.data
n_features = X.shape[1]
# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
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)
###############################################################################
# Split into a training set and a test set using a stratified k fold
# split into a training and testing set
X_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 reduction
n_components = 80
print("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 model
print("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 set
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)))
###############################################################################
# Qualitative evaluation of the predictions using matplotlib
def 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 face
? ? ? ?plt.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 set
def 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 eigenfaces
eigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces, eigenface_titles, h, w)
plt.show()

2 深度學(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有點像)。



3 最后
畢設(shè)幫助,選題指導(dǎo),技術(shù)解答,歡迎打擾,見B站個人主頁
https://space.bilibili.com/33886978