This repository contains Python code for training a Convolutional Neural Network (CNN) to recognize emotions from facial images. The training process includes data augmentation for minority classes, model training, evaluation, and visualization of results (accuracy/loss plots and confusion matrix).
- Data Augmentation: Augments the minority class ("disgusted" emotion) to balance the dataset and improve model performance.
- CNN Model: Implements a deep Convolutional Neural Network for emotion classification.
- Training & Validation: Trains the model using augmented data and validates its performance on a separate test set.
- Callbacks: Utilizes
EarlyStoppingandReduceLROnPlateaucallbacks for efficient training. - Performance Metrics: Evaluates the model's accuracy and loss.
- Visualizations: Generates plots for training/validation accuracy and loss, and a confusion matrix to visualize classification performance.
- Model Saving: Saves the trained model in Keras format (
.keras). - Cleanup: Automatically removes temporary augmented data directories after execution.
Before running the code, ensure you have the following installed:
- Python 3.8+
pip(Python package installer)
The code expects a dataset organized into train and test directories, with subdirectories for each emotion class.
The expected directory structure is as follows:
dataset/ ├── train/ │ ├── angry/ │ ├── disgusted/ │ ├── scared/ │ ├── happy/ │ ├── neutral/ │ ├── sad/ │ └── surprised/ └── test/ ├── angry/ ├── disgusted/ ├── scared/ ├── happy/ ├── neutral/ ├── sad/ └── surprised/
Place your dataset in a folder named dataset in the same directory as the train.py script.
-
Clone the repository or download the
train.pyfile. -
Install the required Python packages:
pip install tensorflow numpy matplotlib scikit-learn seaborn
tensorflow: Used for building and training the deep learning model.numpy: Used for numerical operations.matplotlib: Used for plotting graphs and visualizations.scikit-learn: Specifically for computing theconfusion_matrix.seaborn: For enhanced data visualizations, particularly the confusion matrix heatmap.
Note: The script uses
tensorflow.keras, which is included with TensorFlow. Ensure your TensorFlow installation includes Keras.
To train the emotion recognition model, simply run the train.py script from your terminal:
python train.py