--- 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](https://arxiv.org/abs/1905.11946) - [Focal Loss for Dense Object Detection (Lin et al., 2017)](https://arxiv.org/abs/1708.02002) - [MixUp: Beyond Empirical Risk Minimization](https://arxiv.org/abs/1710.09412) - [CutMix: Regularization Strategy to Train Strong Classifiers](https://arxiv.org/abs/1905.04899) - [AffectNet: A Database for Facial Expression, Valence, and Arousal Computing in the Wild](https://arxiv.org/abs/1708.03985) --- ### 🌐 Live Demo Once deployed, your live demo will be available at: > https://huggingface.co/spaces/abdelrhman145/Face_Emotion_Recognition ---