This project uses the EfficientNetV2B2 architecture for real-time emotion detection from facial expressions. The model is trained on the Face Expression Recognition (FER) dataset.
- Installation
- Dataset
- Model Training
- Real-time Emotion Detection
- Improving Model Accuracy
- Notes
- License
-
Download the complete project from Google Drive:
-
Clone the repository:
git clone https://github.com/Chorko/Emotion-recognition-using-efficientnet.git cd Emotion-recognition-using-efficientnet
-
Install the required dependencies:
pip install -r requirements.txt
-
Set up a virtual environment (preferred): Setting up a virtual environment helps in managing dependencies and avoiding conflicts with system-installed packages:
python -m venv venv source venv/bin/activate # For Linux/Mac .\venv\Scripts\activate # For Windows
-
Install Git LFS if needed for handling large files:
git lfs install
This project uses the Face Expression Recognition (FER) dataset, which contains images of faces classified into 7 emotions: Angry, Disgust, Fear, Happy, Neutral, Sad, and Surprise.
The dataset is not included in this repository. You need to download it manually:
After downloading the dataset, install it by placing it inside the FER/
directory as follows:
FER/
│
├── images/
│ ├── train/
│ │ ├── angry/
│ │ ├── disgust/
│ │ ├── fear/
│ │ ├── happy/
│ │ ├── neutral/
│ │ ├── sad/
│ │ └── surprise/
│ └── validation/
│ ├── angry/
│ ├── disgust/
│ ├── fear/
│ ├── happy/
│ ├── neutral/
│ ├── sad/
│ └── surprise/
To train the model using EfficientNetV2B2, run the emotion_classification_efficientnet.py
script.
It is recommended to use Google Colab with a T4 GPU for training due to the large dataset size and high computation requirements. You can upload this repository and the dataset to your Colab environment.
- Adjust the paths to the dataset and save directories within the training script as necessary to match your Colab file system.
- Train the model:
python emotion_classification_efficientnet.py
For real-time emotion detection using a webcam, run the main.py
script. This uses OpenCV and a pre-trained model for detecting faces and predicting emotions.
-
First, make sure the Haar Cascade file for face detection is available:
haarcascade_frontalface_default.xml
-
Then, run the script:
python main.py
The pre-trained model best_model.keras
is not included in this repository. You need to train the model or download it separately and place it in the models/
directory:
models/
└── best_model.keras
To improve model accuracy, consider experimenting with the following:
-
Learning Rate:
- Try lowering or increasing the learning rate. A smaller learning rate can help the model converge more precisely but may take longer.
- Example:
learning_rate = 0.001 # or lower like 0.00005
-
Increase Number of Epochs:
- Training for more epochs can allow the model to learn better features from the data.
- Example:
epochs = 30 # or 50 for longer training
-
Early Stopping (min_delta):
- Adjust the
min_delta
parameter in early stopping to prevent the model from stopping too early when improvements are minimal. - Example:
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=6)
- Adjust the
-
Dropout Rates:
- Modify the dropout rates to prevent overfitting.
- Example:
dropout_rate = 0.3 # or increase to 0.4
To speed up training, make sure to use Google Colab’s T4 GPU. You can enable the GPU runtime in Colab by navigating to Runtime > Change runtime type > GPU.
-
Trained model and dataset are excluded: The
FER
dataset andbest_model.keras
file are not included in this repository due to size constraints. You can add these manually following the instructions above. -
Using
.gitignore
: The.gitignore
file has been set to exclude the following:- The FER dataset (
FER/
) - The trained model (
models/best_model.keras
)
- The FER dataset (
This project is licensed under the MIT License. See the LICENSE file for more details.
Emotion_recognition_using_efficientnet/
│
├── emotion_classification_efficientnet.py # Script for training EfficientNet on FER dataset
├── main.py # Script for real-time emotion detection using webcam
├── haarcascade_frontalface_default.xml # Haar Cascade file for face detection
│
├── FER/ # Folder containing FER dataset (excluded in .gitignore)
│ └── images/
│ ├── train/
│ └── validation/
│
├── models/ # Folder for storing trained models (excluded in .gitignore)
│ └── best_model.keras # Trained EfficientNet model (optional)
│
├── README.md # Project documentation and instructions
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore file
└── .gitattributes # Git attributes for handling text and large files