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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: mit
|
| 4 |
+
tags:
|
| 5 |
+
- facial-expression-recognition
|
| 6 |
+
- emotion-detection
|
| 7 |
+
- mental-health
|
| 8 |
+
- swin-transformer
|
| 9 |
+
- pytorch
|
| 10 |
+
- computer-vision
|
| 11 |
+
datasets:
|
| 12 |
+
- FER2013
|
| 13 |
+
metrics:
|
| 14 |
+
- accuracy
|
| 15 |
+
- f1-score
|
| 16 |
+
library_name: pytorch
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Facial Expression Recognition for Mental Health Detection
|
| 20 |
+
|
| 21 |
+
## Model Description
|
| 22 |
+
|
| 23 |
+
This model is a **Swin Transformer** fine-tuned for facial expression recognition (FER) with applications in mental health detection. It can classify facial expressions into 7 categories and provide depression risk analysis based on emotional patterns.
|
| 24 |
+
|
| 25 |
+
### Model Architecture
|
| 26 |
+
|
| 27 |
+
- **Base Model**: Swin Transformer (swin_base_patch4_window7_224)
|
| 28 |
+
- **Custom Classifier**:
|
| 29 |
+
- Linear layer (backbone features β 512)
|
| 30 |
+
- ReLU activation
|
| 31 |
+
- Dropout (p=0.6)
|
| 32 |
+
- Linear layer (512 β 7 classes)
|
| 33 |
+
|
| 34 |
+
### Emotion Classes
|
| 35 |
+
|
| 36 |
+
The model predicts 7 facial expressions:
|
| 37 |
+
1. **Angry** π
|
| 38 |
+
2. **Disgust** π€’
|
| 39 |
+
3. **Fear** π¨
|
| 40 |
+
4. **Happy** π
|
| 41 |
+
5. **Neutral** π
|
| 42 |
+
6. **Sad** π’
|
| 43 |
+
7. **Surprise** π²
|
| 44 |
+
|
| 45 |
+
## Training Details
|
| 46 |
+
|
| 47 |
+
### Dataset
|
| 48 |
+
|
| 49 |
+
- **Name**: FER2013 (Facial Expression Recognition 2013)
|
| 50 |
+
- **Size**: ~35,000 grayscale images (48x48 pixels)
|
| 51 |
+
- **Split**: Train/Validation/Test
|
| 52 |
+
|
| 53 |
+
### Training Configuration
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
- **Optimizer**: AdamW
|
| 57 |
+
- **Learning Rate**: 1e-4 with cosine annealing
|
| 58 |
+
- **Batch Size**: 32
|
| 59 |
+
- **Epochs**: 5
|
| 60 |
+
- **Image Size**: 224x224
|
| 61 |
+
- **Data Augmentation**: Random horizontal flip, rotation, color jitter
|
| 62 |
+
- **Loss Function**: Cross-Entropy Loss
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
## Usage
|
| 66 |
+
|
| 67 |
+
### Installation
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
pip install torch torchvision timm huggingface_hub
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### Load Model
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
import torch
|
| 77 |
+
import timm
|
| 78 |
+
from huggingface_hub import hf_hub_download
|
| 79 |
+
|
| 80 |
+
class CustomSwinTransformer(torch.nn.Module):
|
| 81 |
+
def __init__(self, pretrained=True, num_classes=7):
|
| 82 |
+
super(CustomSwinTransformer, self).__init__()
|
| 83 |
+
self.backbone = timm.create_model('swin_base_patch4_window7_224',
|
| 84 |
+
pretrained=pretrained, num_classes=0)
|
| 85 |
+
self.classifier = torch.nn.Sequential(
|
| 86 |
+
torch.nn.Linear(self.backbone.num_features, 512),
|
| 87 |
+
torch.nn.ReLU(),
|
| 88 |
+
torch.nn.Dropout(p=0.6),
|
| 89 |
+
torch.nn.Linear(512, num_classes)
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def forward(self, x):
|
| 93 |
+
x = self.backbone(x)
|
| 94 |
+
return self.classifier(x)
|
| 95 |
+
|
| 96 |
+
# Download and load model
|
| 97 |
+
model_path = hf_hub_download(repo_id="SEARO1/FER_model", filename="best_model.pth")
|
| 98 |
+
model = CustomSwinTransformer(pretrained=False, num_classes=7)
|
| 99 |
+
model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=False)
|
| 100 |
+
model.eval()
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### Inference Example
|
| 104 |
+
|
| 105 |
+
```python
|
| 106 |
+
from torchvision import transforms
|
| 107 |
+
from PIL import Image
|
| 108 |
+
|
| 109 |
+
# Prepare image
|
| 110 |
+
transform = transforms.Compose([
|
| 111 |
+
transforms.Resize((224, 224)),
|
| 112 |
+
transforms.ToTensor(),
|
| 113 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 114 |
+
])
|
| 115 |
+
|
| 116 |
+
image = Image.open("face.jpg").convert("RGB")
|
| 117 |
+
input_tensor = transform(image).unsqueeze(0)
|
| 118 |
+
|
| 119 |
+
# Predict
|
| 120 |
+
with torch.no_grad():
|
| 121 |
+
output = model(input_tensor)
|
| 122 |
+
probabilities = torch.nn.functional.softmax(output, dim=1)
|
| 123 |
+
predicted_class = torch.argmax(probabilities, dim=1)
|
| 124 |
+
|
| 125 |
+
emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
|
| 126 |
+
print(f"Predicted Emotion: {emotions[predicted_class.item()]}")
|
| 127 |
+
print(f"Confidence: {probabilities[0][predicted_class].item()*100:.2f}%")
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Mental Health Application
|
| 131 |
+
|
| 132 |
+
This model can be used for depression risk analysis by analyzing emotional patterns:
|
| 133 |
+
|
| 134 |
+
### Depression Risk Calculation
|
| 135 |
+
|
| 136 |
+
```python
|
| 137 |
+
def analyze_depression_risk(emotion_probs):
|
| 138 |
+
sad_score = emotion_probs[5] # Sad
|
| 139 |
+
fear_score = emotion_probs[2] # Fear
|
| 140 |
+
angry_score = emotion_probs[0] # Angry
|
| 141 |
+
happy_score = emotion_probs[3] # Happy
|
| 142 |
+
|
| 143 |
+
negative_emotions = (sad_score * 0.4 + fear_score * 0.3 + angry_score * 0.3)
|
| 144 |
+
positive_emotions = happy_score
|
| 145 |
+
|
| 146 |
+
depression_risk = (negative_emotions * 100) - (positive_emotions * 20)
|
| 147 |
+
depression_risk = max(0, min(100, depression_risk))
|
| 148 |
+
|
| 149 |
+
if depression_risk < 30:
|
| 150 |
+
return "Low Risk"
|
| 151 |
+
elif depression_risk < 60:
|
| 152 |
+
return "Moderate Risk"
|
| 153 |
+
else:
|
| 154 |
+
return "High Risk"
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
β οΈ **Important**: This is an educational tool and should NOT replace professional medical advice or diagnosis.
|
| 158 |
+
|
| 159 |
+
## Performance
|
| 160 |
+
|
| 161 |
+
The model achieves competitive performance on the FER2013 dataset. See the training logs for detailed metrics.
|
| 162 |
+
|
| 163 |
+
## Limitations
|
| 164 |
+
|
| 165 |
+
- Trained on FER2013 dataset which may not represent all demographics equally
|
| 166 |
+
- Performance may vary with different lighting conditions, angles, and image quality
|
| 167 |
+
- Should not be used as the sole basis for mental health diagnosis
|
| 168 |
+
- Requires frontal face images for best results
|
| 169 |
+
|
| 170 |
+
## Citation
|
| 171 |
+
|
| 172 |
+
If you use this model, please cite:
|
| 173 |
+
|
| 174 |
+
```bibtex
|
| 175 |
+
@misc{fer-mental-health-2024,
|
| 176 |
+
author = {Your Name},
|
| 177 |
+
title = {Facial Expression Recognition for Mental Health Detection},
|
| 178 |
+
year = {2024},
|
| 179 |
+
publisher = {Hugging Face},
|
| 180 |
+
howpublished = {\url{https://huggingface.co/SEARO1/FER_model}}
|
| 181 |
+
}
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
## License
|
| 185 |
+
|
| 186 |
+
MIT License - See LICENSE file for details
|
| 187 |
+
|
| 188 |
+
## Contact
|
| 189 |
+
|
| 190 |
+
For questions or issues, please open an issue on the model repository.
|
| 191 |
+
|
| 192 |
+
---
|
| 193 |
+
|
| 194 |
+
**Developed for educational and research purposes in mental health technology.**
|