Skip to content

BraveVahid/facial-emotion-recognition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Facial Emotion Recognition

Status

A deep learning-based system for detecting human emotions from facial expressions using Convolutional Neural Networks (CNN).

📋 Overview

This project implements an emotion recognition system that can classify facial expressions into 7 different emotions:

  • 😠 Angry
  • 🤢 Disgust
  • 😨 Fear
  • 😊 Happy
  • 😐 Neutral
  • 😢 Sad
  • 😲 Surprise

The model is trained on the FER2013 dataset and achieves approximately 66% accuracy on the test set.

⚠️ Note: This is a work-in-progress project. Future versions will include:

  • Real-time emotion detection using webcam
  • Web-based demo interface

Features

  • Custom CNN Architecture: Deep convolutional neural network with batch normalization and dropout
  • Data Augmentation: Comprehensive augmentation pipeline to improve model generalization
  • Class Balancing: Automatic class weight calculation to handle imbalanced dataset
  • Training Monitoring: Callbacks for early stopping, learning rate reduction, and model checkpointing
  • Comprehensive Evaluation: Detailed performance metrics, confusion matrix, and misclassification analysis

🚀 Installation

Prerequisites:

  • Python 3.8 or higher
  • pip package manager
  • Git LFS (for model files)

Setup:

  1. Clone the repository
git clone https://github.com/BraveVahid/facial_emotion_recognition.git
cd facial_emotion_recognition
  1. Install Git LFS (if not already installed)
# On Ubuntu/Debian
sudo apt-get install git-lfs

# On macOS
brew install git-lfs

# Initialize Git LFS
git lfs install
  1. Install required packages
pip install -r requirements.txt
  1. Download the FER2013 dataset
  • Download from Kaggle
  • Extract and place in the data/ directory with the following structure:
data/
└── fer2013/
    ├── train/
    │   ├── angry/
    │   ├── disgust/
    │   ├── fear/
    │   ├── happy/
    │   ├── neutral/
    │   ├── sad/
    │   └── surprise/
    └── test/
        ├── angry/
        ├── disgust/
        ├── fear/
        ├── happy/
        ├── neutral/
        ├── sad/
        └── surprise/

💻 Usage

Training the Model:

python -m src.train

Training parameters (can be modified in src/train.py):

  • batch size: 64
  • epochs: 100
  • Automatic early stopping with patience of 15 epochs
  • Learning rate reduction on plateau

Evaluating the Model

Open and run the reports.ipynb notebook to:

  • Load the trained model
  • Evaluate on test dataset
  • Generate classification report
  • Visualize confusion matrix
  • Analyze misclassified samples

🏗️ Model Architecture

The model consists of:

Convolutional Blocks:

  • 4 convolutional blocks with increasing filter sizes (64 → 128 → 256 → 512)
  • Batch normalization after each convolution
  • ReLU activation
  • Max pooling (2×2)
  • Dropout layers (0.25 → 0.3 → 0.4 → 0.5)

Fully Connected Layers:

  • Flatten layer
  • Dense layer (1024 neurons) + BatchNorm + Dropout (0.5)
  • Dense layer (512 neurons) + BatchNorm + Dropout (0.5)
  • Output layer (7 neurons, softmax activation)

Regularization:

  • L2 regularization (0.0001) on all layers
  • Dropout at multiple stages
  • Data augmentation during training

Total Parameters: ~10.5M

📁 Project Structure

emotion-detection/
├── .gitattributes      
├── .gitignore       
├── README.md         
├── requirements.txt     
├── reports.ipynb      
├── data/           
│   └── fer2013/
│       ├── train/
│       └── test/
├── model/       
│   ├── emotion_recognition.keras
│   ├── training_history.json
│   ├── training_history.csv
│   └── training_history.pkl
└── src/          
    ├── data_loader.py  
    ├── model.py  
    └── train.py  

📊 Results:

Performance Metrics

Metric Value
Test Accuracy 66.13%
Test Loss 1.1089

Per-Class Performance

Emotion Precision Recall F1-Score Support
Angry 0.55 0.64 0.59 958
Disgust 0.55 0.67 0.60 111
Fear 0.56 0.35 0.43 1024
Happy 0.92 0.83 0.87 1774
Neutral 0.56 0.74 0.64 1233
Sad 0.56 0.51 0.53 1247
Surprise 0.72 0.84 0.78 831

Key Observations:

  • Best Performance: Happy emotion (92% precision, 87% F1-score)
  • Challenging Classes: Fear (43% F1-score) and Sad (53% F1-score)
  • Balanced Accuracy: Model shows relatively consistent performance across most classes
  • The confusion matrix reveals common misclassifications between similar emotions

👨‍💻 Author

Vahid Siyami


⭐ Star this repository if you like it!

About

A deep learning project for facial emotion recognition using CNN architecture trained on the FER2013 dataset. The model classifies facial expressions into 7 emotion categories with comprehensive data augmentation and regularization techniques.

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors