This article usesmatlab UsePCA Complete dimensionality reduction of data, Reconstruction and face recognition.

Reference articles：http://blog.csdn.net/watkinsong/article/details/38536463
<http://blog.csdn.net/watkinsong/article/details/38536463>

In my eyesPCA：

The dimension of data is too high, It's time-consuming and laborious, So I was wondering if I could just deal with some dimensions, And the result is consistent with that of all dimensions. Dangdang,PCA It came out.. Simply speaking, It's a picture with2000 Characteristic dimensions, And it's actually only one of them100 dimension（ Even less）, It has a huge impact on the results.

eg: For the Emperor, Cabinet first aid> Two auxiliary> Three auxiliary> Four auxiliary>> Other unknown officials. So for the emperor, The proportion of effective governance strategies provided by the whole cabinet can be regarded as60%, The whole civil service class can be regarded as75%, Military attache class20%, rank and file5%. That is to say, although there are many ordinary people, But there are few solutions, So I think we can ignore their proposal selectively. thirdly, Military officers can be ignored, Civil service..

To summarize, it's, We only focus on the most influential feature dimensions, Give up the characteristic dimension of insufficient influence.
<http://blog.csdn.net/watkinsong/article/details/38536463>

PCA The thinking process is as follows：

1, Minus mean, Centralization

2, Calculate covariance matrix

3, Selecting eigenvalues and eigenvectors

4, Transform training set into vector space composed of eigenvectors to reduce dimensions

5, Transpose test set times eigenvector, Add the centralized mean value to complete the reconstruction

6
, Distinguish： Select a photo of each person for registration, Minus mean, Multiply by dimension reduction matrix（ I.e. eigenvector）, And record the values of the recordset in the reduced dimension matrix. Traverse picture library, And do the same with the photos. The person of the picture whose distance between the value of the picture in the reduced dimension matrix and the value of the record set is the minimum Euclidean, Classification of the picture.

Matlab The code is as follows：

%% Read the picture. clear ; close all; clc %m = 1680; % number of samples trainset =
zeros(10, 50 * 40); % Picture size 50 * 40 file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction\ data\AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folderbmp Image in format img_num =
length(img_path_list);% Total number of acquired images for i = 10: img_num % Take out the first ten photos as the training set, Top ten as tests
image_name = img_path_list(i).name;% Image name end               %% before training
PCA, do feature normalization       mu = mean(trainset);%mean Function to find
The average value of elements along different dimensions in an array.     trainset_norm = bsxfun(@minus, trainset, mu);% Training set minus average
sigma = std(trainset_norm); %std Calculate standard deviation     trainset_norm =
bsxfun(@rdivide, trainset_norm, sigma);  %trainset_norm Point division sigma( standard deviation)
%% we could save the mean face mu to take a look the mean face
imwrite(uint8(reshape(mu, 50, 40)),
'C:\Users\zyfls\Desktop\ML Various screenshots\5\ At sixes and sevensPCA\meanface.bmp');       fprintf('mean
face saved. paused\n');     %% Computational dimension reduction matrix     X = trainset; % just for convience
[m, n] = size(X);            U = zeros(n);     S = zeros(n);            Cov
= (1 / m) * X' * X;  % Calculate covariance matrix     [U, S, V] = svd(Cov);% singular value decomposition , Return a andX
Diagonal matrix of the same sizeS, Two orthogonal matricesU andV, And satisfy= U*S*V'. ifA bym×n front, beU bym×m front,V byn×n front. Singular valuesS
On the diagonal of, Nonnegative in descending order.     % UseSVD It can be used for non square arrayPCA Handle, The following notes can be used to deal with the matrix     E = diag(S);
contribution = cumsum(E)./sum(E);% Calculate contribution rate %     [U,D]=eig(Cov);
% Computation matrixR Eigenvector matrix ofV And eigenvalue matrixD, Eigenvalue from small to large %     U=(rot90(U))';      % The eigenvector matrixU Sort from large to small %
D=rot90(rot90(D));  % Sorting eigenvalue matrix from large to small %     E=diag(D);          % Convert eigenvalue matrix to eigenvalue vector
%     ratio=0; % Cumulative contribution rate %     for k=1:n %         r=E(k)/sum(E);   % The firstk Contribution rate of main components
%         ratio=ratio+r;  % Cumulative contribution rate %         if(ratio>=0.9)  % Take cumulative contribution rate greater than or equal to90% Principal components
%             break; %         end %     end     fprintf('compute cov
done.\n');     % Dimension reduction matrixU Eigenvectors in, In the dimension reduction of human face, Also known as feature face,  U Each eigenvector in is equivalent to a direction of the found dimension reduction space.
utilizeU You can map features to this space.       %% Show feature faces  U The first ten items     for i = 1:10         ef = U(:, i);
img = ef;           minVal = min(img);           img = img - minVal;
max_val = max(abs(img));           img = img / max_val;           img =
reshape(img, 50, 40);         imwrite(img,
strcat('C:\Users\zyfls\Desktop\ML Various screenshots\5\ At sixes and sevensPCA\','eigenface', int2str(i),
'.bmp'));       end              fprintf('eigen face saved, paused.\n');
pause;            %% dimension reduction       k = 100; % reduce to 100
dimension       test = zeros(10, 50 * 40);       file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction\ data\AR\AR\';% Image folder path       img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folderbmp Image in format     for i = 1:10  % Top ten test sets
image_name = img_path_list(i).name;% Image name         img =
img = double(img);           test(i, :) = img(:);       end
% test set need to do normalization       test = bsxfun(@minus, test, mu);
% reduction  Dimension reduction     Uk = U(:, 1:k);  % Take from1 reachdimsion Feature vector of as dimension reduction space     Z =
test * Uk;     fprintf('reduce done.\n');           %% Test set refactoring     %% for the
test set images, we only minus the mean face,       % so in the reconstruct
process, we need add the mean face back       Xp = Z * Uk';       % show
reconstructed face       for i = 1:10         face = Xp(i, :);         %face =
face .* sigma;         face = face + mu;         face = reshape((face), 50,
40);           imwrite(uint8(face),
strcat('C:\Users\zyfls\Desktop\ML Various screenshots\5\ At sixes and sevensPCA\','reconstructionface',
int2str(i), '.bmp'));         Face_re(i,:)=Xp(i,:)+mu;       end       e =
Face_re-test;     error(1,i)=norm(e);         %dispaly error rate
error_rate=error(1,i);       display(error_rate);    %1.9061e+04
% Reconstruction of training set, Because there are more than one training setsigma The matrix is multiplied here     %% for the train set reconstruction, we
minus the mean face and divide by standard deviation during the train       %
so in the reconstruction process, we need to multiby standard deviation first,
% and then add the mean face back       trainset_re = trainset_norm * Uk;
% reduction       trainset_re = trainset_re * Uk'; % reconstruction     for i =
11:25           train = trainset_re(i, :);           train = train .* sigma;
train = train + mu;         train = reshape(train, 50, 40);
imwrite(uint8(train), strcat('C:\Users\zyfls\Desktop\ML Various screenshots\5\ At sixes and sevensPCA\',
'reconstruction',int2str(i), 'train.bmp'));       end  The above code completes dimension reduction and reconstruction：

The average face obtained is shown on the right：

Characteristic faces：

Reconstructed image：

<http://blog.csdn.net/watkinsong/article/details/38536463>

The above parts have completed dimension reduction, Restructure.

Distinguish：

The identification code is as follows（ Including downsizing and reconstruction）：

%% Read the picture. clear ; close all; clc %m = 1680; % number of samples trainset =
zeros(10, 50 * 40); % Picture size 50 * 40 file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction\ data\AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folderbmp Image in format img_num =
length(img_path_list);% Total number of acquired images j=1; for i = 1: img_num % Take all the photos as a training set if(mod(i,14)
== 0)% Everyone's last one left for the test set continue; end image_name = img_path_list(i).name;% Image name %
name = image_name(1:3); % if strcmp(name,'001') img =
imread(strcat(file_path,image_name)); img = double(img); trainset(j, :) =
img(:); j=j+1; % end end %% before training PCA, do feature normalization mu =
mean(trainset);%mean Function to find The average value of elements along different dimensions in an array. trainset_norm = bsxfun(@minus,
trainset, mu);% Training set minus average sigma = std(trainset_norm); %std Calculate standard deviation trainset_norm =
bsxfun(@rdivide, trainset_norm, sigma); %trainset_norm Point division sigma( standard deviation) %% we
could save the mean face mu to take a look the mean face
imwrite(uint8(reshape(mu, 50, 40)),
'C:\Users\zyfls\Desktop\ML Various screenshots\5\Recognition\meanface.bmp'); %% Computational dimension reduction matrix X =
trainset; % just for convience [m, n] = size(X); U = zeros(n); S = zeros(n);
Cov = (1 / m) * X' * X; % Calculate covariance matrix [U, S, V] = svd(Cov);% singular value decomposition , Return a andcov
Diagonal matrix of the same sizeS, Two orthogonal matricesU andV, And satisfy= U*S*V'. ifA bym×n front, beU bym×m front,V byn×n front. Singular valuesS
On the diagonal of, Nonnegative in descending order. % UseSVD It can be used for non square arrayPCA Handle E = diag(S); contribution =
cumsum(E)./sum(E);% Calculate contribution rate fprintf('compute cov done.\n'); % Dimension reduction matrixU Eigenvectors in,
In the dimension reduction of human face, Also known as feature face, U Each eigenvector in is equivalent to a direction of the found dimension reduction space. utilizeU You can map features to this space. %% Show feature faces U The first ten items
for i = 1:10 ef = U(:, i); img = ef; minVal = min(img); img = img - minVal;
max_val = max(abs(img)); img = img / max_val; img = reshape(img, 50, 40);
imwrite(img, strcat('C:\Users\zyfls\Desktop\ML Various screenshots\5\Recognition\','eigenface',
int2str(i), '.bmp')); end fprintf('eigen face saved, paused.\n'); pause; j=1;
regis = zeros(120,50*40); for i = 1:14: img_num % Take the first photo of each group and make a registration record set image_name =
img_path_list(i).name;% Image name % name = image_name(1:3); % if strcmp(name,'001')
img = imread(strcat(file_path,image_name)); img = double(img); regis(j, :) =
img(:); j=j+1; % end end regis = bsxfun(@minus,regis,mu); Uk = U(:, 1:100);
% Take from1 reach100 Feature vector of as dimension reduction space Zregis = regis * Uk;% The value of record registration record set in dimension reduction matrix %% dimension reduction
k = 100; % reduce to 100 dimension test = zeros(1680, 50 * 40); file_path =
'C:\Users\zyfls\Desktop\ML\ Chapter V data dimensionality reduction\ data\AR\AR\';% Image folder path img_path_list =
dir(strcat(file_path,'*.bmp'));% Get all in this folderbmp Image in format success = 0; for i = 1:
img_num % Traverse each photo to compare it with the registration record, Classification image_name = img_path_list(i).name;% Image name img =
imread(strcat('C:\Users\zyfls\Desktop\ML Various screenshots\5\', int2str(i), '.bmp')); img =
double(img); test(i, :) = img(:); test(i,:) = test(i,:) - mu; Uk = U(:, 1:100);
% Take from1 reachdimsion Feature vector of as dimension reduction space Ztest = test * Uk;% The value of test set in reduced dimension matrix for j=1:120
mdist(j)=norm(Ztest(i,:)-Zregis(j,:));% Calculate distance from registration record end [C,I] =
min(mdist);% Return the minimum distance, Its location if(I<10) I = num2str(I); I = strcat('00',I); elseif
(I<100) I = num2str(I); I = strcat('0',I); else I = num2str(I); end name =
image_name(1:3);% Take the first three digits of the current picture to determine whether the classification is correct if strcmp(name,I) success = success + 1;
end end suc_rate = success/1680;
PS： The format of my picture library name is that the first three numbers represent different people, So this is the way to identify the correctness of classification.

That completes the whole thingPCA Dimension reduction, Reconstruction and recognition, Finally finished the big homework.

Next oneSR（ Sparse dictionary） Distinguish.

<http://blog.csdn.net/watkinsong/article/details/38536463>

<http://blog.csdn.net/watkinsong/article/details/38536463>

<http://blog.csdn.net/watkinsong/article/details/38536463>

30天阅读排行