PCA用于人脸识别

写这个程序是老师布置的作业。一个莫名其妙的机会选了一个莫名其妙的课,于是写了与自己关系不大的人工智能的人脸识别的程序。这里给自己记录一下,估计这个学习都要和这个方面的打交道了。

   

    Part 1:程序流程简介

   

    这个程序是典型的。在已有资源中使用一部分做训练集,找到一个合适的模型或者结论,然后用剩下的部分来测试自己的结论的正确度,进而一步步提高自己的算法效率或者正确性等。

 

    所以,在这次的程序中,前半部分是训练部分,中间有几段是画图部分,后面部分是测试部分。

 

    Part 2:数据库和PCA算法简介

   

    数据库使用的是Yale的人脸数据库。一共15组图片,每组图片里面有11张图片。在我的程序里面,我使用了每组里面的8张照片为训练集,剩下的3张为测试集。所以,一共是120张训练照片,45张测试照片。

 

    PCA算法步骤:

 

    1.Matrix X (input data)                                         N dimensional input space

       即原始矩阵

 

    2.Matrix QX (Covariance of X)                              QX = cov(X) = E[(x-m)(x-m)T]

       求出X的协方差矩阵QX

 

    3.Valuable λk(eigenvalue of QX)                        λ1≥λ2≥λ3。。。

     求出特征值,降序排列

 

   4.Vector Vk

     求出对应的特征向量

 

   5.Projection: V*X

     最后一步,在求出的向量基上面投影(降维)

 

    Part 3:具体代码

    

Matlab代码   收藏代码
  1. <strong>%**********************************************  
  2. % Aim : The first AI program homework  
  3. % Title : PCA for face recognition  
  4. % Author : GongWanlu & Hufei  
  5. % Version : 1.0 final  
  6. % Submit Time : 2011-04-07  
  7. %**********************************************  
  8.   
  9. % %%%%%%%%%%%%%%%%%%%%%%INITIAL  
  10. clear all  
  11. clc  
  12. close all  
  13.   
  14. % %%%%%%%%%%%%%%%%%%%%%%Some variables according to the Yale Face DB  
  15. Num_subject = 15;  
  16. Num_image = 11;  
  17. Train_num_image = 8;                    %for every subject we choose 8 to train  
  18. Test_num_image = 9;                     %choose the left 3 images to test  
  19.   
  20. % %%%%%%%%%%%%%%%%%%%%%%Load Data  
  21. Data = [];  
  22. for i=1:Num_subject  
  23.     for j=1:Train_num_image  
  24.         path = sprintf('FaceDB_yaleA/%03d/%02d.jpg',i,j);  
  25.         pic = imread (path);            %read one picture  
  26.           
  27.         %Make Data ,Add pic into Data  
  28.         pic_line = pic(1:147*137);      %The pic size is 147*137  
  29.                                         %pic_line is 1*N ,N=147*137. from up to  
  30.                                         %down,left to right.  
  31.                                         %Reshape 2D image to 1D image  
  32.                                         %vectors  
  33.         Data = [Data;double(pic_line)]; %add pic_line into Data  
  34.     end  
  35. end  
  36. % End of Load Data  
  37.   
  38. %%%%%%%%%%%%%%%%%%%%%%%Substract mean from Data and make covariance from centering Data  
  39. samplemean = mean(Data);                %mean pic 1*N  
  40.   
  41. for k = 1:(Num_subject * Train_num_image)  
  42.     xmean(k,:)=Data(k,:)-samplemean;    %Normalize  
  43. end                                     %xmean is M*N ,each line is one pic  
  44.                                         %data(mean data) be normalized  
  45. sigma = xmean *xmean';                  %M*M , here is 120*120  
  46. [V D]=eig(sigma);                       %calculate the eigenvalue&eigenvector  
  47.                                         %eigenvalue in D,and vectors in V  
  48.  D1=diag(D);                            %the eigenvalues  
  49.    
  50.  %%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating eigenvalues  
  51.  % At first : sort desc  
  52.  Dsort=flipud(D1);  
  53.  Vsort=fliplr(V);  
  54.    
  55.  %choose part eigenvalues  
  56.  Dsum = sum(Dsort);                     %sum of the eigenvalues,we only choose 80%  
  57.                                         %we have different ways to choose  
  58.                                         %eigenvalues we need,90%,or>1……  
  59. temp_sum = 0;  
  60. p = 0;  
  61. while(temp_sum/Dsum<0.8)  
  62.     p = p+1;  
  63.     temp_sum = sum(Dsort(1:p));  
  64. end  
  65. %End of sort part  
  66.   
  67.  %%%%%%%%%%%%%%%%%%%%%%%Train Step: get the coordinate system  
  68.  i=1;  
  69.  while(i<=p && Dsort(i)>0)  
  70.      face_base(:,i) = Dsort(i)^(-1/2) * xmean' * Vsort (:,i);  
  71.      i=i+1;  
  72.  end  
  73.  % Dsort(i)^(-1/2) used to normalize, make variance=1  
  74.  % face_base is N*p  
  75.  % xmean' * Vsort (:,i); is change small matrix to big matrix. CHACHENG(Chinese)  
  76.    
  77.  %next sentence is vary important is our train result  
  78.  allcoor = Data * face_base;  
  79.  %End of training  
  80.    
  81.  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Draw part  
  82.   %draw CDF  
  83.  x = Dsort (1:p);  
  84.  x = flipud (x);  
  85.  for i=1:p  
  86.      y(i) = 1/p*i;  
  87.  end  
  88.  figure,plot(x,y,'r*'),hold on,plot(x,y,'b');  
  89.  axis([0 inf 0 1]);  
  90.  title('CDF of selected values','fontsize',18);  
  91.    
  92.  %draw the face (only use the first value) to show the result  
  93.  for k=1  
  94.      temp = reshape(face_base(:,k),147,137);        %1D to 2D  
  95.  end  
  96.  figure,imshow(mat2gray(temp));                     %draw it  
  97.  title('one eigen face','fontsize',18);  
  98.   
  99. %draw the face (use 100 values) to show the result  
  100.  BigMap = [];  
  101.  for k=1:4  
  102.      map = [];  
  103.      for j=1:4  
  104.         temp = reshape(face_base(:,(k-1)*4+j),147,137);%1D to 2D  
  105.         map = cat(2,map,temp);   
  106.      end  
  107.      BigMap = cat (1,BigMap,map);  
  108.  end  
  109.  figure,imshow(mat2gray(BigMap));                     %draw it  
  110.  Title ('Result of Chosen EigenValues','fontsize',18);  
  111.  %End of drwing part  
  112.    
  113.  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Test Part  
  114.  Bingo = 0;    
  115.  for i=1:Num_subject  
  116.     for j=Test_num_image:Num_image                  %read the left 15*3 images  
  117.           
  118.         path = sprintf('FaceDB_yaleA/%03d/%02d.jpg',i,j);  
  119.         test_pic = imread(path);  
  120.   
  121.         test_pic_line = test_pic(1:147*137);  
  122.         test_pic_line = double(test_pic_line);  
  123.           
  124.         tcoor= test_pic_line * face_base;           %get the value  
  125.           
  126.         for k=1:Num_subject* Train_num_image  
  127.             mdist(k)=norm(tcoor-allcoor(k,:));      %norm() distence  
  128.         end;  
  129.           
  130.         %三阶近邻   
  131.         [dist,index2] = sort(mdist);  
  132.         class1=floor( (index2(1)-1)/Train_num_image)+1;  
  133.         class2=floor((index2(2)-1)/Train_num_image)+1;  
  134.         class3=floor((index2(3)-1)/Train_num_image)+1;  
  135.           
  136.         if class1~=class2 && class2~=class3   ID = class1;        
  137.         elseif (class1==class2)  ID = class2;    
  138.         elseif (class2==class3)   ID = class3;   
  139.         end;  
  140.           
  141.         if  ID==i  
  142.             Bingo=Bingo+1;  
  143.         end;  
  144.     end;  
  145. end;  
  146. accuracy = Bingo/45; %output the result  
  147. disp(accuracy);  
  148. % End of Test</strong> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值