--- 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.**