1
votes

I extracted PCA features using:

function [mn,A1,A2,Eigenfaces] = pca(T,f1,nf1) 
m=mean(T,2),   %T is the whole training set
train=size(T,2);
A=[];
for i=1:train
    temp=double(T(:,i))-m;
    A=[A temp];
end

train=size(f1,2);    %f1 - Face 1 images from training set 'T'
A=[];
for i=1:train
    temp=double(f1(:,i))-m;
    A1=[A1 temp];
end


train=size(nf1,2);    %nf1 - Images other than face 1 from training set 'T'
A=[];
for i=1:train
    temp=double(nf1(:,i))-m;
    A2=[A2 temp];
end

L=A'*A;
[V D]=eig(L);
for i=1:size(V,2)
    if(D(i,i)>1)
       L_eig=[L_eig V(:,1)];
    end
end 
Eigenfaces=A*L_eig;
end  

Then i projected only the face 1(class +1) from training data as such :

Function 1

for i=1:15                       %number of images of face 1 in training set
    temp=Eigenfaces'*A1(:,i);
    proj_img1=[proj_img1 temp];
end

Then i projected rest of the faces(class -1) from training data as such :

Function 2

 for i=1:221              %number of images of faces other than face 1 in training set
      temp=Eigenfaces'*A2(:,i);
      proj_img2=[proj_img2 temp];
 end

Function 3 Then the input image vector was obtained using:

diff=double(inputimg)-mn;   %mn is the mean of training data
testfeaturevector=Eigenfaces'*diff;

I wrote the results of Function 1 and 2 in a CSV file with labels +1 and -1 respectively. I then used LIBSVM to obtain the accuracy when giving the true label, it returned 0% and when i tried to predict the label it was -1 instead of +1.

And the accuracy coming as 0% ?

Basically my model is not trained properly and i am failing to see the error.

Any suggestions will be greatly appreciated.

5
Just a note: You aren't using mn (mean along the second dimension) and then you go on to subtract the mean along the first dimension. Not sure what the intent is... - Falimond
@Falimond : Sorry, the mean in function 3 had to be "mn" - Sid
is [f1 nf1] composes your T (or some column permutations of T)? - lennon310
@lennon310: Yes lennon, [f1 nf1] composes T - Sid
Looks like you are working on this for some time, and I've no clue what's the problem based on what you said here. what's the size of your data? I would like to have a try with libsvm if you can upload all your stuffs somewhere (with the clear description of them included). Thanks - lennon310

5 Answers

1
votes

Use Eigenfaces as the training set, compose a label vector with 1 or -1s (if the ith column of Eigenfaces refers to 1, then the ith element in label is 1, otherwise it is -1) . And use Eigenfaces and label in svmtrain function.

1
votes

@lennon310:

for i=1:length(Eigenfaces)                   
    temp=Eigenfaces'*A(:,i);
    proj_imgs=[proj_imgs temp];
end
1
votes

@lennon310:

   diff=double(inputimg)-mn;   %mn is the mean of training data

   testfeaturevector=Eigenfaces'*diff;
0
votes

Frankly, your code is a mess.

One questionable part:

data = reshape(data, M*N,1);

Doesn't this make data a matrix with just 1 column? This does not make sense.

Look at this tutorial on eigenfaces. There is code and examples within it to show you what to do. See the related webpage here for more details. The Matlab/Octave code can be found here.

0
votes
@lennon310: 

    temp=double(testimg)-m;  %where 'm' is the mean of the training images
    L=temp'*temp;
    [V D]=eig(L);
    for i=1:size(V,2)
        if(D(i,i)>1)
           L_eig=[L_eig V(:,1)];
        end
    end 
    Eigenfaces=temp*L_eig;