source: proiecte/PPPP/ica/Arch1.m @ 138

Last change on this file since 138 was 138, checked in by (none), 14 years ago

PPPP - ica

File size: 2.7 KB
Line 
1% script Arch1.m
2% Finds ICA representation of train and test images under Architecture I,
3% described in Bartlett & Sejnowski (1997, 1998), and Bartlett, Movellan &
4% Sejnowski (2002):  In Architecture I, we load N principal component
5% eigenvectors into rows of x, and then run ICA on x.
6%
7% Put the aligned training images in the rows of C, one image per row. 
8% In the following examples, there are 500 images of aligned faces of size
9% 60x60 pixels, so C is 500x3600.
10%
11% You can use the following matlab code to create C:
12% markFeatures.m collects eye and mouth positions.
13% align_Faces.m crops, aligns, and scales the face images.
14% loadFaceMat.m loads the images into the rows of C.
15%
16% This script also calls the matrix of PCA eigenvectors organized in
17% the columns of V (3600x499), created by [V,R,E] = pcabigFn(C');
18%
19% The ICA representation will be in the rows of F (called B in Bartlett,
20% Movellan & Sejnowski, 2002):
21
22[V,R,E] = pcabigFn(C');
23%D = zeroMn(C')'; % D is 500x3600 and D = C-ones(500,1)*mean(C);
24%R = D*V;        % R is 500x499 and contains the PCA coefficients;
25
26% We choose to use the first 200 eigenvectors.
27% (If PCA generalizes better by dropping first few eigenvectors, ICA will too).
28
29%x = V(:,1:200)';               % x is 200x3600
30x = V(:,1:3)';          % x is 200x3600
31runica                          % calculates wz, w and uu. The matrix x gets
32                                % overwritten by a sphered version of x.
33%F = R(:,1:200) * inv(w*wz);    % F is 500x200 and each row contains the
34F = R(:,1:3) * inv(w*wz);       % F is 500x200 and each row contains the
35                                % ICA1 rep of an image
36
37% Representations of test images under architecture I:
38% Put original aligned test images in rows of Ctest.
39
40Dtest = zeroMn(Ctest')'; % For proper testing, subtract the mean of the
41                         % training images not the test images:
42                         % Dtest = Ctest-ones(500,1)*mean(C);
43Rtest = Dtest*V;
44%Ftest = Rtest(:,1:200) * inv(w*wz);
45Ftest = Rtest(:,1:3) * inv(w*wz);
46
47% Test nearest neighbor classification using cosine, not euclidean distance,
48% as similarity measure.
49%
50% First create label vectors. These are column vectors of integers. Lets
51% say our 500 training examples consisted of 500 different people. Then
52% trainClass = [1:500]';
53%
54% We also need the correct class labels of the test examples if we want to
55% compute percent correct. Lets say the test examples were two images each
56% of the first 10 individuals. Then
57% testClass = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10]';
58
59trainClass = [1:3]';
60testClass = [1]';
61
62%We now compute percent correct:
63train_ex = F';
64test_ex = Ftest';
65[pc,rankmat] = nnclassFn(train_ex,test_ex,trainClass,testClass);
66
67%pc is percent correct of first nearest neighbor.
68%rankmat gives the top 30 matches for each test image.
69
Note: See TracBrowser for help on using the repository browser.