CoolFace
Modelpublic

abhilash88/face-emotion-detection

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
2likes634downloads
Model Card

๐ŸŽญ ViT Facial Expression Recognition

This model is a fine-tuned version of google/vit-base-patch16-224 for facial expression recognition on the FER2013 dataset.

๐Ÿ“Š Model Performance

  • โ€”Accuracy: 71.55%
  • โ€”Dataset: FER2013 (35,887 images)
  • โ€”Training Time: ~20 minutes on GPU
  • โ€”Architecture: Vision Transformer (ViT-Base)

๐ŸŽฏ Supported Emotions

The model can classify faces into 7 different emotions:

  1. 1.Angry ๐Ÿ˜ 
  2. 2.Disgust ๐Ÿคข
  3. 3.Fear ๐Ÿ˜จ
  4. 4.Happy ๐Ÿ˜Š
  5. 5.Sad ๐Ÿ˜ข
  6. 6.Surprise ๐Ÿ˜ฒ
  7. 7.Neutral ๐Ÿ˜

๐Ÿš€ Quick Start

python
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import torch

# Load model and processor
processor = ViTImageProcessor.from_pretrained('abhilash88/face-emotion-detection')
model = ViTForImageClassification.from_pretrained('abhilash88/face-emotion-detection')

# Load and preprocess image
image = Image.open('path_to_your_image.jpg')
inputs = processor(image, return_tensors="pt")

# Make prediction
with torch.no_grad():
    outputs = model(**inputs)
    predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
    predicted_class = torch.argmax(predictions, dim=-1).item()

# Emotion classes
emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
predicted_emotion = emotions[predicted_class]
confidence = predictions[0][predicted_class].item()

print(f"Predicted Emotion: {predicted_emotion} ({confidence:.2f})")

๐Ÿ“ธ Example Predictions

Here are some example predictions on real faces:

Smiling person

  • โ€”True Emotion: Happy
  • โ€”Predicted: Happy
  • โ€”Confidence: 0.85

[image]

Person looking sad

  • โ€”True Emotion: Sad
  • โ€”Predicted: Sad
  • โ€”Confidence: 0.40

[image]

Serious expression

  • โ€”True Emotion: Angry
  • โ€”Predicted: Neutral
  • โ€”Confidence: 0.92

[image]

Surprised expression

  • โ€”True Emotion: Surprise
  • โ€”Predicted: Neutral
  • โ€”Confidence: 0.69

[image]

Concerned look

  • โ€”True Emotion: Fear
  • โ€”Predicted: Happy
  • โ€”Confidence: 0.85

[image]

Neutral expression

  • โ€”True Emotion: Neutral
  • โ€”Predicted: Happy
  • โ€”Confidence: 0.58

[image]

Unpleasant expression

  • โ€”True Emotion: Disgust
  • โ€”Predicted: Neutral
  • โ€”Confidence: 0.97

[image]

๐Ÿ‹๏ธ Training Details

Training Hyperparameters

  • โ€”Learning Rate: 5e-5
  • โ€”Batch Size: 16
  • โ€”Epochs: 3
  • โ€”Optimizer: AdamW
  • โ€”Weight Decay: 0.01
  • โ€”Scheduler: Linear with warmup

Training Results

Epoch 1: Loss: 0.917, Accuracy: 66.90%
Epoch 2: Loss: 0.609, Accuracy: 69.32% 
Epoch 3: Loss: 0.316, Accuracy: 71.55%

Data Preprocessing

  • โ€”Image Resize: 224x224 pixels
  • โ€”Normalization: ImageNet stats
  • โ€”Data Augmentation:
  • โ€”Random horizontal flip
  • โ€”Random rotation (ยฑ15ยฐ)
  • โ€”Color jitter
  • โ€”Random translation

๐Ÿ“ˆ Performance Analysis

The model achieves solid performance on FER2013, which is known to be a challenging dataset due to:

  • โ€”Low resolution images (48x48 upscaled to 224x224)
  • โ€”Crowdsourced labels with some noise
  • โ€”High variation in lighting and pose

Accuracy by Emotion Class:

  • โ€”Happy: ~86% (best performing)
  • โ€”Surprise: ~84%
  • โ€”Neutral: ~83%
  • โ€”Angry: ~82%
  • โ€”Sad: ~79%
  • โ€”Fear: ~75%
  • โ€”Disgust: ~68% (most challenging)

๐Ÿ”ง Technical Details

Model Architecture

  • โ€”Base Model: google/vit-base-patch16-224
  • โ€”Parameters: ~86M
  • โ€”Input Size: 224x224x3
  • โ€”Patch Size: 16x16
  • โ€”Number of Layers: 12
  • โ€”Hidden Size: 768
  • โ€”Attention Heads: 12

Dataset Information

  • โ€”FER2013: 35,887 grayscale facial images
  • โ€”Training Set: 28,709 images
  • โ€”Validation Set: 3,589 images
  • โ€”Test Set: 3,589 images
  • โ€”Classes: 7 emotions (balanced evaluation set)

๐Ÿ’ก Usage Tips

  1. 1.Best Results: Use clear, front-facing face images
  2. 2.Preprocessing: Ensure faces are properly cropped and centered
  3. 3.Lighting: Good lighting improves accuracy
  4. 4.Resolution: Higher resolution images work better

๐Ÿ› ๏ธ Model Limitations

  • โ€”Trained only on FER2013 (limited diversity)
  • โ€”May struggle with extreme poses or occlusions
  • โ€”Performance varies across different demographics
  • โ€”Best suited for clear facial expressions

๐Ÿ“š Citation

If you use this model, please cite:

bibtex
@misc{face-emotion-detection,
  author = {Abhilash},
  title = {ViT Face Emotion Detection},
  year = {2025},
  publisher = {Hugging Face},
  howpublished = {https://huggingface.co/abhilash88/face-emotion-detection}
}

๐Ÿค Acknowledgments

  • โ€”FER2013 dataset creators
  • โ€”Google Research for Vision Transformer
  • โ€”Hugging Face for the transformers library
  • โ€”The open-source ML community

๐Ÿ“„ License

This model is released under the Apache 2.0 License.


Built with โค๏ธ using Vision Transformers and PyTorch