This project demonstrates how to build an image classification model for emotion recognition using TensorFlow and Keras. The model is trained on a dataset of facial expressions and can be converted to different formats for deployment. The notebook includes steps for data preparation, model training, evaluation, and inference.
The project aims to classify facial expressions into six emotion categories:
- Angry
- Happy
- Neutral
- Sad
- Surprise
- Ahegao
The model is built using a pre-trained MobileNetV2 as the base and fine-tuned for emotion recognition. It includes data augmentation, training, evaluation, and conversion to formats like TensorFlow Lite and TensorFlow.js for deployment.
The dataset used for this project is the Emotion Recognition Dataset. It contains images of facial expressions categorized into different emotion classes.
-
Install the required Python packages:
pip install -r requirements.txt
-
Download the dataset: The dataset is downloaded using the
kagglehublibrary in the notebook.
-
Open the notebook
ImageClassification.ipynbin your preferred IDE (e.g., PyCharm or Jupyter Notebook). -
Follow the steps in the notebook:
- Data preparation
- Data augmentation
- Model training
- Evaluation and visualization
- Model conversion
- Inference
-
Run the cells sequentially to train the model and test its performance.
The trained model is converted into the following formats for deployment:
- TensorFlow Lite: Saved in the
tflite_modeldirectory. - TensorFlow.js: Saved in the
tfjs_modeldirectory. - SavedModel: Saved in the
saved_modeldirectory.
- TensorFlow Lite:
converter_tf_lite = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter_tf_lite.convert() with open('tflite_model/model.tflite', 'wb') as f: f.write(tflite_model)
- TensorFlow.js:
tensorflowjs_converter --input_format=keras model.h5 tfjs_model
The notebook includes a sample inference pipeline using the TensorFlow Lite model. To test the model:
- Provide the path to a test image.
- Run the
predict_image_tflitefunction to get the predicted class and probabilities.
Example:
test_image_path = 'data/final_dataset/test/Angry/sample_image.png'
predicted_class, predictions = predict_image_tflite(test_image_path)
print(f"Predicted class: {predicted_class}")The model achieved high accuracy on the test set. Below is an example of the output for a test image:
- Predicted Class: Angry
- Predicted Probabilities:
Ahegao: 0.0000 Angry: 0.9998 Happy: 0.0000 Neutral: 0.0001 Sad: 0.0000 Surprise: 0.0001