A Deep Learning-based Facial Emotion Classification System using CNN, VGG16, and ResNet50V2 Transfer Learning
Overview โข Features โข Architecture โข Models โข Dataset โข Evaluation โข Deployment โข Installation
This project implements a Deep Learning Facial Emotion Classification system capable of classifying facial expressions into seven emotion categories.
The project explores multiple deep learning approaches, beginning with a custom Convolutional Neural Network (CNN) and progressing toward Transfer Learning using pretrained VGG16 and ResNet50V2 architectures.
The final workflow includes:
- ๐ผ๏ธ Facial image preprocessing
- ๐ Image augmentation
- โ๏ธ Class-weight balancing
- ๐ง Custom CNN architecture
- ๐ VGG16 transfer learning
- โก ResNet50V2 transfer learning
- ๐ Confusion matrix analysis
- ๐ Classification reports
- ๐ ROC curve analysis
- ๐พ Model checkpointing
- ๐งช Model evaluation
- ๐ Gradio-based image prediction interface
The trained ResNet50V2 model is also prepared for deployment, allowing users to upload an image and obtain a predicted facial emotion.
The primary objectives of this project are to:
- Develop a deep learning model for facial emotion classification.
- Compare different CNN-based architectures.
- Investigate the effectiveness of transfer learning.
- Apply image augmentation to improve model generalization.
- Address class imbalance through class weighting.
- Evaluate models using multiple classification metrics.
- Analyze prediction errors using confusion matrices.
- Examine class-wise ROC performance.
- Save trained models for later use.
- Build an interactive prediction interface using Gradio.
The system classifies facial expressions into 7 emotion categories:
| Class | Emotion |
|---|---|
| ๐ | Angry |
| ๐คข | Disgust |
| ๐จ | Fear |
| ๐ | Happy |
| ๐ | Neutral |
| ๐ข | Sad |
| ๐ฒ | Surprise |
0 โ Angry
1 โ Disgust
2 โ Fear
3 โ Happy
4 โ Neutral
5 โ Sad
6 โ Surprise
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Facial Images โ
โ Train / Test โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Image Preprocessing โ
โ Resize + Normalization โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Data Augmentation โ
โ Rotation / Zoom / Shift โ
โ Horizontal Flip โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Class Balancing โ
โ Class Weights โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโโโ
โ CNN โ โ VGG16 โ โ ResNet50V2 โ
โโโโโโฌโโโโ โโโโโโฌโโโโโ โโโโโโโฌโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Evaluation โ
โ Accuracy / Confusion โ
โ Matrix / Classification โ
โ Report / ROC Analysis โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Trained Model (.keras)โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Gradio Interface โ
โ Upload Image โ โ
โ Emotion Prediction โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
The notebook experiments with three major approaches.
The project first develops a custom convolutional neural network containing multiple convolutional blocks.
The architecture includes:
Conv2D- ReLU activation
- Batch Normalization
- Max Pooling
- Dropout
- L2 regularization
- Fully connected layers
- Softmax output layer
The CNN processes facial images at 48 ร 48 resolution with a grayscale input configuration.
Input Image
โ
โผ
Conv2D 32
โ
โผ
Conv2D 64
โ
โผ
Batch Normalization
โ
โผ
Max Pooling
โ
โผ
Dropout
โ
โผ
Conv2D 128
โ
โผ
Conv2D 256
โ
โผ
Batch Normalization
โ
โผ
Max Pooling
โ
โผ
Conv2D 512
โ
โผ
Classification
The second major approach uses VGG16 pretrained on ImageNet.
Images are resized to:
224 ร 224 ร 3
The pretrained VGG16 convolutional base is used as a feature extractor, with selected layers made trainable for fine-tuning.
VGG16
โ
โผ
Flatten
โ
โผ
Dense 1024 + ReLU
โ
โผ
Dropout 0.5
โ
โผ
Dense 512 + ReLU
โ
โผ
Dropout 0.5
โ
โผ
Dense 7 + Softmax
The model uses:
- ImageNet pretrained weights
- Fine-tuning
- Adam optimizer
- Categorical cross-entropy
- Dropout regularization
- Class weighting
The project also implements ResNet50V2 using ImageNet pretrained weights.
The model is partially fine-tuned by freezing earlier layers and allowing the final portion of the network to train.
Input Image
โ
โผ
ResNet50V2
โ
โผ
Dropout 0.25
โ
โผ
Batch Normalization
โ
โผ
Flatten
โ
โผ
Dense 64 + ReLU
โ
โผ
Batch Normalization
โ
โผ
Dropout 0.5
โ
โผ
Dense 7 + Softmax
โ
โผ
Emotion Prediction
The notebook saves the trained model as:
ResNet50_Transfer_Learning.keras
and also creates:
Resnet_model_version_2.keras
The repository contains separate training and testing directories.
๐ฆ Project
โ
โโโ ๐ train/
โ โโโ angry/
โ โโโ disgust/
โ โโโ fear/
โ โโโ happy/
โ โโโ neutral/
โ โโโ sad/
โ โโโ surprise/
โ
โโโ ๐ test/
โ โโโ angry/
โ โโโ disgust/
โ โโโ fear/
โ โโโ happy/
โ โโโ neutral/
โ โโโ sad/
โ โโโ surprise/
โ
โโโ ๐ Deep Learning Model for Facial Emotion Classification.ipynb
โโโ ๐ README.md
The notebook uses directory-based image loading with Keras ImageDataGenerator.
The project performs several preprocessing operations before model training.
For the custom CNN:
48 ร 48
For transfer-learning models:
224 ร 224
Pixel values are rescaled from:
0โ255
to:
0โ1
using:
rescale = 1 / 255.Training images are augmented using transformations such as:
| Augmentation | Configuration |
|---|---|
| Rotation | ยฑ10ยฐ |
| Zoom | 0.2 |
| Width Shift | 0.1 |
| Height Shift | 0.1 |
| Horizontal Flip | Enabled |
| Fill Mode | Nearest |
The purpose is to expose the model to variations in facial appearance and reduce dependence on exact training-image configurations.
The notebook calculates balanced class weights using:
compute_class_weight(
class_weight='balanced'
)The resulting weights are supplied during model training:
class_weight=class_weights_dictThis gives the training process a mechanism to account for differences in the number of images available for each emotion class.
The project uses several training techniques to improve model training and generalization.
โ Image Augmentation
โ Class Weighting
โ Transfer Learning
โ Fine-Tuning
โ Dropout
โ Batch Normalization
โ L2 Regularization
โ Early Stopping
โ Learning Rate Reduction
โ Model Checkpointing
โ Training Log Generation
The transfer-learning experiments use callbacks including:
Saves the best-performing model according to validation loss.
Stops training when monitored validation performance does not improve according to the configured patience.
Reduces the learning rate when validation performance stops improving.
Stores training information in a log file.
The project does not rely exclusively on accuracy.
Multiple evaluation techniques are implemented:
Measures the proportion of correctly classified samples.
The notebook generates class-level:
- Precision
- Recall
- F1-score
- Support
The confusion matrix provides a class-by-class view of:
Actual Emotion
โ
Predicted Emotion
This helps identify which emotions are frequently confused by the model.
The project also calculates ROC curves and AUC values for the seven emotion classes.
Trained Model
โ
โผ
Test Dataset
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
Accuracy Confusion Classification
Matrix Report
โ โ โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ
โผ
ROC Analysis
The notebook follows an incremental deep learning workflow:
Facial Emotion Dataset
โ
โผ
Custom CNN Model
โ
โผ
Model Evaluation
โ
โผ
Data Augmentation
โ
โผ
VGG16 Transfer
โ
โผ
VGG Evaluation
โ
โผ
ResNet50V2 Transfer
โ
โผ
Fine-Tuning + Evaluation
โ
โผ
Saved Keras Model
โ
โผ
Gradio Deployment
The project includes an interactive Gradio prediction workflow.
The saved ResNet50V2 model is loaded and used for inference.
๐ท User Image
โ
โผ
Image Preprocessing
โ
โผ
Resize 224ร224
โ
โผ
Normalize Pixel Values
โ
โผ
ResNet50V2 Model
โ
โผ
Softmax Probabilities
โ
โผ
Highest Probability Class
โ
โผ
๐ Emotion Prediction
The deployment code maps the model's numerical output back to the corresponding emotion label.
The notebook prepares a Gradio-based interface where a user can upload an image.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ง FACIAL EMOTION AI โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ ๐ท Upload Facial Image โ
โ โ
โ [ Upload ] โ
โ โ
โ โ โ
โ โ
โ ๐ค Deep Learning Model โ
โ โ
โ โ โ
โ โ
โ ๐ฏ Predicted Emotion โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Technology | Purpose |
|---|---|
| ๐ Python | Programming Language |
| ๐ง TensorFlow | Deep Learning Framework |
| ๐ฅ Keras | Neural Network Development |
| ๐๏ธ OpenCV | Computer Vision |
| ๐ผ๏ธ PIL | Image Processing |
| ๐ NumPy | Numerical Computing |
| ๐ผ Pandas | Data Processing |
| ๐ Matplotlib | Visualization |
| ๐จ Seaborn | Statistical Visualization |
| ๐ค Gradio | Interactive Deployment |
| โ๏ธ Google Colab | Model Development Environment |
๐ฆ Facial-Emotion-Classification
โ
โโโ ๐ train/
โ โโโ angry/
โ โโโ disgust/
โ โโโ fear/
โ โโโ happy/
โ โโโ neutral/
โ โโโ sad/
โ โโโ surprise/
โ
โโโ ๐ test/
โ โโโ angry/
โ โโโ disgust/
โ โโโ fear/
โ โโโ happy/
โ โโโ neutral/
โ โโโ sad/
โ โโโ surprise/
โ
โโโ ๐ Deep Learning Model for Facial Emotion Classification.ipynb
โ
โโโ ๐ง ResNet50_Transfer_Learning.keras
โ
โโโ ๐ง Resnet_model_version_2.keras
โ
โโโ ๐ README.md
Note: Keep the actual repository structure synchronized with the files you have uploaded to GitHub. The model files above are included in the README structure only if they are present in your repository.
Clone the repository:
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.gitMove into the project directory:
cd YOUR-REPOSITORYInstall the required libraries:
pip install tensorflow
pip install numpy pandas matplotlib seaborn
pip install opencv-python pillow
pip install scikit-learn
pip install gradioUpload the notebook to Google Colab and make sure the dataset directories are available at the paths expected by the notebook.
The notebook currently uses paths such as:
/content/train
/content/test
Run the notebook cells sequentially.
Place the dataset folders in the project directory:
project/
โโโ train/
โโโ test/
โโโ notebook.ipynb
Then update the dataset paths in the notebook if required.
The notebook demonstrates several important deep learning concepts:
โโโโโโโโโโโโโโโโโโโโโโโ
โ Facial Image Data โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโ
โผ โผ โผ
Preprocessing Augmentation Class Weighting
โ โ โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Deep Learning โ
โ Model Training โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โผ โผ โผ
CNN VGG16 ResNet50V2
โ โ โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โผ
Evaluation
โ
โผ
Deployment
This project demonstrates practical experience with:
- Convolutional Neural Networks
- Transfer Learning
- Image Classification
- Fine-Tuning pretrained networks
- TensorFlow/Keras
- Image augmentation
- Class imbalance handling
- Model regularization
- Model checkpointing
- Early stopping
- Learning-rate scheduling
- Classification metrics
- Confusion matrix interpretation
- ROC/AUC analysis
- Model serialization
- Interactive AI deployment
Input โ 48ร48ร1
Input โ 224ร224ร3
7
Softmax
Categorical Cross-Entropy
Adam
| Evaluation Component | Implemented |
|---|---|
| Training Accuracy | โ |
| Test/Validation Accuracy | โ |
| Precision | โ |
| Recall | โ |
| F1-Score | โ |
| Confusion Matrix | โ |
| Classification Report | โ |
| ROC Curve | โ |
| AUC | โ |
| Class Weighting | โ |
| Data Augmentation | โ |
| Model Checkpointing | โ |
| ๐ง Models | ๐ Classes | ๐ผ๏ธ Input | ๐ Deployment |
|---|---|---|---|
| CNN โข VGG16 โข ResNet50V2 | 7 | 48ร48 / 224ร224 | Gradio |
Potential future extensions include:
- Real-time webcam emotion recognition
- Face detection before emotion classification
- More extensive hyperparameter optimization
- Cross-validation experiments
- Additional pretrained architectures
- Ensemble learning
- Model explainability using Grad-CAM
- Confidence-score visualization
- Improved handling of minority emotion classes
- Mobile or web application deployment
- API-based inference
- Model quantization for lightweight deployment
The notebook represents an experimental deep learning workflow and includes multiple model configurations.
Some implementation choices are environment-specific, particularly the Google Colab paths and Google Drive model-storage paths.
The test directory is used as validation/evaluation data in parts of the notebook, so users should distinguish between validation during model development and a completely independent held-out test set when reporting final experimental results.
MS Computer Science Student Specialization in Artificial Intelligence
Institute of Management Sciences, Peshawar, Pakistan
I enjoy building complete software systemsโfrom networking and operating system concepts to backend architecture, machine learning, and distributed systems.
- ๐ Degree / Field: MS in Computer Science (Specialization in Artificial Intelligence)
- ๐ Achievements: Designed and executed 30+ machine learning and data visualization projects delivering actionable insights
- ๐ฏ Focus Areas: Data Visualization, AI/ML, Big Data Mining
- ๐ฌ Contact: shahidazam2020@gmail.com
WhatsApp: Chat on WhatsApp
If you find this project useful for learning or experimentation, consider giving the repository a โญ.
โญ Star this repository โข ๐ด Fork it โข ๐ง Explore the code โข ๐ Build something new
This project is intended for educational, research, and experimental purposes.
If you reuse or extend this project, please provide appropriate attribution to the original work and dataset sources where applicable.
Built with Python, TensorFlow & Computer Vision
If you find this repository useful for learning Machine Learning, consider giving it a โญ.
Your feedback, suggestions, and contributions are welcome.
๐ง Learn Machine Learning โข ๐ป Build Models โข ๐ Analyze Data โข ๐ Create Projects
Made with โค๏ธ for learning, experimentation, and continuous improvement.