File size: 7,809 Bytes
0997ed2
58ef897
0997ed2
58ef897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29bc84a
 
 
91777af
29bc84a
58ef897
 
 
 
 
0997ed2
5c1001f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---

language: en
license: apache-2.0
library_name: tensorflow
tags:
  - tensorflow
  - keras
  - emotion-recognition
  - vgg19
  - ckplus
  - rafdb
  - fine-tuning
  - computer-vision
  - deep-learning
  - facial-expression
  - affective-computing
  - tflite
model-index:
  - name: emotion_vgg19_model
    results:
      - task:
          type: image-classification
        dataset:
          type: dataset
          name: CK+ & RAF-DB
        metrics:
          - name: accuracy
            type: float
            value: 0.7751
    inference: "Supports TensorFlow and TensorFlow Lite inference"
---


# 🧠 Emotion Recognition Model – VGG19 (Fine-Tuned on CK+ and RAF-DB)

## πŸ“˜ Overview

This repository provides a fine-tuned **VGG19-based Emotion Recognition model** trained using a combination of **CK+** and **RAF-DB** datasets. The model is designed to classify human facial emotions into seven categories and has been optimized for both performance and size (TensorFlow and TensorFlow Lite versions available).

The model is a key module in a broader AI system for **emotion-aware human-computer interaction**, providing robust real-time emotion inference.

---

## 🧩 Model Architecture

The model is based on **VGG19** pre-trained on ImageNet, and fine-tuned in **two stages**:

1. **Stage 1 – Frozen Base Training (10 Epochs):**

   - The convolutional base (VGG19) was frozen.
   - Only newly added dense layers were trained.
   - Purpose: Train classifier layers without disrupting pre-trained features.

2. **Stage 2 – Unfrozen Base Fine-Tuning (30 Epochs):**
   - The base model was unfrozen and fine-tuned with a low learning rate.
   - Purpose: Enhance generalization and feature learning for emotion-specific characteristics.

---

## πŸ“Š Datasets

Two publicly available datasets were used for training and evaluation:

1. **[CK+ Dataset (Kaggle)](https://www.kaggle.com/datasets/shareef0612/ckdataset)**
2. **[RAF-DB Dataset (Kaggle)](https://www.kaggle.com/datasets/shuvoalok/raf-db-dataset)**

### Dataset Preparation

- Combined both datasets for richer emotion diversity.
- Applied **class balancing** through **image augmentation** (rotation, flips, brightness, zoom, and shift).
- Final dataset distribution was uniform across emotion classes.

---

## βš™οΈ Training Configuration

| Parameter         | Description                     |
| ----------------- | ------------------------------- |
| **Base Model**    | VGG19 (Pre-trained on ImageNet) |
| **Optimizer**     | Adam                            |
| **Learning Rate** | 1e-4 (unfrozen phase)           |
| **Loss Function** | Sparse Categorical Crossentropy |
| **Batch Size**    | 32                              |
| **Epochs**        | 40 (10 + 30)                    |
| **Image Size**    | 224x224                         |

---

## πŸ“ˆ Performance Summary

| Metric       | Training | Validation | Testing |
| ------------ | -------- | ---------- | ------- |
| **Accuracy** | 97.42%   | 81.26%     | 77.51%  |
| **Loss**     | 0.0910   | 1.0053     | 1.4182  |

### Classification Report

| Class | Precision | Recall | F1-Score |
| ----- | --------- | ------ | -------- |
| 0     | 0.72      | 0.65   | 0.68     |
| 1     | 0.39      | 0.46   | 0.42     |
| 2     | 0.46      | 0.51   | 0.49     |
| 3     | 0.95      | 0.84   | 0.89     |
| 4     | 0.67      | 0.89   | 0.76     |
| 5     | 0.79      | 0.67   | 0.72     |
| 6     | 0.83      | 0.74   | 0.78     |

**Overall Accuracy:** 77.51%  
**Weighted F1-Score:** 0.78

---

## πŸ–ΌοΈ Visualizations

### 1. Training Accuracy and Loss

- **Graph 1:** Training and Validation Accuracy vs Epochs
  ![Training Accuracy](images/Accuracies.png)
  _Training and validation accuracy over 40 epochs._

- **Graph 2:** Training and Validation Loss vs Epochs
  ![Training Loss](images/Losses.png)
  _Training and validation loss over 40 epochs._

### 2. Dataset Distributions

- **Graph 3:** Original Dataset Class Distribution
  ![Original Dataset Distribution](images/Original_Class_Distribution.png)
  _Class distribution of the original combined dataset._

- **Graph 4:** Balanced (Augmented) Dataset Class Distribution
  ![Balanced Dataset Distribution](images/Balanced_Class_Distribution.png)
  _Class distribution after augmentation and balancing._

### 3. Evaluation Visuals

- **Graph 5:** Multi-Class ROC Curves (AUC per class)
  ![ROC Curves](images/ROC.png)
  _Multi-class ROC curves with AUC values._

- **Graph 6:** Confusion Matrix (Heatmap)
  ![Confusion Matrix](images/Confusion_Matrix.png)
  _Confusion matrix heatmap on test set._

- **Graph 7:** Sample Test Results (Subplots of 5 predictions per class)
  ![Sample Test Results](images/Sample_Test_Results.png)
  _Sample predictions (5 images per class) showing model performance._

These visualizations clearly demonstrate model learning stability, class balance, and classification performance across emotions.

---

## 🧩 Model Files

| File                             | Description                                                        |
| -------------------------------- | ------------------------------------------------------------------ |
| `emotion_vgg19_model.h5`         | Original fine-tuned TensorFlow model (β‰ˆ230 MB)                     |
| `emotion_vgg19_optimized.tflite` | Optimized TensorFlow Lite model (β‰ˆ19.2 MB) for mobile/edge devices |

---

## 🧰 Inference Example

```python

import tensorflow as tf

from tensorflow.keras.preprocessing import image

import numpy as np



# Load original model

model_path = 'emotion_vgg19_model.h5'

model = tf.keras.models.load_model(model_path)



# Prepare input

img = image.load_img('test_face.jpg', target_size=(224, 224))

input_data = np.expand_dims(image.img_to_array(img) / 255.0, axis=0)



# Run inference with original model

pred = model.predict(input_data)

classes = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']

print("Original Model Prediction:", classes[np.argmax(pred)])



# Load TFLite optimized model

tflite_model_path = 'emotion_vgg19_optimized.tflite'

interpreter = tf.lite.Interpreter(model_path=tflite_model_path)

interpreter.allocate_tensors()

input_index = interpreter.get_input_details()[0]['index']

output_index = interpreter.get_output_details()[0]['index']

interpreter.set_tensor(input_index, input_data.astype(np.float32))

interpreter.invoke()

output = interpreter.get_tensor(output_index)

print("TFLite Model Prediction:", classes[np.argmax(output)])

```

---

## πŸš€ Key Features

- Dual-dataset fine-tuning (CK+ + RAF-DB)
- Balanced training set with augmentation
- Strong generalization (77.5% test accuracy)
- Mobile-optimized TFLite version (19.2 MB)
- Suitable for real-time emotion-aware applications

---

## 🏷️ Tags

`emotion-recognition` `vgg19` `facial-expression` `deep-learning` `tensorflow` `tflite` `ckplus` `rafdb` `computer-vision` `affective-computing` `multimodal-ai` `fine-tuning`

---

## πŸ“„ Citation

```bibtex

@misc{pasindu_sewmuthu_abewickrama_singhe_2025,

	author       = { Pasindu Sewmuthu Abewickrama Singhe },

	title        = { vgg19-emotion-recognition-ckplus-rafdb (Revision dad246e) },

	year         = 2025,

	url          = { https://huggingface.co/PSewmuthu/vgg19-emotion-recognition-ckplus-rafdb },

	doi          = { 10.57967/hf/6651 },

	publisher    = { Hugging Face }

}

```

---

## πŸ‘€ Author & Model Info

**Author:** P.S. Abewickrama Singhe  
**Developed with:** TensorFlow + Keras  
**License:** Apache-2.0  
**Date:** October 2025  
**Email:** [[email protected]](mailto:[email protected])