A deep learning-based system for detecting human emotions from facial expressions using Convolutional Neural Networks (CNN).
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
- 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
- Python 3.8 or higher
- pip package manager
- Git LFS (for model files)
- Clone the repository
git clone https://github.com/BraveVahid/facial_emotion_recognition.git
cd facial_emotion_recognition- 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- Install required packages
pip install -r requirements.txt- 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/
python -m src.trainTraining 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
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
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
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
| Metric | Value |
|---|---|
| Test Accuracy | 66.13% |
| Test Loss | 1.1089 |
| 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 |
- 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
- GitHub: @BraveVahid
- Email: vahidsiyami.dev@gmail.com
- Telegram: @BraveVahid
⭐ Star this repository if you like it!