please do the extra credit \%\% (1) Load training set images from a .mat file (2 points) clear;
close all; clc; % Detailed instructions: \% Load the training set of 2414 images contained in the
MATLAB data file \% training_set_faces.m into the MATLAB workspace: Each image % in the
training set is a 48px by 42px image of a face. The training set fa% are saved in the MAT file
training_set_faces.mat. When you load this data \% file into the MATLAB workspace, you will
see a 3D array called yalefaces \% where the entry yalefaces (:,:,1) is the first image of the
training set, % yalefaces (:,:,2) is the second image, etc. % \% Suggested MATLAB functions:
load \% !! Your code here !! Load the training images from the provided \% MAT file
training_set_faces.mat load training_set_faces.mat;
\%\% (2) Plot one training set image (5 points) % Detailed intructions: \% Plot one of the images
from the training set 3D array to demonstrate you've \% correctly imported the training set
images. % \% Suggested MATLAB functions: imshow \% PLOTTING WITH imshow: Use the
options 'DisplayRange', [], and \% 'InitialMagnification', 'fit' when using imshow so that the
plotted images % are easy to see. See imshow help documentation for syntax details and the %
in-class Eigenfaces activities more help. % !! Your code here !! In a new figure, plot one of the
images you just loaded \% into MATLAB. Include a descriptive figure title. (5 points) figure
imshow(yalefaces (:, : , 1), [10 250 , 'InitialMagnification ', ... 'fit', 'Interpolation', 'bilinear')
title('eigenface 1)
\% (3) Vectorize the images and combine into 2D array (8 points) % Detailed instructions: %
create a 2D array that contains all the vectorized images in the training % set. First, convert each
image into a vector, simply by concatenating the row % of pixels in the original image, resulting
in a single column with 4842=%2016 elements, just like the in-class activity. % Then combine
all 2414 training set image column vectors into % a single matrix T, where each column of the
matrix is an image. Each row % of this matrix T corresponds to a pixel location. The 2D array
must be % assigned to the variable T. The size of T will be 20162414. %% suggested MATLAB
functions: reshape (or using a for loop) % !! Your code here !! Vectorize training set images into
a 2D array called T % ( 8 points) [a,b, c] = size(yalefaces); T = reshape(yalefaces, [a*b, c]);
\% We need to be sure the data type of our T matrix is double for the rest % of the computations
in this code (mean, subtraction, eig, etc.) T= double (T);% do not modify the line % (4) Find the
mean face ( 5 points) % Detailed instructions: \% The mean face is found by taking the average
pixel value across all images % in the training set at each pixel location. So the mean face pixel
(2,3) is % the average of the pixel values from all the images at the training set for % that one
pixel (2,3). The mean face will be a column vector of size 20161. % Remember that the.
1. please do the extra credit %% (1) Load training set images from a .mat file (2 points) clear;
close all; clc; % Detailed instructions: % Load the training set of 2414 images contained in the
MATLAB data file % training_set_faces.m into the MATLAB workspace: Each image % in the
training set is a 48px by 42px image of a face. The training set fa% are saved in the MAT file
training_set_faces.mat. When you load this data % file into the MATLAB workspace, you will
see a 3D array called yalefaces % where the entry yalefaces (:,:,1) is the first image of the
training set, % yalefaces (:,:,2) is the second image, etc. % % Suggested MATLAB functions:
load % !! Your code here !! Load the training images from the provided % MAT file
training_set_faces.mat load training_set_faces.mat;
2. %% (2) Plot one training set image (5 points) % Detailed intructions: % Plot one of the images
from the training set 3D array to demonstrate you've % correctly imported the training set
images. % % Suggested MATLAB functions: imshow % PLOTTING WITH imshow: Use the
options 'DisplayRange', [], and % 'InitialMagnification', 'fit' when using imshow so that the
plotted images % are easy to see. See imshow help documentation for syntax details and the %
in-class Eigenfaces activities more help. % !! Your code here !! In a new figure, plot one of the
images you just loaded % into MATLAB. Include a descriptive figure title. (5 points) figure
imshow(yalefaces (:, : , 1), [10 250 , 'InitialMagnification ', ... 'fit', 'Interpolation', 'bilinear')
title('eigenface 1)
% (3) Vectorize the images and combine into 2D array (8 points) % Detailed instructions: %
create a 2D array that contains all the vectorized images in the training % set. First, convert each
image into a vector, simply by concatenating the row % of pixels in the original image, resulting
in a single column with 4842=%2016 elements, just like the in-class activity. % Then combine
all 2414 training set image column vectors into % a single matrix T, where each column of the
matrix is an image. Each row % of this matrix T corresponds to a pixel location. The 2D array
must be % assigned to the variable T. The size of T will be 20162414. %% suggested MATLAB
functions: reshape (or using a for loop) % !! Your code here !! Vectorize training set images into
a 2D array called T % ( 8 points) [a,b, c] = size(yalefaces); T = reshape(yalefaces, [a*b, c]);
% We need to be sure the data type of our T matrix is double for the rest % of the computations
in this code (mean, subtraction, eig, etc.) T= double (T);% do not modify the line % (4) Find the
mean face ( 5 points) % Detailed instructions: % The mean face is found by taking the average
pixel value across all images % in the training set at each pixel location. So the mean face pixel
(2,3) is % the average of the pixel values from all the images at the training set for % that one
pixel (2,3). The mean face will be a column vector of size 20161. % Remember that the pixel
locations correspond to the rows of T. % % Suggested MATLAB functions: mean % !! Your
code here !! Find the mean image of the training set (5 points) mu=mean(T,2);
%% (5) Plot the mean face (10 points) % Detailed instructions: % Plot the mean face. First
you need to reshape the mean face (a column % vector) back into a 4842 array. It should look
like a face :-) Now that we % are working with images in the double precision data type, the
pixel values % have been scaled down to the range - 1. As described earlier, use one of two %
options to expand the scale back to 0255 so the face is visible. I % suggest using imshow and
setting 'DisplayRange' to []. % % Suggested MATLAB functions: reshape, imshow % !! Your
code here !! In a new figure, plot the mean face. Include a % descriptive title for this figure. (10
3. points) mf= reshape (mu,[a,b]) figure imshow (mf, [0255], 'InitialMagnification',... 'fit',
'Interpolation', 'bilinear') title('Mean Face')
%% (6) Subtract off mean face from training set images (5 points) % Detailed instructions: %
Subtract off the mean face column vector from every image in the training % set (the columns
of T ). Call this shifted 2D array shifted_T. shifted_T % will be the same size as T, 20162414.
%% Suggested MATLAB functions: - or minus % !! Your code here !! Subtract of mean face
from every image in the % training set ( 5 points) shited_ T=minus(T,mu); %% (7) Calculate
covariance of shifted_T and obtain eigenfaces ( points) % MATLAB covariance: C=cov(A). If
A is a matrix whose columns represent % random variables and whose rows represent
observations, C is the covariance % matrix with the corresponding column variances along the
diagonal. % % For our application, the random variables are each pixel location and the %
observations are the 2414 training images. We have 2016 random variables and %2414
observations of each random variable. % % Why the covariance matrix? As Wikipedia says on
its Eigenfaces page: "Informally, Window
% Why the covariance matrix? As Wikipedia says on its Eigenfaces page: "Informally, %
eigenfaces can be considered a set of "standardized face ingredients", derived % from statistical
analysis of many pictures of faces." The statiscal analysis % we are performing is called
principal component analysis (PCA). Wikipedia again: % "PCA is mathematically defined as an
orthogonal linear transformation that transforms % the data to a new coordinate system such that
the greatest variance by some % projection of the data comes to lie on the first coordinate (called
the first % principal component), the second greatest variance on the second coordinate, % and
so on. And the covariance matrix contains these variances. Read more about % principal
component analysis if you're interested: %
https://en.wikipedia.org/wiki/Principal_component_analysis> % % Our random variables are
the pixel values, which are organized in rows. % So that means we need to take the transpose of
shifted_T when we compute the % covariance. % No code for you to write here. We did it for
you. % Covariance matrix of training set images S=cov( shifted_ T);% do not modify this line
%% (8) Calculate the eigenfaces and associated eigevalues (5 points) % Detailed instructions:
% The size of S size is 20162016. The covariance matrix entries represents % the joint
variability of two pixels; e.g. S(2000,40) represents the joint % variabiliy of the pixel at position
2000 in the vectorized 4842 px image % and the pixel at position 40 . %% obtain the eigenvalues
& eigenvectors of S. The eigenvectors in this % application of facial recognition are called
eigenfaces. The result will be % a diagonal matrix containing the eigenvectors and a 20162016
4. array containing % the corresponding 2016 eigenvectors in columns. % !! Your code here !!
Find the eigenfaces and associated eigevalues (5 points) [V,D]=eig(S);D=diag(D);
%% (9) Sort eigenvalues and their vectors in descending order (10 points) % Detailed
instructions: % Sort eigenvalues in descending order and then sort their corresponding %
eigenvectors in that same order. *EIGENVALUES AND EIGENVECTORS COME IN PAIRS
% AND MUST STAY TOGETHER, I.E. SORT IN THE SAME ORDER. * MATLAB does not
consistently % sort the eigenvalues in ascending order, so you need to write code below that %
can handle MATLAB sorting descending order. I suggest % first turning the eigenvalue
diagonal matrix outputted by the eig into % a vector. Then use sort with the 'descend' mode to
put the eigenvalues % into descending order (largest eigenvalue first). Output both the sorted
eigenvalues % and the indicies that tell you how they were sorted when you call sort. Use %
these indicies to sort the eigenvectors (eigenfaces) into the same order. We % need to keep the
eigenvalues and their corresponding eigenvectors together. % Try out the smaller examples in
the sort Help Documentation before % implementing on the very large matrices in this problem.
% % DO NOT USE fliplr OR flipud. Your answer will be WRONG. Trust us. Use % sort
specfied to be in 'descend' mode. % % From MATLAB help documentation:
% 'ascend' results in ascending order % 'descend' results in descending order % The result is in
Y which has the same shape and type as X. % [Y,I]=sort(X,DIM,MODE) also returns an index
matrix I. (USE THIS!!) % % Suggested MATLAB functions: diag, sort (DO NOT USE fliplr
like you see in % Wikipedia. IT DOES NOT WORK FOR OUR TRAINING SET!!!!) % Need
more help? Check out the 'food order' example (with code!) from % the class slides. % !! Your
code here !! Sort eigenvalues and eigenfaces from largest to smallest % (descending order) (10
points) ( -5 pts if you use fliplr. Do no use it. % Use sort as instructed above and shown in in-
class examples.) [ Dsort, ind ]=sort(D, 'descend ' ); Vsort =V(:, ind );
% (10) Plot the first 9 Eigenfaces (15 points) % Detailed instructions: % Plot the eigenfaces
associated with the largest 9 eigenvalues. You first need % to reshape the columns corresponding
to the first 9 eigenvalues into 4842% arrays for plotting. %% Suggested MATLAB functions:
reshape or for loop, imshow utilizing the 'DisplayRange' % option because the pixel values in the
eigenfaces are so small after converting % to double, sgtitle % p % Try plotting the 9 eigenfaces
in a 33 figure array using the function % subplot. Title the array of subplots with the function
sgtitle. % ! Your code here ! In a new figure or new figures, plot the first 9 % eigenfaces
associated with the largest 9 eigenvalues. Include descriptive % titles. (15 points)
5. % EXTRA CREDIT %% %% EXTRA CREDIT (up to 2 points) Find the number of
eigenfaces needed to represent 95% of the total variance % Detailed instructions: % How many
eigenfaces do we need to represent 95% percentage of the data? % BIG HINT: Use Wikipedia
as a resource here. This article contains code %% that you can use (i.e. copy-and-paste) % %
The number of principal components k is determined arbitrarily by setting %% a threshold
epsilon =0.95 on the total variance. % The total variance is v= lambda_1 + lambda_ 2++
lambda_n where %%n = number of eigenvalues (equal to the number pixels in each image). %
Your task is to find the smallest k that satisfies: % (lambda_1 1 lambda_2
2++1ambda_k)/v>0.95% ! ! Your code here !! Find the number of eigenfaces that represent 95%
of the % data in the training set. Use part of the MATLAB code in the Wikipedia % article above
and modify as needed. (10 points)
%% EXTRA CREDIT (up to 1 point) Only keep eigenfaces that contain 95% of the info %
Detailed instructions: % Only consider the eigenfaces that represent 95% of the data by reducing
the % size of the eigenvectors/eigenfaces matrix. Only inlude the eigenvectors that % you need
according to this threshold. The result will be a 2D array of size %2016k where k is found in the
previous section. % !! Your code here !! Only use eigenfaces that represent 95% of the data by
% reducing the size of the eigenfaces matrix. %% EXTRA CREDIT (up to 5 points) Find
weights of training images in the eigenfaces subspac % Detailed instructions: % Project the
training images onto the eigenfaces subspace by taking the % dot product of each eigenface
(eigenvector) with each image with the mean face % subtracted. The eigenfaces and mean-
subtracted training images are vectors % with 4842=2016 elements. The result of this dot
product a scalar % ( 11 array) representing how much of this input image is "in the direction" %
of an eigenface. Or in other words, the dot product is a measure of how similar % the input face
is to the particular eigenface.
% % The more similar the input image is to a particular eigenface, the larger % the contribution
from this eigenface to the reconstructed image when using the % eigenfaces as a basis. This is
similar to a Fourier coefficient and reconstructing % signals from Fourier coefficients and sines
and cosines (the basis functions % for Fourier series). % % Store all these projections for all the
training images into a 2D array. % The number of elements in each column will equal the
number of eigenfaces you % are using (k). There will be 2414 columns for the 2414 training set
images. % % Suggested MATLAB functions: for loop or matrix multiplication % !! Your code
here !! Project the training images onto the eigenfaces subspace. %% EXTRA CREDIT (up to 2
points) Reconstruct one training set image from eigenface weights % Detailed instructions: %
So now we have the coefficients (weights) to reconstruct all of the training % images from a
6. linear combination of the eigenfaces with the appropriate weights, % just like a Fourier series.
These coefficient vectors can be thought of as a
% images from a linear combination of the eigenfaces with the appropriate weights, % just like
a Fourier series. These coefficient vectors can be thought of as a % type of compressed
representation of the input image. Instead of 2016 numbers % to describe an image (grayscale
values at all the pixels), we only need %k<2016 numbers and the eigenfaces to describe any
image. % % Compare one original image from the yalefaces 3D array to it's reconstructed %
image using the weights you just found and the eigenfaces. A reconstructed image % is the sum
of the mean face ( 20161 column vector) and the k weights % (or coefficients) multiplied by
their corresponding eigenface. Just like a Fourier % Series! % % Plot both the original image
from the yalefaces 3D array and its reconstruction % from the weights and the eigenfaces. You
will need to reshape the vectorized % reconstructed image to a 2D array before plotting. % %
Suggested MATLAB functions: matrix multiplcation or for loop, reshape, % subplot, imshow %
!! Your code here !! Reconstruct one of the training set images from its % k weights. Visually
compare the original and reconstructed % images by plotting them side by side.
%% EXTRA CREDIT (up to 4 points) Test facial recognition using a face from the training %
Should be a perfect match! % Test the facial recognition ability of your code using one of the
images % from the training set. This image will have a similarity score of 1 and % should be a
perfect match to one of the images in the training % set ( itself). % !! Your code here !! Set the
input image variable (your choice as to what % variable to use) to one of the training images. %
Calculate similarity score of this test input image from training set images % Detailed
instructions: % Calculate the similarity score of a 'test input image' to each training set % image
by taking the dot product of the mean-subtracted input image with % each eigenface/eigenvector.
% % In general: First vectorize the input image. Then subtract off the mean % image vector
from the input image. Then take the dot product of this mean- % subtracted test image (resized
to a vector) with each eigenface. The % result will be a vector of a length equal to the number of
eigenfaces
% that you are using (k). % !! Your code here !! Vectorize the test image if not already done so,
% subtract off mean from test image if not already done so. Find the % mean-subtracted test
image's eigenfaces coefficients. % Plot the weights of the test input image on the eigenfaces
using a stem plot. % In other words, plot the entries of the vector you just found above. % %
Suggested MATLAB function: stem % !! Your code here !! Plot of eigenfaces coefficients for
the test input image % Assign a similarity score to the input image by comparing the
7. coefficients/weights % associated with each eigenface found for the input image and the training
set % images. To perform facial recognition, the similarity score is calculated between % an
input face image and each of the training images. The matched face is the % one with the highest
similarity, and the magnitude of the similarity score indicates
% the confidence of the match (with a unit value indicating an exact match). % % Use a
similarity score based in the inverse Euclidean distance defined % as %% similarity score with
respect to eigenface n=%1/(1+weigenface_n winput_img || 2) % w_eigenface_n is teh
weight/coefficient vector of eigenface n and % w_input_img is the weight/coefficient vector of
the test input image and % A ||2 is the L2-norm of A (also called Euclidean distance). %
https://en.wikipedia.org/wiki/Euclidean_distance % % Check out
https://thedeadbeef.wordpress.com/2010/12/02/eigenfaces-face-recognition-matlab/ % for an
example of using this similariy score in MATLAB. % % Suggested MATLAB function: norm,
for loop % !! Your code here !!. Calculate the similarity score of the input images % with
respect to each eigenface and store in a vector of length equal to % the number of training
images (2414).
% Find the image in the training set with the highest similarity score % Detailed instructions: %
The training set image that is the closest match to the input image will % will have the largest
similarity score. % For an input image that is one of the images in the training set, the max %
similarity score should be 1 , indicating a perfect match. %% suggested MATLAB function: max
% !! Your code here !! Find closest training set image to the input image % according to the
similarity score % Visual check of test input image facial recognition % Detailed instructions:
% Display the input image and the closest training set image to visually % check the accuracy of
your facial recognition code. For this test input % image that was an image from the test set, you
should see the same faces. %
% Suggested MATLAB functions: imshow, subplot % !! Your code here !! Visually check the
code's facial recognition ability % for your test input image that was from the training set %%
EXTRA CREDIT (up to 4 points) Test facial recognition using a non-human face % Next, test
the image of a mandrill baboon (mandrill.bmp). This is a face % but not a human face. So the
facial recognition should give a 'poor' % result. You will copy and paste a lot of code from the
previous % section because you will be completing many of the same tasks, just on a %
different image. % % First import the mandrill.bmp file into MATLAB. Convert the image to
the % double data type. Resize the image to be the same size as the images in the % training set
4842px. % % Suggested MATLAB functions: imread, im2double, imresize % !! Your code
8. here !! Load the mandrill.bmp image. Convert to double.
% Resize to training set image size. % calculate similarity of non-human input image to
training set images % Detailed instructions: % calculate the similarity of the non-human face
input image to each training % image by taking the dot product of the mean-subtracted input
image with % each eigenface/eigenvector. %% In general: First vectorize the input image. Then
subtract off the mean % image vector from the input image. Then take the dot product of this
mean- % subtracted test image (resized to a vector) with each eigenface. The % result will be a
vector of a length equal to the number of eigenfaces % that you are using (k). % !! Your code
here !! subtract off mean face from input image. Find the % mean-subtracted input image's
eigenfaces coefficients.
% Plot the weights of the input image on the eigenfaces using a stem plot. % In other words,
plot the entries of the vector you just found above. % % Suggtested MATLAB function: stem
% !! Your code here !! Plot of eigenfaces coefficients for the test input image % Assign a
similarity score to the input image by comparing the coefficients/weights % associated with each
eigenface found for the input image and the training set % images. Use a similarity score based
in the inverse Euclidean distance. % % Suggested MATLAB function: norm, for loop % !!
Your code here !!. Calculate the similarity score of the input images % with respect to each
eigenface and store in a vector of length equal to % the number of training images (2414).
% Find the image in the training set with the highest similarity score % Detailed instructions: %
The training set image that is the closest match to the input image will % will have the largest
similarity score. This is the training set image % that is the closest match to the input image. %%
suggested MATLAB function: max % !! Your code here !! Find closest training set image to the
input image % according to the similarity score % Visual check of non-human face input image
facial recognition % Detailed instructions: % Display the input image and the closest training set
image to visually % check the accuracy of your facial recognition code. %i % Suggested
MATLAB functions: imshow or imagesc with colormap set to gray % !! Your code here !!
Visually check the code's facial recognition ability % on the non-human face.