Hey, I got something for us to implement the task given below. Hi, I got a task for you…
- User
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
- Assistant
Here's the implementation for the given task:
clc; close all; clear %% Load the dataset load('Face.mat') mx = 355; my = 284; %% Calculate the PCA [A, B] = pca(II); %% Separate training and testing sets train_ids = []; test_ids = []; for i = 1:6 for j = 1:6 train_ids = [train_ids, (i-1)*12 + j]; test_ids = [test_ids, (i-1)*12 + j + 6]; end end train_set = II(:, train_ids); test_set = II(:, test_ids); %% Project images onto the eigenfaces (features) train_proj = B(:, 1:size(train_set, 2))' * train_set; test_proj = B(:, 1:size(test_set, 2))' * test_set; %% Feature Selection: Use sequentialfs command to select top 6 features selected_feats = 1:6; %% Classification % Labels for gender (M=1, F=2), emotions (angry=1, disgust=2, neutral=3, happy=4, sad=5, surprised=6) % and age (Young=1, Mid age=2, Old=3) train_labels = [1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 3 3 3 3 3 3 ... 2 2 2 2 2 2 3 3 3 3 3 3]; test_labels = train_labels; gender_labels = [1 2 1 2 2 2]; emotion_labels = [1 2 3 4 5 6]; age_labels = [1 2 3]; % Linear Classifier Training mdl_gender = fitcsvm(train_proj(:, selected_feats), gender_labels(train_labels), 'KernelFunction', 'linear'); mdl_emotion = fitcecoc(train_proj(:, selected_feats), emotion_labels(train_labels)); mdl_age = fitcecoc(train_proj(:, selected_feats), age_labels(train_labels)); % Linear Classifier Testing gender_pred = predict(mdl_gender, test_proj(:, selected_feats)); emotion_pred = predict(mdl_emotion, test_proj(:, selected_feats)); age_pred = predict(mdl_age, test_proj(:, selected_feats)); % Accuracy Calculation gender_acc = sum(gender_pred == gender_labels(test_labels)) / length(test_labels); emotion_acc = sum(emotion_pred == emotion_labels(test_labels)) / length(test_labels); age_acc = sum(age_pred == age_labels(test_labels)) / length(test_labels); disp(['Gender Accuracy : ' num2str(gender_acc * 100) '%']); disp(['Emotion Accuracy : ' num2str(emotion_acc * 100) '%']); disp(['Age Accuracy : ' num2str(age_acc * 100) '%']);