File size: 9,136 Bytes
f329517
7515162
f329517
7515162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f329517
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---

language: en
license: apache-2.0
library_name: tensorflow
tags:
  - tensorflow
  - keras
  - tflite
  - emotion-recognition
  - transformer
  - lstm
  - mediapipe
  - computer-vision
  - deep-learning
  - facial-expression
  - affective-computing
  - sequential-data
model-index:
  - name: emotion_landmark_lstm_model
    results:
      - task:
          type: sequence-classification
        dataset:
          type: dataset
          name: Optimized 478-Point 3D Facial Landmark Dataset
        metrics:
          - name: accuracy
            type: float
            value: 0.7289
    inference: "Supports TensorFlow and TensorFlow Lite real-time inference"
---


# πŸŽ₯ Emotion Sequence Transformer (TensorFlow) β€” Mediapipe 478 Landmarks (Seq256)

**Version:** v1.0  
**Framework:** TensorFlow 2.x  
**Optimized format:** TensorFlow Lite  
**Input:** 478 Mediapipe Face Mesh landmarks per frame (up to 300 frames)  
**Output:** 6-class emotion prediction (`Angry`, `Disgust`, `Fear`, `Happy`, `Neutral`, `Sad`)

---

## 🧠 Model Overview

The **Emotion Sequence Transformer** is a deep learning model built using TensorFlow for recognizing **human emotions** from continuous **video clips**.  
It uses **478 Mediapipe facial landmarks per frame** to capture spatiotemporal patterns of facial movements across time.  
The model predicts one of six basic emotions by analyzing both facial geometry and temporal variation within sequences of up to **300 frames**.

This model is suitable for **real-time video-based emotion detection**, **affective computing**, **human-computer interaction**, and **emotion-aware AI systems**.

---

## πŸ“Š Dataset

This model was trained on the **[Optimized 478-Point 3D Facial Landmark Dataset](https://www.kaggle.com/datasets/psewmuthu/optimized-video-facial-landmarks)** β€”  
a dataset derived from the **Video Emotion Dataset**, optimized for emotion recognition using Mediapipe’s 3D face mesh landmarks.

Each sample in the dataset includes:

- Up to **300 frames per clip**
- **478 facial landmarks per frame**
- Corresponding **emotion label**

---

## 🧩 Model Architecture

The architecture is based on a **Transformer encoder** design that processes sequential data of facial landmarks.

**Pipeline:**

1. Input normalization using precomputed mean and std (global stats)
2. Sequence embedding via positional encodings
3. Transformer encoder blocks to capture temporal and spatial dependencies
4. Dense layers for emotion classification (6 output neurons with softmax)

**Core Components:**

- Transformer Encoder Layers (Multi-Head Self-Attention)
- Layer Normalization and Dropout
- Dense classification head

---

## πŸ“ˆ Performance

| Metric                | Value      |
| --------------------- | ---------- |
| **Test Accuracy**     | 0.7289     |
| **Test Loss**         | 1.1336     |
| **Macro F1-Score**    | 0.73       |
| **Weighted F1-Score** | 0.73       |
| **Max Clip Length**   | 300 frames |
| **Input Shape**       | (300, 478) |

### 🧾 Classification Report

| Emotion              | Precision | Recall | F1-score            | Support |
| -------------------- | --------- | ------ | ------------------- | ------- |
| Angry                | 0.75      | 0.73   | 0.74                | 139     |
| Disgust              | 0.88      | 0.70   | 0.78                | 128     |
| Fear                 | 0.52      | 0.60   | 0.55                | 114     |
| Happy                | 0.88      | 0.97   | 0.92                | 129     |
| Neutral              | 0.66      | 0.79   | 0.72                | 101     |
| Sad                  | 0.70      | 0.58   | 0.64                | 134     |
| **Overall Accuracy** | **0.73**  |        | **Macro Avg: 0.73** | 745     |

---

## πŸ“Š Visualizations

### πŸ”Ή Training Accuracy and Loss

![Accuracy and Loss](images/Accuracies_and_Losses.png)

### πŸ”Ή Confusion Matrix

![Confusion Matrix](images/Confusion_Matrix.png)

### πŸ”Ή ROC Curves (Per Class)

![ROC Curves](images/ROC_Curves.png)

---

## πŸ“‚ Repository Structure

```

TF-Emotion-Sequence-Transformer/

β”œβ”€β”€ tf_emotion_sequence_transformer_mp478_seq256.h5

β”œβ”€β”€ tf_emotion_sequence_transformer_mp478_seq256_optimized.tflite

β”œβ”€β”€ tf_emotion-sequence-transformer-bilstm-usage.ipynb

β”œβ”€β”€ assets/

β”‚   β”œβ”€β”€ global_mean.npy

β”‚   β”œβ”€β”€ global_std.npy

β”‚   β”œβ”€β”€ label_encoder.pkl

β”‚   └── metadata.json

└── README.md

```

### File Descriptions

| File                                                            | Description                                                                                          |
| --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| `tf_emotion_sequence_transformer_mp478_seq256.h5`               | Main TensorFlow model trained on 478 landmarks (300 frames max).                                     |
| `tf_emotion_sequence_transformer_mp478_seq256_optimized.tflite` | Optimized TensorFlow Lite version for deployment (mobile, edge).                                     |
| `tf_emotion-sequence-transformer-bilstm-usage.ipynb`            | Example notebook demonstrating how to use the model for emotion prediction from Mediapipe landmarks. |
| `assets/global_mean.npy`                                        | Precomputed global mean for normalization.                                                           |
| `assets/global_std.npy`                                         | Precomputed global standard deviation for normalization.                                             |
| `assets/label_encoder.pkl`                                      | Encoder mapping integer labels to emotion names.                                                     |
| `assets/metadata.json`                                          | Model metadata and configuration details.                                                            |

---

## πŸš€ Example Usage

### πŸ”Έ TensorFlow (.h5) Model

```python

import numpy as np

import tensorflow as tf

import joblib

import json



# Load Model

model = tf.keras.models.load_model("tf_emotion_sequence_transformer_mp478_seq256.h5")



# Load assets

mean = np.load("assets/global_mean.npy")

std = np.load("assets/global_std.npy")

label_encoder = joblib.load("assets/label_encoder.pkl")



# Preprocess input

input_seq = np.load("example_input.npy")  # shape: (300, 478)

input_seq = (input_seq - mean) / std

input_seq = np.expand_dims(input_seq, axis=0)



# Predict

pred = model.predict(input_seq)

emotion = label_encoder.inverse_transform([np.argmax(pred)])[0]

print("Predicted Emotion:", emotion)

```

---

### πŸ”Έ TensorFlow Lite (Optimized) Model

```python

import numpy as np

import tensorflow as tf

import joblib



# Load TFLite model

interpreter = tf.lite.Interpreter(model_path="tf_emotion_sequence_transformer_mp478_seq256_optimized.tflite")

interpreter.allocate_tensors()



# Get input and output tensors

input_details = interpreter.get_input_details()

output_details = interpreter.get_output_details()



# Load preprocessing assets

mean = np.load("assets/global_mean.npy")

std = np.load("assets/global_std.npy")

label_encoder = joblib.load("assets/label_encoder.pkl")



# Prepare input

input_seq = np.load("example_input.npy")  # shape: (300, 478)

input_seq = (input_seq - mean) / std

input_seq = np.expand_dims(input_seq, axis=0).astype(np.float32)



# Inference

interpreter.set_tensor(input_details[0]['index'], input_seq)

interpreter.invoke()

pred = interpreter.get_tensor(output_details[0]['index'])



# Decode emotion

emotion = label_encoder.inverse_transform([np.argmax(pred)])[0]

print("Predicted Emotion:", emotion)

```

---

## πŸ”– Version Information

**Version:** v1.0  
**Date:** November 2025  
**Author:** [P.S. Abewickrama Singhe](https://www.kaggle.com/psewmuthu)  
**Framework:** TensorFlow 2.x  
**Exported Models:** `.h5`, `.tflite`  
**Landmarks per frame:** 478  
**Max frames per clip:** 300

---

## 🏷️ Tags

`tensorflow` β€’ `emotion-recognition` β€’ `mediapipe` β€’ `transformer` β€’ `sequence-model` β€’ `facial-landmarks` β€’ `video-analysis` β€’ `tflite` β€’ `human-emotion-ai` β€’ `affective-computing` β€’ `computer-vision` β€’ `deep-learning`

---

## πŸ“š Citation

If you use this model in your research, please cite it as:

```bibtex

@misc{pasindu_sewmuthu_abewickrama_singhe_2025,

	author       = { Pasindu Sewmuthu Abewickrama Singhe },

	title        = { EmotionFormer-BiLSTM (Revision f329517) },

	year         = 2025,

	url          = { https://huggingface.co/PSewmuthu/EmotionFormer-BiLSTM },

	doi          = { 10.57967/hf/6899 },

	publisher    = { Hugging Face }

}

```

---

## πŸͺͺ License

This model is released under the **Apache 2.0 License** β€” free for academic and commercial use with attribution.

---