[94] | 1 | % script Arch2.m |
---|
| 2 | % Finds ICA representation of train and test images under Architecture II, |
---|
| 3 | % described in Bartlett & Sejnowski (1997, 1998), and Bartlett, Movellan & |
---|
| 4 | % Sejnowski (2002): In Architecture II, we load N principal component coefficients |
---|
| 5 | % into rows of x, and then run ICA on x. |
---|
| 6 | % |
---|
| 7 | % Put 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 F (called U in Bartlett, Movellan & |
---|
| 20 | % 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 | x = R(:,1:200)'; % x is 200x500; |
---|
| 27 | runica % calculates w, wz, and uu. The matrix x gets overwritten |
---|
| 28 | % by a sphered version of x. |
---|
| 29 | F = uu'; % F is 500x200 and each row contains the ICA2 rep of 1 image. |
---|
| 30 | % F = w * wz * zeroMn(R(:,1:200)')'; is the same thing. |
---|
| 31 | |
---|
| 32 | % Representations of test images under architecture II |
---|
| 33 | % Put original aligned test images in rows of Ctest: |
---|
| 34 | |
---|
| 35 | Dtest = zeroMn(Ctest')'; % For proper testing, subtract the mean of the |
---|
| 36 | % training images not the test images: |
---|
| 37 | % Dtest = Ctest-ones(500,1)*mean(C); |
---|
| 38 | Rtest = Dtest*V; |
---|
| 39 | Ftest = w * wz * zeroMn(Rtest(:,1:200)')'; |
---|
| 40 | |
---|
| 41 | % Test nearest neighbor classification using cosine, not euclidean distance, |
---|
| 42 | % as similarity measure. |
---|
| 43 | % |
---|
| 44 | % First create label vectors. These are column vectors of integers. Lets |
---|
| 45 | % say our 500 training examples consisted of 500 different people. Then |
---|
| 46 | % trainClass = [1:500]'; |
---|
| 47 | % |
---|
| 48 | % We also need the correct class labels of the test examples if we want to |
---|
| 49 | % compute percent correct. Lets say the test examples were two images each |
---|
| 50 | % of the first 10 individuals. Then |
---|
| 51 | % testClass = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10]'; |
---|
| 52 | |
---|
| 53 | %We now compute percent correct: |
---|
| 54 | train_ex = F'; |
---|
| 55 | test_ex = Ftest'; |
---|
| 56 | [pc,rankmat] = nnclassFn(train_ex,test_ex,trainClass,testClass); |
---|
| 57 | |
---|
| 58 | %pc is percent correct of first nearest neighbor. |
---|
| 59 | %rankmat gives the top 30 matches for each test image. |
---|
| 60 | |
---|