Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,193 +1,219 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
tags:
|
| 4 |
-
- facial-expression-recognition
|
| 5 |
-
- emotion-recognition
|
| 6 |
-
- computer-vision
|
| 7 |
-
- pytorch
|
| 8 |
-
- mediapipe
|
| 9 |
-
- efficientnet
|
| 10 |
-
- real-time
|
| 11 |
-
- image-classification
|
| 12 |
-
pipeline_tag: image-classification
|
| 13 |
-
---
|
| 14 |
-
|
| 15 |
-
<div align="center">
|
| 16 |
-
|
| 17 |
-

|
| 18 |
-
|
| 19 |
-
<a href="https://git.io/typing-svg"><img src="https://readme-typing-svg.demolab.com?font=Fira+Code&weight=600&size=22&pause=1000&color=06B6D4¢er=true&vCenter=true&width=750&lines=Hybrid+CNN+%2B+MediaPipe+Landmark+Architecture;7+Emotion+Classes+%E2%80%94+Real-Time+at+30+FPS;Bidirectional+Cross-Attention+%7C+EfficientNet-
|
| 20 |
-
|
| 21 |
-
<br/>
|
| 22 |
-
|
| 23 |
-

|
| 24 |
-

|
| 25 |
-

|
| 26 |
-

|
| 27 |
-

|
| 28 |
-

|
| 29 |
-
[](https://github.com/Huuffy/VisageCNN)
|
| 30 |
-
|
| 31 |
-
</div>
|
| 32 |
-
|
| 33 |
-
---
|
| 34 |
-
|
| 35 |
-
## What Is This?
|
| 36 |
-
|
| 37 |
-
**HybridEmotionNet** β a dual-branch neural network for real-time facial emotion recognition that fuses **EfficientNet-
|
| 38 |
-
|
| 39 |
-
Processes webcam frames at **30+ FPS**, extracts **478 3D landmarks**, crops the face, and classifies into 7 emotions with temporal smoothing.
|
| 40 |
-
|
| 41 |
-
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
| 69 |
-
|
| 70 |
-
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
---
|
| 156 |
-
|
| 157 |
-
##
|
| 158 |
-
|
| 159 |
-
|
|
| 160 |
-
|---------|-------|
|
| 161 |
-
|
|
| 162 |
-
|
|
| 163 |
-
|
|
| 164 |
-
|
|
| 165 |
-
|
|
| 166 |
-
|
|
| 167 |
-
|
|
| 168 |
-
|
| 169 |
-
---
|
| 170 |
-
|
| 171 |
-
##
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- facial-expression-recognition
|
| 5 |
+
- emotion-recognition
|
| 6 |
+
- computer-vision
|
| 7 |
+
- pytorch
|
| 8 |
+
- mediapipe
|
| 9 |
+
- efficientnet
|
| 10 |
+
- real-time
|
| 11 |
+
- image-classification
|
| 12 |
+
pipeline_tag: image-classification
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
<div align="center">
|
| 16 |
+
|
| 17 |
+

|
| 18 |
+
|
| 19 |
+
<a href="https://git.io/typing-svg"><img src="https://readme-typing-svg.demolab.com?font=Fira+Code&weight=600&size=22&pause=1000&color=06B6D4¢er=true&vCenter=true&width=750&lines=Hybrid+CNN+%2B+MediaPipe+Landmark+Architecture;7+Emotion+Classes+%E2%80%94+Real-Time+at+30+FPS;Bidirectional+Cross-Attention+%7C+EfficientNet-B2+%2B+478+Landmarks;87.9%25+Validation+Accuracy+%7C+Disgust+92%25+Recall" alt="Typing SVG" /></a>
|
| 20 |
+
|
| 21 |
+
<br/>
|
| 22 |
+
|
| 23 |
+

|
| 24 |
+

|
| 25 |
+

|
| 26 |
+

|
| 27 |
+

|
| 28 |
+

|
| 29 |
+
[](https://github.com/Huuffy/VisageCNN)
|
| 30 |
+
|
| 31 |
+
</div>
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## What Is This?
|
| 36 |
+
|
| 37 |
+
**HybridEmotionNet ** β a dual-branch neural network for real-time facial emotion recognition that fuses **EfficientNet-B2 appearance features** with **MediaPipe 3D landmark geometry** via bidirectional cross-attention.
|
| 38 |
+
|
| 39 |
+
Processes webcam frames at **30+ FPS**, extracts **478 3D landmarks**, crops the face at 224Γ224, and classifies into 7 emotions with EMA + sliding window temporal smoothing.
|
| 40 |
+
|
| 41 |
+
** highlights:** 87.9% validation accuracy Β· Disgust recall 51%β90% Β· Fear recall 65%β75% Β· 75k balanced training images Β· ViT-scored quality filtering
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
## Architecture
|
| 46 |
+
|
| 47 |
+

|
| 48 |
+
|
| 49 |
+
```
|
| 50 |
+
Face crop (224Γ224) βββΊ EfficientNet-B2 βββΊ [B, 256] appearance
|
| 51 |
+
blocks 0-1 frozen
|
| 52 |
+
blocks 2-8 fine-tuned
|
| 53 |
+
|
| 54 |
+
478 landmarks (xyz) βββΊ MLP encoder βββΊ [B, 256] geometry
|
| 55 |
+
1434 β 512 β 384 β 256
|
| 56 |
+
|
| 57 |
+
Bidirectional Cross-Attention (4 heads each)
|
| 58 |
+
ββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
β coord β CNN (geometry queries appear.) β
|
| 60 |
+
β CNN β coord (appear. queries geometry) β
|
| 61 |
+
ββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββββββββββββββ
|
| 62 |
+
β
|
| 63 |
+
Fusion MLP: 512 β 384 β 256 β 128
|
| 64 |
+
β
|
| 65 |
+
Classifier: 128 β 7 emotions
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
| Component | Detail |
|
| 69 |
+
|-----------|--------|
|
| 70 |
+
| CNN branch | EfficientNet-B2, ImageNet init, blocks 0β1 frozen, gradient checkpointing |
|
| 71 |
+
| Coord branch | MLP 1434 β 512 β 384 β 256, BN + Dropout |
|
| 72 |
+
| Fusion | Bidirectional cross-attention + MLP |
|
| 73 |
+
| Parameters | ~8M total |
|
| 74 |
+
| Model size | ~90 MB |
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
## Performance
|
| 79 |
+
|
| 80 |
+
| Metric | Value |
|
| 81 |
+
|--------|-------|
|
| 82 |
+
| Validation accuracy | **87.9%** |
|
| 83 |
+
| Macro F1 | **0.88** |
|
| 84 |
+
| Inference speed | ~12 ms/frame on RTX 3050 |
|
| 85 |
+
|
| 86 |
+
| Emotion | Precision | Recall | F1 |
|
| 87 |
+
|---------|-----------|--------|----|
|
| 88 |
+
| Angry | 0.85 | 0.83 | 0.84 |
|
| 89 |
+
| Disgust | 0.97 | 0.90 | 0.94 |
|
| 90 |
+
| Fear | 0.89 | 0.75 | 0.82 |
|
| 91 |
+
| Happy | 0.97 | 0.99 | 0.98 |
|
| 92 |
+
| Neutral | 0.85 | 0.91 | 0.88 |
|
| 93 |
+
| Sad | 0.78 | 0.88 | 0.83 |
|
| 94 |
+
| Surprised | 0.83 | 0.90 | 0.86 |
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## Files in This Repo
|
| 99 |
+
|
| 100 |
+
| File | Size | Required |
|
| 101 |
+
|------|------|---------|
|
| 102 |
+
| `models/weights/hybrid_best_model.pth` | ~90 MB | Yes β best macro F1 checkpoint |
|
| 103 |
+
| `models/weights/hybrid_swa_final.pth` | ~90 MB | Optional β SWA ensemble model |
|
| 104 |
+
| `models/scalers/hybrid_coordinate_scaler.pkl` | 18 KB | Yes β landmark scaler |
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## Quick Start
|
| 109 |
+
|
| 110 |
+
### 1 β Clone the code
|
| 111 |
+
|
| 112 |
+
```bash
|
| 113 |
+
git clone https://github.com/Huuffy/VisageCNN.git
|
| 114 |
+
cd VisageCNN
|
| 115 |
+
python -m venv venv && venv\Scripts\activate
|
| 116 |
+
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
|
| 117 |
+
pip install -r requirements.txt
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
### 2 β Download weights
|
| 121 |
+
|
| 122 |
+
```python
|
| 123 |
+
from huggingface_hub import hf_hub_download
|
| 124 |
+
import shutil, pathlib
|
| 125 |
+
|
| 126 |
+
for remote, local in [
|
| 127 |
+
("models/weights/hybrid_best_model.pth", "models/weights/hybrid_best_model.pth"),
|
| 128 |
+
("models/weights/hybrid_swa_final.pth", "models/weights/hybrid_swa_final.pth"),
|
| 129 |
+
("models/scalers/hybrid_coordinate_scaler.pkl", "models/scalers/hybrid_coordinate_scaler.pkl"),
|
| 130 |
+
]:
|
| 131 |
+
src = hf_hub_download(repo_id="Huuffy/VisageCNN", filename=remote)
|
| 132 |
+
pathlib.Path(local).parent.mkdir(parents=True, exist_ok=True)
|
| 133 |
+
shutil.copy(src, local)
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
Or with the HF CLI:
|
| 137 |
+
```bash
|
| 138 |
+
huggingface-cli download Huuffy/VisageCNN models/weights/hybrid_best_model.pth --local-dir .
|
| 139 |
+
huggingface-cli download Huuffy/VisageCNN models/weights/hybrid_swa_final.pth --local-dir .
|
| 140 |
+
huggingface-cli download Huuffy/VisageCNN models/scalers/hybrid_coordinate_scaler.pkl --local-dir .
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### 3 β Run inference
|
| 144 |
+
|
| 145 |
+
```bash
|
| 146 |
+
# Standard
|
| 147 |
+
python inference/run_hybrid.py
|
| 148 |
+
|
| 149 |
+
# With SWA ensemble
|
| 150 |
+
python inference/run_hybrid.py --ensemble
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
Press **Q** to quit.
|
| 154 |
+
|
| 155 |
+
---
|
| 156 |
+
|
| 157 |
+
## Emotion Classes
|
| 158 |
+
|
| 159 |
+
| Label | Emotion | Key Signals |
|
| 160 |
+
|-------|---------|-------------|
|
| 161 |
+
| 0 | Angry | Furrowed brows, tightened jaw |
|
| 162 |
+
| 1 | Disgust | Raised upper lip, wrinkled nose |
|
| 163 |
+
| 2 | Fear | Wide eyes, raised brows, open mouth |
|
| 164 |
+
| 3 | Happy | Raised cheeks, open smile |
|
| 165 |
+
| 4 | Neutral | Relaxed, no strong deformation |
|
| 166 |
+
| 5 | Sad | Lowered brow corners, downturned lips |
|
| 167 |
+
| 6 | Surprised | Raised brows, wide eyes, dropped jaw |
|
| 168 |
+
|
| 169 |
+
---
|
| 170 |
+
|
| 171 |
+
## Training Dataset
|
| 172 |
+
|
| 173 |
+
75,376 total images β 10,768 per class Γ 7 emotions, perfectly balanced.
|
| 174 |
+
|
| 175 |
+
**Sources:** AffectNet Β· RAF-DB Β· FER2013 Β· AffectNet-Short Β· ScullyowesHenry Β· RAF-DB Kaggle
|
| 176 |
+
|
| 177 |
+
All images passed a two-stage quality filter:
|
| 178 |
+
1. MediaPipe FaceMesh (dual confidence: 0.5 normal + 0.2 lenient for extreme expressions)
|
| 179 |
+
2. ViT confidence scoring (`dima806/facial_emotions_image_detection`) with per-class asymmetric mislabel thresholds
|
| 180 |
+
|
| 181 |
+
Final class balance achieved via ViT-scored capping β lowest-confidence images removed first, preserving the highest quality examples per class.
|
| 182 |
+
|
| 183 |
+
---
|
| 184 |
+
|
| 185 |
+
## Training Config
|
| 186 |
+
|
| 187 |
+
| Setting | Value |
|
| 188 |
+
|---------|-------|
|
| 189 |
+
| Loss | Focal Loss Ξ³=2.0 + label smoothing 0.12 |
|
| 190 |
+
| Optimizer | AdamW, weight decay 0.05 |
|
| 191 |
+
| LR | OneCycleLR β CNN 5e-5, fusion 5e-4 |
|
| 192 |
+
| Batch | 96 + grad accumulation Γ2 (eff. 192) |
|
| 193 |
+
| Augmentation | CutMix + noise + rotation + zoom |
|
| 194 |
+
| Mixed precision | torch.amp (AMP) |
|
| 195 |
+
| Best model saved by | Macro F1 (not val accuracy) |
|
| 196 |
+
| SWA | Epochs 30β70, BN update after training |
|
| 197 |
+
| Early stopping | patience=15 on macro F1 |
|
| 198 |
+
|
| 199 |
+
---
|
| 200 |
+
|
| 201 |
+
## Retrain From Scratch
|
| 202 |
+
|
| 203 |
+
```bash
|
| 204 |
+
# Delete old cache and train
|
| 205 |
+
rmdir /s /q models\cache
|
| 206 |
+
python scripts/train_hybrid.py
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
Full guide: [GitHub README](https://github.com/Huuffy/VisageCNN)
|
| 210 |
+
|
| 211 |
+
---
|
| 212 |
+
|
| 213 |
+
<div align="center">
|
| 214 |
+
|
| 215 |
+
**Built with curiosity and a lot of training runs**
|
| 216 |
+
|
| 217 |
+

|
| 218 |
+
|
| 219 |
+
</div>
|