title: Facial Emotion Recognition
emoji: π
colorFrom: blue
colorTo: pink
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
π Facial Emotion Recognition (EfficientNet-B4 + Focal Loss + MixUp/CutMix)
This project demonstrates Facial Emotion Recognition using a fine-tuned EfficientNet-B4 model trained on a balanced subset of the AffectNet dataset.
It can detect the emotion of a person from:
- πΈ Uploaded images
- π₯ Live camera feed
π§ Model Overview
| Feature | Description |
|---|---|
| Base Model | EfficientNet-B4 (from timm library) |
| Loss Function | Focal Loss (Ξ³=2, Ξ±=1) |
| Regularization | MixUp & CutMix augmentations |
| Optimizer | AdamW with CosineAnnealingLR |
| Input Size | 224Γ224 |
| Classes | Anger, Contempt, Disgust, Fear, Happy, Neutral, Sad, Surprise |
These techniques were introduced to reduce overfitting and enhance model generalization, achieving higher validation accuracy compared to a baseline model using standard CrossEntropyLoss.
π Performance Summary
| Metric | Baseline (CE Loss) | Improved Model (Focal + MixUp/CutMix) |
|---|---|---|
| Train Accuracy | 97.8% | 60.6% |
| Val Accuracy | 77.2% | 79.2% |
| Val F1 Score | 0.77 | 0.79 |
| Early Stopping | No | β Yes |
| Overfitting | β Present | β Reduced |
Note: The validation accuracy surpasses the training accuracy in the improved model, indicating successful regularization and harder training augmentations that improve generalization.
βοΈ Usage
πΈ Upload Image
Upload an image containing a face β the model will detect the face, draw a bounding box, and display the predicted emotion label.
π₯ Live Camera
Activate your webcam to detect emotions in real-time. The bounding box and emotion label are updated continuously.
π§© Tech Stack
- Python 3.10+
- PyTorch (for model training & inference)
- Timm (for EfficientNet backbone)
- OpenCV (for face detection)
- Gradio (for the web interface)
- NumPy, Pillow, Seaborn, Matplotlib (for visualization & preprocessing)
π Dataset
The model was trained using a balanced subset of the AffectNet dataset, resized to 224Γ224 with approximately 3,690 images per class.
Augmentations included random flips, color jitter, and light rotations to preserve facial expression semantics.
π¨βπ» Authors
Developed by:
- Abdelrhman Khaled Hassan
As part of an academic research project exploring emotion classification and regularization techniques in deep learning.
π References
- EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks
- Focal Loss for Dense Object Detection (Lin et al., 2017)
- MixUp: Beyond Empirical Risk Minimization
- CutMix: Regularization Strategy to Train Strong Classifiers
- AffectNet: A Database for Facial Expression, Valence, and Arousal Computing in the Wild
π Live Demo
Once deployed, your live demo will be available at:
https://huggingface.co/spaces/abdelrhman145/Face_Emotion_Recognition