Back to Trending
RepositoryPythonFRFrance
amineHorseman

amineHorseman/facial-expression-recognition-svm

Training SVM classifier to recognize people expressions (emotions) on Fer2013 dataset

177stars
65forks
6issues
2views

Facial expression recognition using SVM

Extract face landmarks using Dlib and train a multi-class SVM classifier to recognize facial expressions (emotions).

Motivation:

The task is to categorize people images based on the emotion shown by the facial expression. To train our model, we want to use Fer2013 datset that contains 30,000 images of expressions grouped in seven categories: Angry, Disgust, Fear, Happy, Sad, Surprise and Neutral. The problem is that Fer2013 images are not aligned and it's difficult to classify facial expressions from it. The state-of-art accuracy achieved in this dataset, as far as I know, is 75.2% (refer to: Christopher Pramerdorfer, Martin Kampel. "Facial Expression Recognition using Convolutional Neural Networks: State of the Art". arXiv:1612.02903v1, 2016), a Convolutional Neural Network was used during several hours on GPU to obtain these results. Lets try a much simpler (and faster) approach by extracting Face Landmarks + HOG features and feed them to a multi-class SVM classifier. The goal is to get a quick baseline for educational purpose, if you want to achieve better results please refer to Pramerdorfer's paper.

Classification Results :

Features7 emotions5 emotions
HoG features29.0%34.4%
Face landmarks39.2%46.9%
Face landmarks + HOG48.2%55.0%
Face landmarks + HOG on slinding window50.5%59.4%

As expected, the Deep Learning approaches achieve better results (compare results with Facial Expressions Recognition using CNN)

The SVM training time was about ~400 seconds on an i7 2.8Ghz CPU, for the last experiment (sliding window) the training time reached 2060 seconds.

For the experiments with 5 emotions, the following expressions was used: Angry, Happy, Sad, Surprise, Neutral.

The code was tested for python 2.7 and 3.6.

How to use

  1. Download Fer2013 dataset and the Face Landmarks model

  2. Unzip the downloaded files

    And put the files fer2013.csv and shape_predictor_68_face_landmarks.dat in the root folder of this package.

  3. Install dependencies

    pip install numpy
    pip install argparse
    pip install sklearn
    pip install scikit-image
    pip install pandas
    pip install hyperopt
    pip install dlib
    
  4. Convert the dataset to extract Face Landmarks and HOG Features

    python convert_fer2013_to_images_and_landmarks.py
    

    You can also use these optional arguments according to your needs: -j, --jpg (yes|no): save images as .jpg files (default=no) -l, --landmarks (yes|no): extract Dlib Face landmarks (default=yes) -ho, --hog (yes|no): extract HOG features (default=yes) -hw, --hog_windows (yes|no): extract HOG features using a sliding window (default=yes) -o, --onehot (yes|no): one hot encoding (default=no) -e, --expressions (list of numbers): choose the faciale expression you want to use: 0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral (default=0,1,2,3,4,5,6)

    Example

    python convert_fer2013_to_images_and_landmarks.py --landmarks=yes --hog=no --how_windows=no --jpg=no --onehot=no --expressions=1,3,4
    
  5. Train the model

    python train.py --train=yes
    
  6. Evaluate the model

    If you have already a pretrained model

    python train.py --evaluate=yes
    
  7. Train and evaluate [instead of step 5 and 6]

    python train.py --train=yes --evaluate=yes 
    
  8. Customize the training parameters:

    Feel free to change the values of the parameters in the parameters.py file according to your needs.

  9. Find the best hyperparameters (using hyperopt):

    python optimize_parameters.py --max_evals=15
    

    The argument max_evals specifies the number of combinaisions hyperopt will try. After finding the best hyperparameters, you should change the corresponding values in parameters.py and retrain the model. N.B: the accuracies displayed during hyperoptimization are for validation_set only (not test_set)

Contributing

Some ideas for interessted contributors:

  • Add other features extraction techniques?
  • Predict expression from a .jpg|.png file?

Similar Projects

Discover other similar projects and AI models.

RepositoryJupyter Notebook
microsoft/ML-For-Beginners

12 weeks, 26 lessons, 52 quizzes, classic Machine Learning for all

92.5k
Nov 7, 2026
RepositoryC++
tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone

201.9k
Dec 1, 2026
RepositoryJupyter Notebook
tatsuyah/vehicle-detection

Vehicle detection using machine learning and computer vision techniques for Udacity's Self-Driving Car Engineer Nanodegree.

1.1k
Oct 31, 2017
RepositoryPython
pytorch/pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration

105.8k
Dec 1, 2026
RepositoryPython
d2l-ai/d2l-zh

《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。

82.9k
Jul 30, 2024
RepositoryPython
openbq-org/OpenBB

Open Data Platform for analysts, quants and AI agents.

76.0k
Nov 29, 2026
RepositoryJupyter Notebook
rasbt/LLMs-from-scratch

Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

109.4k
Nov 26, 2026
RepositoryPython
scikit-learn/scikit-learn

scikit-learn: machine learning in Python

68.4k
Dec 1, 2026
RepositoryPython
nidhaloff/igel

a delightful machine learning tool that allows you to train, test, and use models without writing code

3.1k
Dec 7, 2025