首页 > 代码库 > 人脸识别---基于深度学习和稀疏表达的人脸识别算法

人脸识别---基于深度学习和稀疏表达的人脸识别算法

  • 介绍
  • 基于深度学习和稀疏表达的人脸识别算法
    • 1 利用VGGFace提取人脸特征
    • 2 PCA对人脸特征进行降维
    • 3 稀疏表达的人脸匹配
  • Code

1 介绍

本文将介绍一种基于深度学习和稀疏表达的人脸识别算法。

首先。利用深度学习框架(VGGFace)提取人脸特征;其次,利用PCA对提取的特征进行降维;最后,利用稀疏表达分类实现特征匹配。我採用CMC曲线评价在AR数据库上的识别性能。最后我还提供了整个过程的code。


2 基于深度学习和稀疏表达的人脸识别算法

2.1 利用VGGFace提取人脸特征

以下介绍利用VGGFace对人脸特征进行提取。我们利用的数据库为AR数据库。数据库的图比例如以下:
技术分享
接下来我们利用VGGFace对人脸特征进行提取。

  • 我们利用Matconvent作为深度学习框架,Matconvent能够到http://www.vlfeat.org/matconvnet/上下载,我採用的1.0-beta19。也能够下载最新版本号。
  • VGGFace的deep model能够到http://www.robots.ox.ac.uk/~vgg/software/vgg_face/上进行下载,我採用的是Matconvnet的版本号。模型大概1.01G。
    如果VGGFace的模型为F<script type="math/tex" id="MathJax-Element-1295">F</script>,图片为x<script type="math/tex" id="MathJax-Element-1296">x</script>。那么提取的特征为y=F(x)<script type="math/tex" id="MathJax-Element-1297">y = F(x)</script>.

2.2 PCA对人脸特征进行降维

利用pca对数据降维,VGGFace提取出的特征为4096维。对提取的特征进行降维最后降到128维。

2.3 稀疏表达的人脸匹配

数据库一共同拥有C<script type="math/tex" id="MathJax-Element-1298">C</script>个人,每个人有k<script type="math/tex" id="MathJax-Element-1299">k</script>张图片,那么每个人的特征字典为Dc={fc1,fc2,,fck}<script type="math/tex" id="MathJax-Element-1300">D_c=\{f_{c_1}, f_{c_2}, \ldots, f_{c_k}\}</script>, 那么C<script type="math/tex" id="MathJax-Element-1301">C</script>个人就组成一个Gallery特征字典D={D1,D2,?,DC}<script type="math/tex" id="MathJax-Element-1302">D=\{D_1, D_2, \cdots, D_C\}</script>。给一定probe人脸x, 那么特征为y=F(x)<script type="math/tex" id="MathJax-Element-1303">y=F(x)</script>, 则稀疏表达能够有例如以下表达:

||x||1,Dx=y<script type="math/tex" id="MathJax-Element-1304">||x||_1, Dx=y</script>

当中x<script type="math/tex" id="MathJax-Element-1305">x</script>为稀疏编码。


最后我们能够利用稀疏表达分类器来识别这个probe人脸x<script type="math/tex" id="MathJax-Element-1306">x</script>:

mincrc(y)=||y?Dcδc(x)||22<script type="math/tex" id="MathJax-Element-1307">\min_cr_c(y)=||y-D_c\delta_c(x)||_2^{2}</script>

3 Code

function cnn_vgg_faces()
%CNN_VGG_FACES  Demonstrates how to use VGG-Face
clear all
clc
addpath PCA
run(fullfile(fileparts(mfilename(‘fullpath‘)),...
    ‘..‘, ‘matlab‘, ‘vl_setupnn.m‘)) ;
net = load(‘data/models/vgg-face.mat‘) ;
list = dir(‘../data/AR‘);
C = 100;
img_list = list(3:end);
index = [1, 10];
%% 建立基于VGGFace的Gallery字典
dictionary = [];
for i = 1:C
    disp(i)
    numEachGalImg(i) = 0;
    for j = 1:2
        im = imread(strcat(‘../data/AR/‘,img_list((i-1)*26+index(j)).name));
        im_ = single(im) ; % note: 255 range
        im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
        for k = 1:3
            im1_(:,:,k) = im_;
        end
        im2_ = bsxfun(@minus,im1_,net.meta.normalization.averageImage) ;
        res = vl_simplenn(net, im2_) ;
        feature_p(:,j) = res(36).x(:);
    end
    numEachGalImg(i) = numEachGalImg(i) + size(feature_p,2);
    dictionary = [dictionary feature_p];
end
%% PCA对特征进行降维
FaceContainer = double(dictionary‘);
[pcaFaces W meanVec] = fastPCA(FaceContainer,128);
X = pcaFaces;
[X,A0,B0] = scaling(X);
LFWparameter.mean = meanVec;
LFWparameter.A = A0;
LFWparameter.B = B0;
LFWparameter.V = W;
imfo = LFWparameter;
train_fea = (double(FaceContainer)-repmat(imfo.mean, size(FaceContainer,1), 1))*imfo.V;
dictionary = scaling(train_fea,1,imfo.A,imfo.B);
for i = 1:size(dictionary, 1)
    dictionary(i,:) = dictionary(i,:)/norm(dictionary(i,:));
end
dictionary = double(dictionary);
totalGalKeys = sum(numEachGalImg);
cumNumEachGalImg = [0; cumsum(numEachGalImg‘)];

%% 利用稀疏编码进行特征匹配
% sparse coding parameters
if ~exist(‘opt_choice‘, ‘var‘)
    opt_choice = 1;
end
num_bases = 128;
beta = 0.4;
batch_size = size(dictionary, 1);
num_iters = 5;
if opt_choice==1
    sparsity_func= ‘L1‘;
    epsilon = [];
elseif opt_choice==2
    sparsity_func= ‘epsL1‘;
    epsilon = 0.01;
end

Binit = [];

fname_save = sprintf(‘../results/sc_%s_b%d_beta%g_%s‘, sparsity_func, num_bases, beta, datestr(now, 30));

AtA = dictionary*dictionary‘;
for i = 1:C
    fprintf(‘%s  \n‘,num2str(i));
    tic
    im = imread(strcat(‘../data/AR/‘,img_list((i-1)*26+26).name));
    im_ = single(im) ; % note: 255 range
    im_ = imresize(im_, net.meta.normalization.imageSize(1:2)) ;
    for k = 1:3
        im1_(:,:,k) = im_;
    end
    im2_ = bsxfun(@minus,im1_,net.meta.normalization.averageImage) ;
    res = vl_simplenn(net, im2_) ;
    feature_p = res(36).x(:);
    feature_p  = (double(feature_p)‘-imfo.mean)*imfo.V;
    feature_p = scaling(feature_p,1,imfo.A,imfo.B);
    feature_p = feature_p/norm(feature_p, 2);
    [B S stat] = sparse_coding(AtA,0, dictionary‘, double(feature_p‘), num_bases, beta, sparsity_func, epsilon, num_iters, batch_size, fname_save, Binit);
    for m = 1:length(numEachGalImg)
        AA = S(cumNumEachGalImg(m)+1:cumNumEachGalImg(m+1),:);
        X1 = dictionary(cumNumEachGalImg(m)+1:cumNumEachGalImg(m+1),:);
        recovery = X1‘*AA;
        YY(m) = mean(sum((recovery‘-double(feature_p)).^2));
    end
    score(:,i) = YY;
    toc
end
accuracy = calrank(score1,1:1,‘ascend‘);
fprintf(‘rank-1:%d/%%\n‘,accuracy*100);

文中以
calrank能够计算得到CMC曲线:參见http://blog.csdn.net/hlx371240/article/details/53482752。
最后得到rank-1为82%。
整个代码见资源,因为vgg-face 太大,能够自己到vgg的官网下载,然后放到../matconvnet-1.0-beta19\examples\data\models中。

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

人脸识别---基于深度学习和稀疏表达的人脸识别算法