%DEMO
clear
clc
close all
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STEP 1
%Load data and calculate the mean face
%load data
display('loading data...');
[test_ids,test_imgs,train_ids,train_imgs,nonface_imgs]=load_data();
%calculate the average face
display('calculate the average face...');
avg_face=getAvgFace(train_imgs);
%get the size of the original images (all datasets have images with the
%same dimensions)
img_size=[size(train_imgs,2),size(train_imgs,3)];
%get the number of samples in the training data
n=size(train_imgs,1);
%Too much figures..ask for closing figures of step1
choice = questdlg('would you like to close all figures of step1?', ...
'Step1', ...
'Yes','No','No');
% Handle response
switch choice
case 'Yes'
close all
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STEP 2
%get principle components W and the N latent coordinates
%resize data to be (height*width)xN matrix
train_imgs=(reshape(train_imgs,[size(train_imgs,1),size(train_imgs,2)*...
size(train_imgs,3)]))';
nonface_imgs=(reshape(nonface_imgs,[size(nonface_imgs,1),size(nonface_imgs,2)*...
size(nonface_imgs,3)]))';
test_imgs=(reshape(test_imgs,[size(test_imgs,1),size(test_imgs,2)*...
size(test_imgs,3)]))';
avg_face=avg_face(:); %resize the mean to be (height*width)x1 vector
display('calculate the principal components...');
[W,X,L]=PCA_(train_imgs,avg_face); %get the principle components
%get the bound of x axis in plots
m=length(L);
%Scree Plot
figure;
plot(L);
title('Scree Plot');
ylim([-200,max(L(:))])
xlim([-50,m])
xlabel('Index of Eigenvalues') % x-axis label
set(gca,'XTickLabel',num2str(get(gca,'XTick').')) %to avoid x10^k
ylabel('Eigenvalues') % y-axis label
set(gca,'YTickLabel',num2str(get(gca,'YTick').')) %to avoid x10^k
print('visualization\\ScreePlot','-dpng'); %save it
%Fraction of Variance (FVE)
FVE=cumsum(L(:))/sum(L(:)); %calc FVE
%plot FVE
figure;
plot(FVE);
title('Fraction of Variance Explained');
xlabel('c') % x-axis label
ylabel('FVE') % y-axis label
ylim([min(FVE(:)),1])
print('visualization\\FVE','-dpng'); %save it
%Plot first 10 principle components (Eigenvectors)
figure('units','normalized','outerposition',[0 0 1 1]) %full screen
for i=1:10
subplot(2,5,i);
imshow(reshape(W(:,i),img_size(1),img_size(2)),[]);
colormap(jet);
title(sprintf('Eigenvector %d',i));
end
print('visualization\\10EigenVectors','-dpng'); %save it
%reconstruct the data
%pick random image
num=round(rand*n-1)+1;
%reconstruct the original images using few number of basis vectors
figure('units','normalized','outerposition',[0 0 1 1]) %full screen
j=2;
subplot(2,5,1);
imshow(reshape(train_imgs(:,num),img_size(1),img_size(2)),[]);
title('original image');
for i=5:10:90
display(sprintf('reconstructing the data using only the first %d basis vectors...',i));
X=W(:,1:i)'*(train_imgs(:,num)-avg_face); %recalculate X
Y=(W(:,1:i)*X)+avg_face; %reconstruct the image using (i)th basis vectors
subplot(2,5,j); j=j+1;
imshow(reshape(Y,img_size(1),img_size(2)),[]);
title(sprintf('Using %d principal components',i));
end
print('visualization\\reconst_low','-dpng'); %save it
%find the smallest dimensionality such that at least 90% of the variance is
%explained
alpha=0.9;
indices=find(FVE>=alpha); %pick all c values (number of dimentions) that
%satisfy the condition >= alpha
index=indices(1); %get the first one
display(sprintf('The smallest dimensionality that explain at least 90%s of the variance=%d','%',index));
%Too much figures..ask for closing figures of step2
choice = questdlg('would you like to close all figures of step2?', ...
'Step2', ...
'Yes','No','No');
% Handle response
switch choice
case 'Yes'
close all
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STEP 3
%Reconstruction using low dimensionality data
%calculate the latent coordinates using only the first 115 principle
%components.
%we can compute the first c(th) principle components using:
%[W,X,L]=PCA_(train_imgs,avg_face,index); %get the first (index=value) principle components
%or..
W=W(:,1:index); %discard principle components starting from the basis vector after (index)
X=W'*(train_imgs-repmat(avg_face,[1,n])); %recalculate X
%reconstruct face images (training set)
display('reconstructing face images...');
%Y=sqrt(n)*(W*X)+repmat(avg_face,[1,n]);
Y=(W*X)+repmat(avg_face,[1,n]);
%reconstruct nonface images
display('reconstructing non-face images...');
%avg_nonface(:,:)=mean(nonface_imgs,2); %get mean of non face images
X_nonfaces=W'*(nonface_imgs-repmat(avg_face,[1,size(nonface_imgs,2)])); %recalculate X
Y_nonface=(W*X_nonfaces)+repmat(avg_face,[1,size(nonface_imgs,2)]); %reconstruct
%compute the absolute difference images of training face images
diff_face=abs(Y-train_imgs);
%compute the squared reconstruction error of training face images
error_face=(sum((Y-train_imgs).^2,1));
%compute the absolute difference images of non-face images
diff_nonface=abs(Y_nonface-nonface_imgs);
%compute the squared reconstruction error of non-face images
error_nonface=(sum((Y_nonface-nonface_imgs).^2,1));
%show 5 random samples from reconstructed faces and reconstructed non-faces
%images
%pick 5 reconstructed face images
for i=1:5
figure('units','normalized','outerposition',[0 0 1 1]) %full screen
num=round(rand*n-1)+1;
%original image
ax1 =subplot(1,3,1);
imshow(reshape(train_imgs(:,num),img_size(1),img_size(2)),[]);
title(sprintf('original image# %d',num));
colormap(ax1,gray);
%reconstructed image
ax2=subplot(1,3,2);
imshow(reshape(Y(:,num),img_size(1),img_size(2)),[]);
title('reconstructed image');
colormap(ax2,gray);
%abs error
ax3=subplot(1,3,3);
imshow(reshape(diff_face(:,num),img_size(1),img_size(2)),[]);
title('abs error');
colormap(ax3,jet);
axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off',...
'Visible','off','Units','normalized', 'clipping' , 'off');
%report squared reconstruction error
t=text(0.5, 1,sprintf('Squared Reconstruction Error: %f',...
error_face(num)),'HorizontalAlignment',...
'center','VerticalAlignment', 'top');
t.FontSize = 12;
%save it
print(fullfile('visualization',sprintf('reconst_face_%d',i)),'-dpng');
end
%pick 5 reconstructed non-face images
for i=1:5
figure('units','normalized','outerposition',[0 0 1 1]) %full screen
num=round(rand*size(nonface_imgs,2)-1)+1;
%original image
ax1=subplot(1,3,1);
imshow(reshape(nonface_imgs(:,num),img_size(1),img_size(2)),[]);
colormap gray %restore the gray colormap
title(sprintf('original image# %d',num));
colormap(ax1,gray)
%reconstructed image
ax2=subplot(1,3,2);
colormap(ax2,gray);
imshow(reshape(Y_nonface(:,num),img_size(1),img_size(2)),[]);
title('reconstructed image');
%abs error
ax3=subplot(1,3,3);
imshow(reshape(diff_nonface(:,num),img_size(1),img_size(2)),[]);
colormap(ax3,jet);
title('abs error');
axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off',...
'Visible','off','Units','normalized', 'clipping' , 'off');
%report squared reconstruction error
t=text(0.5, 1,sprintf('Squared Reconstruction Error: %f',...
error_nonface(num)),'HorizontalAlignment',...
'center','VerticalAlignment', 'top');
t.FontSize = 12;
%save it
print(fullfile('visualization',sprintf('reconst_nonface_%d',i)),'-dpng');
end
%picking a threshold value to distinguish between face and non-face images
T=(mean(error_face)+(max(error_face)-mean(error_face))/2+...
mean(error_nonface)-(mean(error_nonface)-mi
林当时
- 粉丝: 114
- 资源: 1万+
最新资源
- 技术资料分享多核处理器构架的高速JPEG解码算法很好的技术资料.zip
- 技术资料分享第24章 性能和资源占用很好的技术资料.zip
- 技术资料分享第23章 LCD驱动API函数很好的技术资料.zip
- 技术资料分享第22章 LCD驱动程序很好的技术资料.zip
- 技术资料分享第21章 高层次配置很好的技术资料.zip
- 技术资料分享第20章 底层配置很好的技术资料.zip
- 技术资料分享第19章 与时间相关的函数很好的技术资料.zip
- 技术资料分享第18章 输入设备很好的技术资料.zip
- 技术资料分享第17章 Shift-JIS支持很好的技术资料.zip
- 技术资料分享第16章 Unicode很好的技术资料.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈