📄 runpcakernel.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use Borg Linux Cluster
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
source /tools/env/borg.env
bjobs
bsuba -Is /tools/matlab/matlab.650/bin/matlab -nodisplay
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Feret images and ground truth
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
truth = load('faGndTruth.dat');
%truth = load('fbGndTruth.dat');
[n d]=size(truth);
offs = 811;
[face]=showFeretFace(truth, offs);
figure;
for k=1:12
subplot(3,4,k); showFeretFace(truth, offs+k);
end
% extract eyes-mouth geometry constraint
reX = zeros(n,1); reY = zeros(n,1);
leX = zeros(n,1); leY = zeros(n,1);
moX = zeros(n,1); moY = zeros(n,1);
ntX = zeros(n,1); ntY = zeros(n,1);
d = zeros(n,1); a = zeros(n,1); b=zeros(n,1);
min_d = zeros(n,1); max_d=zeros(n,1);
reX = truth(:,2);reY = truth(:,3);
leX = truth(:,4);leY = truth(:,5);
moX = truth(:,6);moY = truth(:,7);
ntX = truth(:,8);ntY = truth(:,9);
axis ij; hold on;
m=1;
for k=1:n
if (reX(k) ~= -1)
plot(reX(k), reY(k), 'or');
plot(leX(k), leY(k), 'ob');
plot(ntX(k), ntY(k), '.k');
plot(moX(k), moY(k), '+m');
d(m)=norm([leX(k) leY(k)]-[reX(k) reY(k)], 2);
b(m)=norm([leX(k) leY(k)]-[moX(k) moY(k)], 2);
a(m)=norm([moX(k) moY(k)]-[reX(k) reY(k)], 2);
min_d(m) = min([d(m) a(m) b(m)]);
max_d(m) = max([d(m) a(m) b(m)]);
m=m+1;
end
end
dr = max_d./min_d;
% geometry
figure; hold on;
plot(a/d, b/d, '.b');
plot(min_d, max_d, '.');
ylabel('\fontsize{12} b/d');
xlabel('\fontsize{12} a/d');
title('\fontsize{12} eyes-mouth distance/scale likelihood ');
% scale
figure;
subplot(2,2,1); hold on;
title('min:max d, scale=1'); plot(min_d, max_d, '.');
subplot(2,2,2); hold on;title('min:max d, scale=0.8');plot(0.8*min_d, 0.8*max_d, '.');
subplot(2,2,3); hold on;title('min:max d, scale=0.8^2'); plot(0.64*min_d, 0.64*max_d, '.');
subplot(2,2,4); hold on;title('min:max d, scale=0.8^3');plot(0.64*0.8*min_d, 0.64*0.8*max_d, '.');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PCA on facials
% (1) creat face icons: 12x16
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%truth = load('faGndTruth.dat');
truth = load('fbGndTruth.dat');
n = length(truth(:,1));
W = 12;
H = 16;
m=1;
offs=0;
for k=1:n
face = showFeretFace(truth, k);
[h w] = size(face);
if h ~=0 & w ~=0
fht(m) = h; fwid(m) = w; hwr(m) = h/w;
[icon vec]=scaleImage(face, W, H);
%figure(1); subplot(4,5,k); colormap('gray'); imagesc(icon);
%figure(2); subplot(4,5,k); imshow(face, 256);
faceIcons(:,m)=vec';
fprintf('\n >>k=%d,m=%d>> ', k, m);
m=m+1;
end
end
%save 'fa-icons-w12h16-3d.dat' faceIcons -ascii;
save 'fb-icons-w12h16-3d.dat' faceIcons -ascii;
W=20;H=30;
truth = load('fbGndTruth.dat');
[faces]=getFaceIcons(truth, W, H);
truth = load('faGndTruth.dat');
[faFaces]=getFaceIcons(truth, W, H);
save 'faFacesW20H30.dat' faFaces -ascii;
[T4, lat4]=pcaAnalysis(faFaces, 6);
save 'pcaFaceW20H30.dat' T4 -ascii;
uFaces =[];
uFaces = faFaces(1:300,:);
lFaces = faFaces(301:600,:);
figure; [T5, lat5]=pcaAnalysis(uFaces, 8);
save 'pcaUpperFaceW20H30.dat' T5 -ascii;
nFaces1 = randomSubImages('1839.jpg', 30, W, H);
nFaces2 = randomSubImages('1873.jpg', 30, W, H);
nFaces = double([nFaces1, nFaces2]);
[d n]=size(faFaces);
% plot upper face on dim 4 and 5.
figure; hold on; d1=1; d2=2;
title('\fontsize{12} upper-face projection 1-2');
for k=1:n
prj = T5*uFaces(:,k);
plot(prj(d1), prj(d2), '+r');
end
t = 20
for k=1:1800
prj = T5*nFaces(t:t+300-1,k);
plot(prj(d1), prj(d2), '.k');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PCA on facials
% (2) covar analysis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
first_use = 'n';
faIcons = load('fa-icons-w12h16-3d.dat');
%fbIcons = load('fb-icons-w12h16-3d.dat');
% get non face samples
if first_use == 'y'
jpgPath = 'd:/data/Feret/jpeg/nonface/';
jpgs = '1839';h=16; w=12; m=30;
fname = sprintf('%s%s.jpg', jpgPath, jpgs);
img = imread(fname, 'jpg');
nfaces = getNonFaceImage(img, m, w, h);
nface = double(nfaces);
jpgs = '1873';h=16; w=12; m=30;
fname = sprintf('%s%s.jpg', jpgPath, jpgs);
img = imread(fname, 'jpg');
nfaces2 = getNonFaceImage(img, m, w, h);
nface2 = double(nfaces);
% total 1800 nonfaces
nface(:,901:1800)=nface2;
save 'nonFaceW12H16.dat' nface -ascii;
else
nface = load('nonFaceW12H16.dat');
end
upFace = faIcons(1:96,:);
loFace = faIcons(97:192,:);
nUpFace = nface(1:96,:);
nLoFace = nface(97:192,:);
if first_use == 'y'
[T1, lat1]=pcaAnalysis(faIcons, 12);
save 'pcaFaceW12H16.dat' T1 -ascii;
save 'latFaceW12H16.dat' lat1 -ascii;
figure; [T2, lat2]=pcaAnalysis(upFace, 8);
save 'pcaUpperFaceW12H16.dat' T2 -ascii;
save 'latUpperFaceW12H16.dat' lat2 -ascii;
figure; [T3, lat3]=pcaAnalysis(loFace, 8);
save 'pcaLowerFaceW12H16.dat' T3 -ascii;
save 'latLowerFaceW12H16.dat' lat3 -ascii;
else
T1 = load('pcaFaceW12H16.dat');
T2 = load('pcaUpperFaceW12H16.dat');
T3 = load('pcaLowerFaceW12H16.dat');
end
[d n] = size(faIcons);
d1=1; d2=2;
figure; hold on; axis xy;
title('\fontsize{12} full-face projection 1-2');
for k=1:n
prj = T1*faIcons(:,k); plot(prj(d1), prj(d2), '+r');
end
for k=1:n
prj = T1*nface(:,k); plot(prj(d1), prj(d2), '.b');
end
% non-linear mapping
prjFace = T1*faIcons;
prjNonFace = T1*nface;
meanPrjFace = mean(prjFace')';
for k=1:n
prjFace(:,k) = prjFace(:,k) - meanPrjFace;
prjNonFace(:,k) = prjNonFace(:,k) - meanPrjFace;
end
% non-linear mapping
x = prjFace(1:4,:);
y = quadraticMap(x);
ynf = quadraticMap(prjNonFace(1:4,:));
[Tnl, latnl]=pcaAnalysis(y, 6);
d1=1; d2=3;
figure; hold on; axis xy;
title('\fontsize{12} full-face non-linear projection 1-2');
for k=1:n
prj = Tnl*y(:,k); plot(prj(d1), prj(d2), '+r');
end
for k=1:n
prj = Tnl*ynf(:,k); plot(prj(d1), prj(d2), '.b');
end
% plot upper face on dim 4 and 5.
figure; hold on; d1=1; d2=2;
title('\fontsize{12} upper-face projection 1-2');
for k=1:n
prj = T2*upFace(:,k);
plot(prj(d1), prj(d2), '+r');
end
for k=1:n
prj = T2*nLoFace(:,k);
plot(prj(d1), prj(d2), '.k');
end
% plot lower face on dim 4 and 5.
figure;
hold on;d1=1; d2=2;
for k=1:n
prj = T3*loFace(:,k);
plot(prj(5), prj(6), '+r');
end
for k=1:n
prj = T3*nLoFace(:,k);
plot(prj(d1), prj(d2), '.k');
end
title('\fontsize{12} Lower Face Distribution on 1-2');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PCA on facials
% (3) non-face data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
jpgs =['1839', '1842', '1873', '1875'];
jpgPath = 'd:/data/Feret/jpeg/nonface/';
jpgs = '1875';
n = 30; h=24; w=16;
fname = sprintf('%s%s.jpg', jpgPath, jpgs);
img = imread(fname, 'jpg');
imshow(img, 256);
nfaces2 = getNonFaceImage(img, 30, w, h);
for j=1:8
figure(j);
for k=1:9
subplot(3,3,k); loadATTFace(j,k);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Kernel PCA
% 12-11-2003.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
first_use = 'n';
% build face icons
if first_use == 'y'
W = 12; H = 16; faces=[];
truth = load('faGndTruth.dat');
[faces]=getFaceIcons(truth, W, H);
save 'faFacesW12H16.dat' faces -ascii;
end
% get non-face icons
if first_use == 'y'
W=12; H=16;
nFaces1 = randomSubImages('1839.jpg', 30, W, H);
nFaces2 = randomSubImages('1873.jpg', 30, W, H);
nFaces = double([nFaces1, nFaces2]);
save 'nonFacesW12H16.dat' nFaces -ascii;
end
% PCA
if first_use == 'y'
[T1, lat1, mf]=pcaAnalysis(faces, 12);
save 'pcaFaceW12H16.dat' T1 -ascii;
save 'latFaceW12H16.dat' lat1 -ascii;
figure; [T2, lat2, muf]=pcaAnalysis(upFaces, 8);
save 'pcaUpperFaceW12H16.dat' T2 -ascii;
save 'latUpperFaceW12H16.dat' lat2 -ascii;
figure; [T3, lat3, mlf]=pcaAnalysis(loFaces, 8);
save 'pcaLowerFaceW12H16.dat' T3 -ascii;
save 'latLowerFaceW12H16.dat' lat3 -ascii;
end
% load data
clear;
W=12; H=16;
faces = load('faFacesW12H16.dat');
[d n]=size(faces);
upFaces=faces(1:d/2,1:n);loFaces=faces(1+d/2:192,1:n);
mFace = mean(faces')'; mUpFace = mean(upFaces')'; mLoFace = mean(loFaces')';
nFaces = load('nonFacesW12H16.dat');
% load PCA
T1 = load('pcaFaceW12H16.dat');
T2 = load('pcaUpperFaceW12H16.dat');
T3 = load('pcaLowerFaceW12H16.dat');
mT1Face = T1*mFace; mT2Face = T2*mUpFace; mT3Face = T3*mLoFace;
t1Faces= T1*faces; nT1Faces=T1*nFaces;
t2Faces= T2*upFaces; nT2Faces=T2*nFaces(1:96,:);
t3Faces= T3*loFaces; nT3Faces=T3*nFaces(97:192,:);
% remove mean
for j=1:length(faces(1,:))
t1Faces(:,j)=t1Faces(:,j) - mT1Face;
t2Faces(:,j)=t2Faces(:,j) - mT2Face;
t3Faces(:,j)=t3Faces(:,j) - mT3Face;
end
% remove mean
for j=1:length(nFaces(1,:))
nT1Faces(:,j)= nT1Faces(:,j)- mT1Face;
nT2Faces(:,j)= nT2Faces(:,j)- mT2Face;
nT3Faces(:,j)= nT3Faces(:,j)- mT3Face;
end
figure; d1=1; d2=2;
% plot whole face on dim 1 and 2.
for k=1:6
subplot(3,2,k); hold on;
plot(nT1Faces(2*k-1,:), nT1Faces(2*k,:), '.b');
plot(t1Faces(2*k-1,:), t1Faces(2*k,:), '+r');
str=sprintf('%s%d-%d', '\fontsize{12}',2*k-1, 2*k); xlabel(str);
end
title('\fontsize{12} Face Distribution on PCA');
figure;
d1=5; d2=9; hold on;
plot(nT1Faces(d1,:), nT1Faces(d2,:), '.b');
plot(t1Faces(d1,:), t1Faces(d2,:), '+r');
xlabel('\fontsize{12} Face Distribution on PCA 5-9');
% plot upper face on dim 1 and 2.
figure;
for k=1:4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -