File size: 5,389 Bytes
67f28f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 194 195 |
---
language: en
license: mit
tags:
- facial-expression-recognition
- emotion-detection
- mental-health
- swin-transformer
- pytorch
- computer-vision
datasets:
- FER2013
metrics:
- accuracy
- f1-score
library_name: pytorch
---
# Facial Expression Recognition for Mental Health Detection
## Model Description
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.
### Model Architecture
- **Base Model**: Swin Transformer (swin_base_patch4_window7_224)
- **Custom Classifier**:
- Linear layer (backbone features β 512)
- ReLU activation
- Dropout (p=0.6)
- Linear layer (512 β 7 classes)
### Emotion Classes
The model predicts 7 facial expressions:
1. **Angry** π
2. **Disgust** π€’
3. **Fear** π¨
4. **Happy** π
5. **Neutral** π
6. **Sad** π’
7. **Surprise** π²
## Training Details
### Dataset
- **Name**: FER2013 (Facial Expression Recognition 2013)
- **Size**: ~35,000 grayscale images (48x48 pixels)
- **Split**: Train/Validation/Test
### Training Configuration
- **Optimizer**: AdamW
- **Learning Rate**: 1e-4 with cosine annealing
- **Batch Size**: 32
- **Epochs**: 5
- **Image Size**: 224x224
- **Data Augmentation**: Random horizontal flip, rotation, color jitter
- **Loss Function**: Cross-Entropy Loss
## Usage
### Installation
```bash
pip install torch torchvision timm huggingface_hub
```
### Load Model
```python
import torch
import timm
from huggingface_hub import hf_hub_download
class CustomSwinTransformer(torch.nn.Module):
def __init__(self, pretrained=True, num_classes=7):
super(CustomSwinTransformer, self).__init__()
self.backbone = timm.create_model('swin_base_patch4_window7_224',
pretrained=pretrained, num_classes=0)
self.classifier = torch.nn.Sequential(
torch.nn.Linear(self.backbone.num_features, 512),
torch.nn.ReLU(),
torch.nn.Dropout(p=0.6),
torch.nn.Linear(512, num_classes)
)
def forward(self, x):
x = self.backbone(x)
return self.classifier(x)
# Download and load model
model_path = hf_hub_download(repo_id="SEARO1/FER_model", filename="best_model.pth")
model = CustomSwinTransformer(pretrained=False, num_classes=7)
model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=False)
model.eval()
```
### Inference Example
```python
from torchvision import transforms
from PIL import Image
# Prepare image
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = Image.open("face.jpg").convert("RGB")
input_tensor = transform(image).unsqueeze(0)
# Predict
with torch.no_grad():
output = model(input_tensor)
probabilities = torch.nn.functional.softmax(output, dim=1)
predicted_class = torch.argmax(probabilities, dim=1)
emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
print(f"Predicted Emotion: {emotions[predicted_class.item()]}")
print(f"Confidence: {probabilities[0][predicted_class].item()*100:.2f}%")
```
## Mental Health Application
This model can be used for depression risk analysis by analyzing emotional patterns:
### Depression Risk Calculation
```python
def analyze_depression_risk(emotion_probs):
sad_score = emotion_probs[5] # Sad
fear_score = emotion_probs[2] # Fear
angry_score = emotion_probs[0] # Angry
happy_score = emotion_probs[3] # Happy
negative_emotions = (sad_score * 0.4 + fear_score * 0.3 + angry_score * 0.3)
positive_emotions = happy_score
depression_risk = (negative_emotions * 100) - (positive_emotions * 20)
depression_risk = max(0, min(100, depression_risk))
if depression_risk < 30:
return "Low Risk"
elif depression_risk < 60:
return "Moderate Risk"
else:
return "High Risk"
```
β οΈ **Important**: This is an educational tool and should NOT replace professional medical advice or diagnosis.
## Performance
The model achieves competitive performance on the FER2013 dataset. See the training logs for detailed metrics.
## Limitations
- Trained on FER2013 dataset which may not represent all demographics equally
- Performance may vary with different lighting conditions, angles, and image quality
- Should not be used as the sole basis for mental health diagnosis
- Requires frontal face images for best results
## Citation
If you use this model, please cite:
```bibtex
@misc{fer-mental-health-2024,
author = {Your Name},
title = {Facial Expression Recognition for Mental Health Detection},
year = {2024},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/SEARO1/FER_model}}
}
```
## License
MIT License - See LICENSE file for details
## Contact
For questions or issues, please open an issue on the model repository.
---
**Developed for educational and research purposes in mental health technology.**
|