Companion code for icafaces. This file contains 4 matlab functions: 1. cosFn.m Computes cosines between train and test vectors. 2. nnclassFn.m Performs nearest neighbor classification using cosines. 3. pcaFn.m Calculates principal components of a matrix. 4. pcabigfn.m Does PCA using the "smaller covariance matrix" trick for large images. <<<<>>> *********************************************************** *************************cosFn.m***************************** *********************************************************** <<<<>>> % function [S] = cosFn(mat1,mat2), % Computes the cosine (normalized dot product) between training vectors in % columns of mat1 and test vectors in columns of mat2. Outputs a matrix of % cosines (similarity matrix). function [S] = cosFn(mat1,mat2), denom = sum(mat1.^2,1)*sum(mat2'.^2,2); denom (find(denom==0)) = 0.00000000000000000000001; numer = mat1'*mat2; S = numer./denom; <<<<>>> *********************************************************** *************************nnClassFn.m***************************** *********************************************************** <<<<>>> %function [testPerf,rankmat,rank] = nnclassFn(train,test,trainClass,answer) % %Reads in training examples, test examples, class labels of training %examples, and correct class of test examples. Data are in columns of train %and test, and labels are column vectors. %Gets matrix of normalized dot products. Outputs nearest neighbor %classification of test examples and percent correct. %rankmat gives the top 30 matches for each test image. rank is a vector %containing the percent of times the correct match is in the top N matches. function [testPerf,rankmat,rank] = nnclassFn(train,test,trainClass,answer); numTest = size(test,2); numTrain = size(train,2); %Get distances to training examples %dists = eucDist(test,train); %Outputs a Ntest x Ntrain matrix of Euc dist dists=-1 * cosFn(test,train);%Outputs a Ntest x Ntrain matrix of cosines %sort the rows of dists to find the nearest training example: [Sdist,nearest] = sort(dists'); %cols of Sdist are distances in ascend order %1st row of nearest is index of 1st closest training example %Create vector with nearest example, and vector with class label. Nnbr = nearest(1,:); %First row of nearest contains NN %Nnbr = nearest(2,:); testClass = trainClass(Nnbr); correct = find( (testClass - answer == 0)); testPerf = size(correct,1) / size(answer,1) if(size(correct,2)>size(correct,1)) testPerf = size(correct,2) / size(answer,2) 'check vector orientation' end %get rank = %correct in top N: cumtestPerf=0; for i = 1:30 rankmat(:,i) = trainClass(nearest(i,:)'); correcti = find( (rankmat(:,i) - answer == 0)); cumtestPerf = cumtestPerf + size(correcti,1) / size(answer,1); rank(i) = cumtestPerf; end %For FERET test, want probeID (answer), then rank, then matched ID no., %then FA flag, then "matching score". This will be a matrix with: %probe rank match FAflag matching score %i 1 trainClass(nearest(i,:)) Sdist(:,i)>4.7 1./Sdist(:,i) %i 2 OR rankmat(i,:)' %i 3 %i 4 <<<<>>> *********************************************************** *************************pcaFn.m***************************** *********************************************************** <<<<>>> %function [U,R,E] = pcaFn(B); % %Principal components the normal way. Data in columns of B. %U is a matrix containing the principal component eigenvectors in % its columns. %R is a matrix containing the principal component representations in its % rows. Each row is the representation for the corresponding column % of B. %E is the vector of eigenvaules corresponding to the eigenvectors in U. function [U,R,E] = pcaFn(B); %Read data into columns of B; %B = datamat'; [N,P] = size(B); %********subtract mean mb=mean(B'); B=B-(ones(P,1)*mb)'; %********Find eigenvectors vi of BB' (NxN) [V,D] = eig (1/(P-1)*(B*B')); %********Sort eigenvectors eigvalvec = max(D); [seigvals, index] = sort(eigvalvec); % sort goes low to high Vsort = V(:,[fliplr(index)]); U=Vsort; length = sqrt (sum (U.^2)); U = U ./ (ones(N,1) * length); R = B'*U; E = fliplr(seigvals); <<<<>>> *********************************************************** *************************pcabigFn.m***************************** *********************************************************** <<<<>>> %function [U,R,E] = pcabigFn(B); %Compute PCA by calculating smaller covariance matrix and reconstructing %eigenvectors of large cov matrix by linear combinations of original data %given by the eigenvecs of the smaller cov matrix. %Data in Cols of B. Third version. % %***** justification % %B = N x P data matrix. N = dim of data (Data in Cols of B, zero mean) % P = #examples % N >> P % %Want eigenvectors ui of BB' (NxN) %Solution: %Find eigenvectors vi of B'B (PxP) %From def of eigenvector: B'Bvi = di vi ---> BB'Bvi = di Bvi %Eigenvecs of BB' are Bvi %------------------------------- %[V,D] = eig (B'B) %Eigenvecs are in cols of V. (Sorted cols) % %U = BV; Cols of U are Bvi (eigenvecs of lg cov mat.) (Gave unit length) %R = B'U; Rows of R are pcarep of each observation. %E = eigenvalues (eigenvals of small and large cov mats are equal) %***** function [U,R,E] = pcabigFn(B); %Read data into columns of B; %B = datamat'; [N,P] = size(B); %********subtract mean mb=mean(B'); B=B-(ones(P,1)*mb)'; %********Find eigenvectors vi of B'B (PxP) [V,D] = eig (1/(P-1)*(B'*B)); %scale factor gives eigvals correct %magnitude for large cov mat %(assuming sample cov) %(assuming sample cov) %********Sort eigenvectors eigvalvec = max(D); [seigvals, index] = sort(eigvalvec); % sort goes low to high Vsort = V(:,[fliplr(index)]); %********Reconstruct U = B*Vsort; % Cols of U are Bvi. (N-dim Eigvecs) %********Give eigvecs unit length. Improves classification. length = sqrt (sum (U.^2)); U = U ./ (ones(N,1) * length); R = B'*U; % Rows of R are pcarep of each image. E = fliplr(seigvals);