SeanHowFun abhilash88 commited on
Commit
ed7fd49
·
0 Parent(s):

Duplicate from abhilash88/face-emotion-detection

Browse files

Co-authored-by: Abhilash Sahoo <abhilash88@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: google/vit-base-patch16-224
4
+ tags:
5
+ - vision
6
+ - image-classification
7
+ - facial-expression-recognition
8
+ - emotion-detection
9
+ - pytorch
10
+ - transformers
11
+ datasets:
12
+ - FER2013
13
+ metrics:
14
+ - accuracy
15
+ pipeline_tag: image-classification
16
+ widget:
17
+ - src: https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=300&h=300&fit=crop&crop=face
18
+ example_title: Happy Face
19
+ - src: https://images.unsplash.com/photo-1457131760772-7017c6180f05?w=300&h=300&fit=crop&crop=face
20
+ example_title: Sad Face
21
+ - src: https://images.unsplash.com/photo-1506794778202-cad84cf45f1d?w=300&h=300&fit=crop&crop=face
22
+ example_title: Serious Face
23
+ ---
24
+
25
+ # 🎭 ViT Facial Expression Recognition
26
+
27
+ This model is a fine-tuned version of [google/vit-base-patch16-224](https://huggingface.co/google/vit-base-patch16-224) for facial expression recognition on the FER2013 dataset.
28
+
29
+ ## 📊 Model Performance
30
+
31
+ - **Accuracy**: 71.55%
32
+ - **Dataset**: FER2013 (35,887 images)
33
+ - **Training Time**: ~20 minutes on GPU
34
+ - **Architecture**: Vision Transformer (ViT-Base)
35
+
36
+ ## 🎯 Supported Emotions
37
+
38
+ The model can classify faces into 7 different emotions:
39
+
40
+ 1. **Angry** 😠
41
+ 2. **Disgust** 🤢
42
+ 3. **Fear** 😨
43
+ 4. **Happy** 😊
44
+ 5. **Sad** 😢
45
+ 6. **Surprise** 😲
46
+ 7. **Neutral** 😐
47
+
48
+ ## 🚀 Quick Start
49
+
50
+ ```python
51
+ from transformers import ViTImageProcessor, ViTForImageClassification
52
+ from PIL import Image
53
+ import torch
54
+
55
+ # Load model and processor
56
+ processor = ViTImageProcessor.from_pretrained('abhilash88/face-emotion-detection')
57
+ model = ViTForImageClassification.from_pretrained('abhilash88/face-emotion-detection')
58
+
59
+ # Load and preprocess image
60
+ image = Image.open('path_to_your_image.jpg')
61
+ inputs = processor(image, return_tensors="pt")
62
+
63
+ # Make prediction
64
+ with torch.no_grad():
65
+ outputs = model(**inputs)
66
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
67
+ predicted_class = torch.argmax(predictions, dim=-1).item()
68
+
69
+ # Emotion classes
70
+ emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
71
+ predicted_emotion = emotions[predicted_class]
72
+ confidence = predictions[0][predicted_class].item()
73
+
74
+ print(f"Predicted Emotion: {predicted_emotion} ({confidence:.2f})")
75
+ ```
76
+
77
+ ## 📸 Example Predictions
78
+
79
+ Here are some example predictions on real faces:
80
+
81
+
82
+ ### Smiling person
83
+ - **True Emotion**: Happy
84
+ - **Predicted**: Happy
85
+ - **Confidence**: 0.85
86
+
87
+ ![Example](examples/example_1_happy.jpg)
88
+
89
+ ### Person looking sad
90
+ - **True Emotion**: Sad
91
+ - **Predicted**: Sad
92
+ - **Confidence**: 0.40
93
+
94
+ ![Example](examples/example_2_sad.jpg)
95
+
96
+ ### Serious expression
97
+ - **True Emotion**: Angry
98
+ - **Predicted**: Neutral
99
+ - **Confidence**: 0.92
100
+
101
+ ![Example](examples/example_3_angry.jpg)
102
+
103
+ ### Surprised expression
104
+ - **True Emotion**: Surprise
105
+ - **Predicted**: Neutral
106
+ - **Confidence**: 0.69
107
+
108
+ ![Example](examples/example_4_surprise.jpg)
109
+
110
+ ### Concerned look
111
+ - **True Emotion**: Fear
112
+ - **Predicted**: Happy
113
+ - **Confidence**: 0.85
114
+
115
+ ![Example](examples/example_5_fear.jpg)
116
+
117
+ ### Neutral expression
118
+ - **True Emotion**: Neutral
119
+ - **Predicted**: Happy
120
+ - **Confidence**: 0.58
121
+
122
+ ![Example](examples/example_6_neutral.jpg)
123
+
124
+ ### Unpleasant expression
125
+ - **True Emotion**: Disgust
126
+ - **Predicted**: Neutral
127
+ - **Confidence**: 0.97
128
+
129
+ ![Example](examples/example_7_disgust.jpg)
130
+
131
+
132
+ ## 🏋️ Training Details
133
+
134
+ ### Training Hyperparameters
135
+ - **Learning Rate**: 5e-5
136
+ - **Batch Size**: 16
137
+ - **Epochs**: 3
138
+ - **Optimizer**: AdamW
139
+ - **Weight Decay**: 0.01
140
+ - **Scheduler**: Linear with warmup
141
+
142
+ ### Training Results
143
+ ```
144
+ Epoch 1: Loss: 0.917, Accuracy: 66.90%
145
+ Epoch 2: Loss: 0.609, Accuracy: 69.32%
146
+ Epoch 3: Loss: 0.316, Accuracy: 71.55%
147
+ ```
148
+
149
+ ### Data Preprocessing
150
+ - **Image Resize**: 224x224 pixels
151
+ - **Normalization**: ImageNet stats
152
+ - **Data Augmentation**:
153
+ - Random horizontal flip
154
+ - Random rotation (±15°)
155
+ - Color jitter
156
+ - Random translation
157
+
158
+ ## 📈 Performance Analysis
159
+
160
+ The model achieves solid performance on FER2013, which is known to be a challenging dataset due to:
161
+ - Low resolution images (48x48 upscaled to 224x224)
162
+ - Crowdsourced labels with some noise
163
+ - High variation in lighting and pose
164
+
165
+ ### Accuracy by Emotion Class:
166
+ - **Happy**: ~86% (best performing)
167
+ - **Surprise**: ~84%
168
+ - **Neutral**: ~83%
169
+ - **Angry**: ~82%
170
+ - **Sad**: ~79%
171
+ - **Fear**: ~75%
172
+ - **Disgust**: ~68% (most challenging)
173
+
174
+ ## 🔧 Technical Details
175
+
176
+ ### Model Architecture
177
+ - **Base Model**: google/vit-base-patch16-224
178
+ - **Parameters**: ~86M
179
+ - **Input Size**: 224x224x3
180
+ - **Patch Size**: 16x16
181
+ - **Number of Layers**: 12
182
+ - **Hidden Size**: 768
183
+ - **Attention Heads**: 12
184
+
185
+ ### Dataset Information
186
+ - **FER2013**: 35,887 grayscale facial images
187
+ - **Training Set**: 28,709 images
188
+ - **Validation Set**: 3,589 images
189
+ - **Test Set**: 3,589 images
190
+ - **Classes**: 7 emotions (balanced evaluation set)
191
+
192
+ ## 💡 Usage Tips
193
+
194
+ 1. **Best Results**: Use clear, front-facing face images
195
+ 2. **Preprocessing**: Ensure faces are properly cropped and centered
196
+ 3. **Lighting**: Good lighting improves accuracy
197
+ 4. **Resolution**: Higher resolution images work better
198
+
199
+ ## 🛠️ Model Limitations
200
+
201
+ - Trained only on FER2013 (limited diversity)
202
+ - May struggle with extreme poses or occlusions
203
+ - Performance varies across different demographics
204
+ - Best suited for clear facial expressions
205
+
206
+ ## 📚 Citation
207
+
208
+ If you use this model, please cite:
209
+
210
+ ```bibtex
211
+ @misc{face-emotion-detection,
212
+ author = {Abhilash},
213
+ title = {ViT Face Emotion Detection},
214
+ year = {2025},
215
+ publisher = {Hugging Face},
216
+ howpublished = {https://huggingface.co/abhilash88/face-emotion-detection}
217
+ }
218
+ ```
219
+
220
+ ## 🤝 Acknowledgments
221
+
222
+ - FER2013 dataset creators
223
+ - Google Research for Vision Transformer
224
+ - Hugging Face for the transformers library
225
+ - The open-source ML community
226
+
227
+ ## 📄 License
228
+
229
+ This model is released under the Apache 2.0 License.
230
+
231
+ ---
232
+
233
+ **Built with ❤️ using Vision Transformers and PyTorch**
config.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ViTForImageClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.0,
6
+ "encoder_stride": 16,
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.0,
9
+ "hidden_size": 768,
10
+ "id2label": {
11
+ "0": "LABEL_0",
12
+ "1": "LABEL_1",
13
+ "2": "LABEL_2",
14
+ "3": "LABEL_3",
15
+ "4": "LABEL_4",
16
+ "5": "LABEL_5",
17
+ "6": "LABEL_6"
18
+ },
19
+ "image_size": 224,
20
+ "initializer_range": 0.02,
21
+ "intermediate_size": 3072,
22
+ "label2id": {
23
+ "LABEL_0": 0,
24
+ "LABEL_1": 1,
25
+ "LABEL_2": 2,
26
+ "LABEL_3": 3,
27
+ "LABEL_4": 4,
28
+ "LABEL_5": 5,
29
+ "LABEL_6": 6
30
+ },
31
+ "layer_norm_eps": 1e-12,
32
+ "model_type": "vit",
33
+ "num_attention_heads": 12,
34
+ "num_channels": 3,
35
+ "num_hidden_layers": 12,
36
+ "patch_size": 16,
37
+ "pooler_act": "tanh",
38
+ "pooler_output_size": 768,
39
+ "problem_type": "single_label_classification",
40
+ "qkv_bias": true,
41
+ "torch_dtype": "float32",
42
+ "transformers_version": "4.53.1"
43
+ }
examples/example_1_happy.jpg ADDED
examples/example_2_sad.jpg ADDED
examples/example_3_angry.jpg ADDED
examples/example_4_surprise.jpg ADDED
examples/example_5_fear.jpg ADDED
examples/example_6_neutral.jpg ADDED
examples/example_7_disgust.jpg ADDED
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f2f521ea028f1aff5d6021c4458c32ba56ff7a0edfb40e94cf1ab5bdf05c8da1
3
+ size 343239356
preprocessor_config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_convert_rgb": null,
3
+ "do_normalize": true,
4
+ "do_rescale": true,
5
+ "do_resize": true,
6
+ "image_mean": [
7
+ 0.5,
8
+ 0.5,
9
+ 0.5
10
+ ],
11
+ "image_processor_type": "ViTImageProcessor",
12
+ "image_std": [
13
+ 0.5,
14
+ 0.5,
15
+ 0.5
16
+ ],
17
+ "resample": 2,
18
+ "rescale_factor": 0.00392156862745098,
19
+ "size": {
20
+ "height": 224,
21
+ "width": 224
22
+ }
23
+ }