%% By hhh 2008-3-6
%% TITLE: PCA for the preprocessed images
%% Read the training data
clear all
close all
set(0,'RecursionLimit',100000);
% training = dir('..\..\..\data\aurora_data\arc_training');
% path = '..\..\..\data\aurora_data\arc_training';
% I try the program using face data first
% for MIT database
% path = 'E:\FaceDetection\MyData\MIT\Training';
% for Viola database----only here has difference! and LoadI.m one place different from MIT.
path = 'E:\FaceDetection\MyData\Viola\Training';
% Load images
[A,trainFNum,trainNonFNum,row,col] = LoadImgs(path);
trainingNum = trainFNum + trainNonFNum;
% obtain the diffirent images between the images and the average image
[A,FAImean] = CalDiffirentImg(A,trainingNum,row,col);
%% PCA for the training images
% (1) covariance matrix, S = A*A' (skip computing by using svd)
% (2) partial eigenvalue decomposition S = U'*E*V
% [U,E,V] = svd(A); % singular val decomp much faster------Out of memory!!!
CovOfDiffImg = A*A';
% according to Turk,calculate A'*A firstly
% in the condition of m<<n^2(m is faces number,n^2 is their size), then eigenVecs=A*U.
% But here, the images are only 19*19,then A is 361*1000,so m>n^2.
% So,directly A*A'.------hhh, 3.31,2008
[U,E,V] = svd(CovOfDiffImg); % singular val decomp much faster
eigVals = diag(E);
TotalEnergy = sum(eigVals)*0.98;
for i = size(eigVals,1):-1:1
if(sum(eigVals(1:i))<=TotalEnergy)
pcaNum = i + 1;
break;
end
end
% (3) get sorted eigenvalues (diag of E) and eigenvectors (U)
% try diag(E.^2) later!!!!!!
eigVecs = U(:,1:pcaNum); % these are eigenfaces --- not A*U'!! ----hhh
% show the eigenfaces
% titleName = 'EigenAuroras';
% titleName = 'EigenFaces';
% ShowSubplot(titleName,eigVecs,row,col);
%% Projection of test images onto base space
% the projected coefficients of the training set A.every column is 'pcaNum'
% coefficients to each image in the training set(each column of A).
WeightsOfTrainingSet = eigVecs'*A;
% see a example:
figure;imshow(reshape(uint8(eigVecs*WeightsOfTrainingSet(:,66)),row,col));
figure;imshow(reshape(uint8(eigVecs*WeightsOfTrainingSet(:,66)+FAImean),row,col));
a = uint8(eigVecs*WeightsOfTrainingSet(:,66)+FAImean);
W = U'*A;
figure;imshow(reshape(uint8(U*W(:,66)+FAImean),row,col));
aa = uint8(U*W(:,66)+FAImean);
%% Read the test data
% test = dir('..\..\..\data\aurora_data\arc_test');
% path = '..\..\..\data\aurora_data\arc_test';
% I try the program using face data first
% for MIT database
% path = 'E:\FaceDetection\MyData\MIT\Test';
% for Viola database
path = 'E:\FaceDetection\MyData\Viola\Test';
% Load images
[B,testFNum,testNonFNum,row,col] = LoadImgs(path);
% obtain the diffirent images between the test images and the previous
% average image of the training set A.
for j = 1:size(B,2)
B(:,j) = (B(:,j) - FAImean);
%figure;imshow(reshape(uint8(A(:,j)),m,n));
end
%% Projection of test images onto base space
% the projected coefficients of the test set B.every column is 'pcaNum'
% coefficients to each image in the test set(each column of B).
WeightsOfTestSet = eigVecs'*B;
% see a example:
figure;imshow(reshape(uint8(eigVecs*WeightsOfTestSet(:,66)),row,col));
figure;imshow(reshape(uint8(eigVecs*WeightsOfTestSet(:,66)+FAImean),row,col));
b = uint8(eigVecs*WeightsOfTestSet(:,66)+FAImean);
WB = U'*B;
figure;imshow(reshape(uint8(U*WB(:,66)+FAImean),row,col));
bb = uint8(U*WB(:,66)+FAImean);
%% Calculate the distances between the training set itself and
% the distances between the training set and test set.
%belong to different database
% 1~3 database
% TrainLableMatrix = [zeros(1,500),ones(1,500)];
% TestLableMatrix = [zeros(1,500),ones(1,500)];
% 4\5\6 database
% TrainLableMatrix = [zeros(1,1000),ones(1,1000)];
% TestLableMatrix = [zeros(1,1000),ones(1,1000)];
% 7 database
% TrainLableMatrix = [zeros(1,1500),ones(1,1000)];
% TestLableMatrix = [zeros(1,1500),ones(1,1000)];
% 8 database
% TrainLableMatrix = [zeros(1,1500),ones(1,1200)];
% TestLableMatrix = [zeros(1,1500),ones(1,1200)];
% general database!!!
TrainLableMatrix = [ones(1,trainFNum),zeros(1,trainNonFNum)];
TestLableMatrix = [ones(1,testFNum),zeros(1,testNonFNum)];
% Mr. Vassilis Athitsos said in his code readme.txt: It is strongly
% recommended that you randomize the order of your training objects before
% you creat these matrice.----TestTrainMatrix,TestLableMatrix aren't changed.
RandTrainLableMatrix = zeros(size(TrainLableMatrix));
RandWeightsOfTrainingSet = zeros(size(WeightsOfTrainingSet));
% generate 'trainingNum' random numbers
index = [];
CountNum=0;
% The following 7 lines is good for selecting 'trainingNum' random numbers too,but I use an other more simple way!!!
% while (trainingNum-CountNum)>500
% [index,c1] = GenRandArr(index,trainingNum-CountNum,trainingNum);
% CountNum = CountNum + c1;
% end
% for i = CountNum+1:trainingNum
% index(i) = GenRandNum(index,i,trainingNum);
% end
n = 3;
while trainingNum>CountNum
n = n + 1;
[index,CountNum] = GenRandArr(index,trainingNum*n,trainingNum);
end
for i = 1:trainingNum
RandTrainLableMatrix(i) = TrainLableMatrix(index(i));
RandWeightsOfTrainingSet(:,i) = WeightsOfTrainingSet(:,index(i));
end
TrainTrainMatrix = dists(RandWeightsOfTrainingSet,RandWeightsOfTrainingSet);
TestTrainMatrix = dists(WeightsOfTestSet,RandWeightsOfTrainingSet);
%% Write the two kinds of matrixs into traintrain_distances.bin and traintest_distances.bin
filename1 = 'E:\GaitManifoldLearning\Boostmap\boostmap_code\boostmap\data\bm_datasets\FaceData\traintrain_distances.bin';
result1 = write_float_matrix(TrainTrainMatrix, filename1);
filename2 = 'E:\GaitManifoldLearning\Boostmap\boostmap_code\boostmap\data\bm_datasets\FaceData\testtrain_distances.bin';
result2 = write_float_matrix(TestTrainMatrix, filename2);
% Write the train and test lables matrixs into training_lables.bin and
% test_labless.bin ----- 0 for nonface while 1 for face
filename3 = 'E:\GaitManifoldLearning\Boostmap\boostmap_code\boostmap\data\bm_datasets\FaceData\training_labels.bin';
result = write_float_matrix(RandTrainLableMatrix, filename3);
filename4 = 'E:\GaitManifoldLearning\Boostmap\boostmap_code\boostmap\data\bm_datasets\FaceData\test_labels.bin';
result = write_float_matrix(TestLableMatrix, filename4);
%% Turk's face recognition
% % pesi_database_mediati is a matrix with the averaged eigenface components of the images
% % present in database. Each class has its averaged eigenface.
% pesi_database=zeros(pcaNum, dbInfor.individualNum);
% numero_elementi_classe=zeros(dbInfor.individualNum,1);
% for ii=1:dbInfor.faceNum
% ingresso_database=double(dbInfor.data{ii,1});
% classe_database=dbInfor.data{ii,2};
% pesi_correnti=eigFaces'*(ingresso_database-media);
% pesi_database(:,classe_database)=pesi_database(:,classe_database)+pesi_correnti;
% numero_elementi_classe(classe_database)=numero_elementi_classe(classe_database)+1;
% end
% for ii=1:dbInfor.individualNum
% pesi_database_mediati(:,ii)=pesi_database(:,ii)/numero_elementi_classe(ii);
% end
%
% weight.individualNum = dbInfor.individualNum;
% weight.pcaNum = pcaNum;
% weight.Vtrue = eigFaces;
% weight.Dtrue = eigVals;
% weight.media = media;
% weight.meanFaces = pesi_database_mediati;
% weight.individualName= dbInfor.individualName;
评论0