Skip to content

Face model to classify gender and race. Trained on LFWA+ Dataset.

Notifications You must be signed in to change notification settings

trantrungpixta/face-classification

 
 

Repository files navigation

Gender and Race Classification with Face Images

Let's train a simple face attribute classification model! The dataset we are going to use contains 73 different face attributes, but we are only going to focus on gender and race. Here are some sample results of the trained model. (For now, the dataset I used has only "White," "Black," and "Asian" faces. I will try to add more races in future)

Ellen's Oscar Selfie Trump Rally Korean Protest

Requirements

Packages

dlib
face_recognition
NumPy
pandas
scikit-learn

Dataset

LFWA+ dataset

Usage

Data Preprocessing

Download Dataset

You can download LFWA+ dataset here. You will see a Dropbox link for LFWA+ dataset in this page. Please unzip the lfw.zip file after you download.

Feature Extraction
python feature_extraction.py --data_dir LFWA/ --save_feature feature.csv --save_label label.csv

You can run feature_extraction.py like the above command or just run the jupyter notebook (feature-extracion.ipynb) step by step.

We are going to use the face embedding function face_encodings in face_recognition package, which is based on dlib's face_recognition_model_v1. This function takes a face image as input and outputs a 128-dimensional face embedding vector. This function is basically an implementation of Face-Net, a deep CNN trained with triplet loss. Please refer to the original paper for more details

For face detection, I just used face_recognition's face_locations. This is a histogram of oriented gradient (HOG), which is quite outdated, by default. You can use a CNN face detector by changing input parameters in line 49 of feature_extraction.py and line 20 of pred.py like the follwing

# line 49 of feature_extraction.py
X_faces_loc = face_locations(X_img, model = "cnn")
# line 20 of pred.py
locs = face_locations(X_img, number_of_times_to_upsample = N_UPSCLAE, model = "cnn")

Model Training

python train.py --feature feature.csv --label label.csv --save_model face_model.pkl

You can run train.py as above or just run the jupyter notebook (train.ipynb) step by step.

I used MLPClassifier in sklearn package for the classifier. You can change it to the classifier of your flavor but I like MLPClassifier since it is really simple!

Prediction

Classify face attributes for all images in a directory using pred.py

python pred.py --img_dir test/ --model face_model.pkl

More Examples!

blm women's march g20 grammy volleyball baskettball obama I love Obama.. Please don't get me wrong! real madrid sanders

Future Works

  • Add more data (Google Image Search)
  • Use a better face detection model (faster rcnn)

About

Face model to classify gender and race. Trained on LFWA+ Dataset.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 93.7%
  • Python 6.3%