Hey, I got something for us to implement the task given below. Hi, I got a task for you to implement. Description: In this task , you will use the dataset Face.mat to classify
emotions, age, and gender of the subjects. This dataset includes 12 images of 6 different subjects. For
each subjects we have 2 samples of 6 different emotions. Use one of each samples in your training
dataset and the other one in your testing dataset. This way you would have 36 images in the training
dataset and 36 images in the testing dataset.
Goal: To classify the gender (2 classes: M, F), emotions (6 classes: angry, disgust, neutral, happy, sad,
surprised) and age (3 classes: Young, Mid age, Old). Using a linear classifier. You should label your
training and testing data for each of the classification problem separately.
Classifier: Use the linear classifier
Features: Projection of the image on each of the eigenfaces. For this purpose, you need to calculate
PCA of your training data. Your kth feature will be the projection (dot product) of the image on the
kth eigen vector (PCA direction) also known as eigenface.
Feature Selection: You will extract 36 Features as there are 36 images (and eigen vectors) in your
training data. Use the “sequentialfs” command in MATLAB to select the top 6 features using the
sequential forward search algorithm. The whos command on ‘Face.mat’ gives the following information about the dataset:
Variable Size Data Type Bytes
II 100820x72 double 58072320
m 1x72 double 576 .            Here is the code that My professor implemented: clc; close all; clear

%% This code is written by Prof.ET Esfahani for instruction of MAE502
% course in Spring 2021. This code should not be shared with anyone outside
% of the class.

%% Loaiding the dataset, 6 subjects, 12 images per subject
% II: is the matrix containing all 72 images, each row is one image in form
% of a vector with zero mean
% m: is a vector whose kth compomemnt is the mean value of kth image in II
load(‘Face.mat’)
cnt = 0;
mx = 355; my = 284; % original image size
for s=1:6
figure(s)
for i=1:12
cnt = cnt+1;
I = reshape(II(:,cnt),mx,my);
subplot(3,4,i)
imagesc(I); axis off; axis equal; colormap gray
end
end


%% Calculating the PCA
[A , B] = pca(II); % B is the new vectors (dimentions)

% ploting the eigenfaces
figure
title(‘First 12 Eigen Faces’)

for j=1:12
I = reshape(B(:,j),mx,my);
subplot(3,4,j)
imagesc(I); axis off; axis equal; colormap gray
end
%% Reconstructing an image
I_rec = zeros(mx,my)+m(44);
I_org = reshape(II(:,44),mx,my);
figure
subplot(1,2,1)
imagesc(I_org); axis off; axis equal; colormap gray
title(‘Original Image’)
%
% for i=1:72
% B(:,i) = B(:,i)/norm(B(:,i)); % Normalizing each dimention (making a unit vector)
% EF = reshape(B(:,i),mx,my); % Extracting each Eigen Face
% w(i) = B(:,i)‘*I_org(:); % Projecting the image onto each Eigen face (finding the linear coefficient in the new direction)
% I_rec = I_rec + w(i)*EF; % The reconstructed image is the weighted sum of the eigen-faces (we include the first i eagien faces in each iteration)
% subplot(1,2,2)
% imagesc(I_rec); axis off; axis equal; colormap gray
% title([‘Reconstructed Image i=’ num2str(i)])
% pause(0.1)
% end


%% Reconstructing an image outside of the dataset
I_org = double(imread(‘JB2.jpg’));
I_org = I_org(:,2:end-1);
I_rec = zeros(mx,my);
figure
subplot(1,2,1)
imagesc(I_org); axis off; axis equal; colormap gray
title(‘Original Image’)

%
% for i=1:72
% EF = reshape(B(:,i),mx,my); % Extracting each Eigen Face
% w2(i) = B(:,i)’*I_org(:); % Projecting the image onto each Eigen face (finding the linear coefficient in the new direction)
% I_rec = I_rec + w2(i)EF; % The reconstructed image is the weighted sum of the eigen-faces (we include the first i eagien faces in each iteration)
% subplot(1,2,2)
% imagesc(I_rec); axis off; axis equal; colormap gray
% title([‘Reconstructed Image i=’ num2str(i)])
% pause(0.1)
% end
%% Using Eigen faces for classification
% we calculate 6 features that are the projection of the image on eigen
% face 1 to 6 that is being saved in matrix F. The projection of each
% image on each eigen face will provide a unique feature. That means that
% out of 72 possible feature we are using only 6 of them.
% You can see how good each feature is in seperating the subjects, note
% that I have plotted each subject with a differnt collor.

SubCol = ‘rgbkmy’;
F = zeros(72,6);
for i=1:72
F(i,:) = B(:,[1:6])‘II(:,i);
end

figure
for i=1:6
subplot(2,2,1)
sid = (i-1)12+1:(12i);
plot(F(sid,1),F(sid,2),[SubCol(i) ‘o’]); hold on
subplot(2,2,2)
plot(F(sid,3),F(sid,4),[SubCol(i) ‘o’]); hold on
subplot(2,2,3)
plot(F(sid,5),F(sid,6),[SubCol(i) ‘o’]); hold on
end

%% Let See where president Biden will land in our projection
X = B(:,[1:6])‘I_org(:);
subplot(2,2,1); plot(X(1),X(2),'’)
subplot(2,2,2); plot(X(3),X(4),'’)
subplot(2,2,3); plot(X(5),X(6),'‘)

% using the first two features, President Biden’s data is closet to Subject
% 1, shown in red. Which is an old white male with white hair (See Fig 1).
% Is this an accident?

% Let’s try another person
I_HC = imread(‘HC.jpg’);
I_HC = double(rgb2gray(I_HC));
I_HC = I_HC(:,4:end-3);

X = B(:,[1:6])’*I_HC(:);
subplot(2,2,1); plot(X(1),X(2),‘s’)
subplot(2,2,2); plot(X(3),X(4),‘s’)
subplot(2,2,3); plot(X(5),X(6),‘s’)
legend({‘S1’,‘S2’,‘S3’,‘S4’,‘S5’,‘S6’,‘JB’,‘HC’},‘Location’,‘NorthEastOutside’)
%Using features 3 and 4, Hillary Clinton will be closest to subject 6 who
%is a middle age female. See Fig 6
%However, if you use feature 1, she will be closer to Subject 1

%% Reconstruction of HC
I_rec = zeros(mx,my);
figure
subplot(1,2,1)
imagesc(I_HC); axis off; axis equal; colormap gray
title(‘Original Image’)


for i=1:72
EF = reshape(B(:,i),mx,my); % Extracting each Eigen Face
w2(i) = B(:,i)'*I_HC(:); % Projecting the image onto each Eigen face (finding the linear coefficient in the new direction)
I_rec = I_rec + w2(i)*EF; % The reconstructed image is the weighted sum of the eigen-faces (we include the first i eagien faces in each iteration)
subplot(1,2,2)
imagesc(I_rec); axis off; axis equal; colormap gray
title([‘Reconstructed Image i=’ num2str(i)])
pause(0.1)
end  Based on my professor code, Implement the task.

Based on the above implementation, take the parts that helps us solving our task mentioned in the description earlier. Let’s go .