Skip to content

Ruiqi2002/Age_and_Gender_Predictor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 

Repository files navigation

Author: Ng Rui Qi, Xiu Xiang, Yong Yang

Predicting the apparent Age and Gender using face images

This project aims to implement:-

  1. Preprocessing image

  2. Perform Eigenface Analysis for Eigendecomposition and Singular Value Decomposition (SVD). Discuss their similarity and differences

  3. Develop model to predict apparent age and gender using face images (SVD Processed feature)

  4. implement an Artificial Neural Network (ANN)

  5. discuss our experimental design

Table of content

Import Library

For this project, we have implemented these libraries:

library Functions/Usage
numpy - uses in creating arrays
- performs operations in array.etc
sys - manipulate different parts of Python runtime environment
skimage - use in image processing (greyscale, rescaling)
matplotlib - library use for ploting
glob - an Unix style pathname expansion for python
os - library to call upon os system
time - access time in system
pandas - open source data analysis and manipulation tool
sklearn - library for model selection

Data Preprocessing

this section preprocess data from https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/

Note: After checking , we found out that "61_3_20170109150557335.jpg.chip.jpg" contains an error thats its second value contains a "3" The following image can lead to an missing data in the dataset. Thus, we decided to remove this data from the dataset

Another solution is to add it to 62_1_.......jpg

In the code below, we will set:

D as our dataset

y as the Age of Dataset image (transform into float value between 0 and 1)
To transform the data, the formula: $\displaystyle \frac{Age}{Max Age}$
will be used

Example:
$\displaystyle \frac{72.0}{100.0} = 0.655$

The answer will be in float type (decimal

z will be our Gender in Dataset, which will only contains 0 and 1
For the later computation, these data needs to be in the forms of -1s and 1s.

Note that, in “Gender”,

0 is Male
1 is Female

To transform the data, we will be using the formula,

$2(Gender)-1 $,

From this equation, we can make the values in Gender into -1 and 1 only.
After that, we will print the shape of the list for debugging/checking purpose.

Splitting the dataset into training and testing

The dataset will be split into two dataset

  • training dataset
  • testing dataset

Why do we split the dataset into 90% train and 10% test?

There is mainly due to the size of our dataset. Based on our description above, we can obtain informations of our dataset.
Our dataset only have total of 9779 instances. To obtain a better result, most data instances should be use to train the model.

To obtain fixed result, we have fix the random state as 205

EigenFace Analyse

Eigenface is a name given to a set of eigenvectors when it is used in computer vision problems of human face recognition.

This approach of using eigenfaces for face recognition were developed by Sirovich and Kirby.

The Eigenface technique is using the space of images (face images) to project them as low demensional representation of face images.

Using Principal Component Analysis (PCA), we can use a cpllection of face images to form a set of basis features. The determined principal components can be used to recosntruct an input image and classify a face as an element.

Eigendecomposition Vs Singular Decomposition(SVD)

Consider the formula of Eigendecompositon $A = PDP^{-1}$ and the formula of Singular Value Decomposition(SVD) SVD $A=UΣV^{T}$

Description Eigendecompostion SVD
Properties The Vector Matrix, $P$ are not necessarily orthogonal, thus, the change of basis can be rotation The vectors $U$ and $V$ are orthonormal, so they can perform rotations
- Matrix $P$ and $P^{-1}$ are inverse of each other $U$ and $V$ are not necessarily inverse of each other
- D can be any complex number The entries in the diagonal matrix Σ arre real and positive value
- Can only exist in square matrix due to its formula Can be both rectangular or square matrix
Theory SVD says for any linear map, there is an orthonormal frame in the domain such that it is first mapped to a different orthonormal frame in the image space, and then the values are scaled. Eigendecomposition says that there is a basis, it doesn't have to be orthonormal, such that when the matrix is applied, this basis is simply scaled.

Assuming we have 𝑛 linearly independent eigenvectors of course. In some cases your eigenspaces may have the linear map behave more like upper triangular matrices. (not sure)
Eigenface The column of P is the Eigenface The row of Vt is the Eigenface

Eigendecomposition

Eigendecomposition, also known as matrix diagonalization aims to perform a matrix decomposition of a square matrix into eigenvalues and eigenvectors.

The eigenvectors are derived from the covariance matrix over high-dimensional vectie space of face images. Yhe eigenvectors will form a basis set of images that can use to constrcut covariance matrix which can define a new coordinate system:

  • Eigenvectors with the largest eigenvalue has the most variation among the training vectors $x$
  • Eigenvectors with the smallest eigenvalue has the least variation
  • Thus, the eigenvector formed can be derived as (principle component/eigenface)

Singular Value Decomposition (SVD)

SVD can refactors the images into three different matrices, U S and VT. U and VT are known as singular vector and entry in Σ are singular values

Dicsussion on similarity and difference

Similarity Difference
For both Eigen decomposition and SVD have produce similar faces For Eigen decomposition, we are required to obtain the mean centered data of the image using $D – mean$.
Example, Eigenface 4,5,6,9 After that, we calculate the covariance matrix and obtain the eigenvalue and eigenvectors.
This is because both equations are methods to reduce high dimensional images in dataset while retaining as many features in the data. For SVD, we use $D^{T}D$ to obtain the eigenvalue and eigenvectors.
Thus, it is possible for them to obtain similar eigenfaces.

Preprocessing for SVD processed features

In this section, we will preprocess the dataset for step 5 - develop model using SVD process feature

  • Step 1: calculate the mean of the dataset
  • Step 2: Demean the dataset (x_train - x_train_mean)
  • Step 3: Whittening the dataset

Note: Whitening is a data pre-processing step. It can be used to remove correlation or dependencies between features in a dataset.

Linear Regression

Predict apparent age and gender using face images

For this question, we have choose to apply Closed Form Solution with/without regularization parameter(λ) from our lecture

The formula for closed form solution is:
$W = (D^TD+λI)^{-1}D^Ty $

For better visualization, we will design an experiment to find out the most suitable model to run the dataset.

The aim to create an experiment here to figure out the purpose and usage of SVD in Closed Form Solution and how does it affect our results.

Before we continued in this section, we need to know that:

  • Predicting Age is a regression problem. Thus, we should focus on Mean Squared Error (MSE)
  • Predicting Gender is a classfication problem (only two output will be given). Thus, we should focus on accuracy score

Simple conclusion on the linear regression

Based on observation, we can conclude that the bigger the lambda, the longer the time taken to compute.

The lower lambda value,lower mean sqaured error and the higher accuracy of Gender Prediction.

Among the 5 values we tested ,we can found out that 50 is the best hyperparameter for linear regression.

Linear Regression using SVD

For this SVD, we will be using the whitening reduced dataset to perform the SVD Processed feature

Simple Conclusion on the Linear Regression using SVD processed feature

When we compare this model to our linear regression model, we can spot some difference.

One of the most significant differences will be:

  • the time taken to compute the model. Linear Regression using SVD processed feature quicker.

In our features normalization part,we calculate the mean of the features first.This step is to center the data around the zero and remove the bias.Then we compute demean by substarct mean of features.Demean can ensure the data are around the zero and also handle bias removing.After that, we want to increase more performance of model then we let all the features in a similar scale by whitenning. Target of those steps is to normalize the features by center data,remove bias, scaling features and ensure the models are not overfitting.This is the reason why time taken of linear regression using SVD decrease from 2 seconds to 0.1 seconds.

ANN(SGD and ADAM)

A prediction function will be written and used in both SGD and ADAM
In the prediction function, sigmoid function,

$\displaystyle \frac{1}{1 + \exp(-y)}$

Both model will be using the whitten_reduced dataset for better efficiency

Comparison between Linear Regression, Linear Regression using SVD, ANN with MSE and ANN with ADAM

  Model MSE ACC
0 Linear Regression -> 0.066193 0.668712
1 Linear using SVD -> 0.075199 0.643149
2 SGD -> 0.203906 0.537832
3 ADAM -> 0.078690 0.627812

Discussion

  • Throught observing the table,we can know that linear regression without SVD had the lowest MSE and highest ACC.This model use the orignal dataset and another models used the SVD processed features.Then there has a probabiity of SVD maybe remove some important features and quantity of train set decrease.Therefore another model maybe underfitting.
  • Time Taken of Linear Regression with using SVD is less than model without SVD because of quantity of features linear model using svd is less than normal linear model.
  • we can come to conclusion that SVD Process Feature plays a huge role in reducing the time taken to compute a model

From the result of both ANN with SGD and ADAM

  • For the SGD model and ADAM model,we can improve the efficiency of both model through modify the number of learning rate,number of nueron in hidden layer and epochs.
  • We find the MSE gradually decrease and ACC gradually increase when the number of epochs increase.Therefore we set all number of epochs of the predictive model to be 30.Through our testing (learning rate,number of nueron in hidden layer),we use (0.01,3) for SGD model to predict age.(0.001,3) for SGD model to predict gender and ADAM model to predict age .(0.005,50) for ADAM model to predict gender.
  • For the result,ADAM has a higher performance than SGD model.The trainset for predict gender of ADAM had a result which near to 0.9.This is a quite high result of predictive model.However SGD only had 0.5 acc of gender prediction.Moreover,SGD model had nearly 0.3 score of mse and result o ADAM almost less than 0.1.
  • Althought ADAM is better than SGD but time process is moere than SGD model.
  • Therefore we can know ADAM model is better than SGD model in age and gender prediction but maybe SGD model also can be improved by find the best hyperparameter for it.

Conclusion

As a conclusion, we can know that linear regression without using svd is the best model topredict age and gender

As the results, the 4 models have almost same result of trainset that mean the 4 models are such a good implementation.

Our Model Prediction

In this section, we have implement the model on top to predict our own images

Conclusion

Because of linear regression without using SVD processed features had the lowest MSE and highest ACC.Therefore we decided to use it for progress our selfie to predict age and gender.

After fitting data into the model,we get a good results for our picture because there is no exaggerate results to us.Among the result there is only the picture 3 has special result maybe that mean picture 3 had some problem.As the result can show that,linear regression without using SVD is a good implementation

~ THANK YOU ~

About

This project aims to predict the age and gender of a person using face images. Different approach is used to find out the best results.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors