Source code of FER2013-Project, also published at https://github.com/WAYTECHG/FER2013-Project
Facial Expression Recognition on FER2013
Custom CNN against three transfer learning models ยท seven expressions ยท live webcam inference in the browser
Overview
This project trains and compares four classifiers for facial expression recognition on FER2013: a custom CNN and three transfer learning models (VGG16, MobileNetV2 and ResNet50). The trained models are exported to ONNX and served through a static browser demo for image uploads and live webcam inference.
| Task | Data | Deployment |
|---|---|---|
| Seven expressions: Angry, Disgust, Fear, Happy, Neutral, Sad, Surprise | FER2013, 7,178 test images | ONNX models executed in the visitor's browser |
Each classifier accepts a 48 ร 48 RGB face crop, normalized by dividing pixel values by 255, and returns seven softmax probabilities.
Live demo
| Host | Link |
|---|---|
| Render | fer2013-project.onrender.com |
| Hugging Face Spaces | Wizzas/Face-Expression-Recognition |
Both hosts serve the same static application. The demo lets you:
- Upload a JPG, JPEG, PNG or WEBP image and select Analyze image.
- Start the webcam to display face boxes, predicted expressions and confidence scores.
- Inspect the seven-class probability profile, and select a face when several are detected.
- Switch between the trained models. Custom CNN is the default.
- Explore the benchmarks, confusion matrices, classification reports, ROC curves and Grad-CAM figures.
Camera permission is required for webcam input. If the embedded Hugging Face Space blocks camera access, use Open full page for camera access in the interface, or open the Render link. Image and camera inference stay in the browser; only contact-form messages are sent out, to Formspree.
Results
| Model | Approach | Reported test accuracy |
|---|---|---|
| Custom CNN | Custom convolutional network | 66.00% |
| ResNet50 | Residual transfer learning | 61.23% |
| MobileNetV2 | Lightweight transfer learning | 60.34% |
| VGG16 | Convolutional transfer learning | 60.13% |
These figures come from the submitted project report (Table 4.2.1, page 60). They are recorded results, not a new evaluation of the deployed browser application.
Evaluation also covers class-wise precision, recall and F1-score, confusion matrices, one-vs-rest ROC/AUC and Grad-CAM visualizations. The saved figures are in project_demo/assets/results/. Grad-CAM is generated by the research notebooks; the browser demo displays the saved figures.
Model export
All four Keras checkpoints were exported to float32 ONNX without retraining. Each export was checked for numerical agreement with the original model on nine inputs; see project_demo/VALIDATION.md.
| Model | ONNX size |
|---|---|
| Custom CNN | 5.25 MB |
| MobileNetV2 | 10.36 MB |
| VGG16 | 60.46 MB |
| ResNet50 | 96.31 MB |
Conversion agreement does not establish a new FER2013 accuracy figure.
Repository structure
| Path | Purpose |
|---|---|
preprocess.py |
Dataset preprocessing and input preparation |
CNN.ipynb |
Custom CNN training and evaluation |
vgg16.ipynb |
VGG16 training and evaluation |
mobilenetV2.ipynb |
MobileNetV2 training and evaluation |
resnet.ipynb |
ResNet50 training and evaluation |
main.py |
Desktop image and webcam inference using OpenCV |
haarcascade_frontalface_default.xml |
Face detector used by the desktop script |
json_files/ |
Saved training histories and test results |
result_images/ |
Saved evaluation figures |
project_demo/ |
Static browser interface and deployment assets |
Run the browser demo locally
From the repository root, run:
py -m http.server 8000 --directory .\project_demo
Open http://localhost:8000 and keep the terminal running. Serve the app over HTTP or HTTPS instead of opening index.html directly. The static demo does not need TensorFlow or a Python inference server.
Keep the complete project_demo folder together, including styles.css, config.js, core.js, vendor/, models/ and assets/. The browser models are .onnx files with matching .json metadata; the original .keras files are not needed for static hosting.
Reproduce the research
The notebooks use TensorFlow/Keras, NumPy, Matplotlib, Seaborn, scikit-learn and tf-keras-vis. The desktop demo also uses OpenCV. Set up a separate environment for research work:
py -3.11 -m venv .venv-research
.\.venv-research\Scripts\python.exe -m pip install tensorflow opencv-python numpy matplotlib seaborn scikit-learn tf-keras-vis
Review and update the dataset and checkpoint paths in the notebooks before running them. main.py contains machine-specific paths that must be changed to your own face detector, CNN_best.keras and test image locations.
Dataset and preprocessing
The project uses the FER2013 dataset, organized into fer2013/train/ and fer2013/test/. Each split contains the class folders angry, disgust, fear, happy, neutral, sad and surprise. Follow the notebooks' validation setup when reproducing training.
All classifiers use RGB values divided by 255. Preserve this preprocessing and the class order when exporting or replacing a model.
The browser demo locates faces with OpenCV YuNet, run through ONNX Runtime Web. The desktop script uses an OpenCV Haar cascade. Their face crops can differ, so predictions need not match on every scene.
Limitations
- FER2013 classifiers predict visual expression categories, not a person's internal emotional state.
- Accuracy depends on lighting, pose, face visibility and how closely the input resembles the training data.
- The reported benchmark accuracy does not guarantee the same performance on webcam footage.
- Browser analysis rates vary by device and model. Custom CNN is the recommended starting point for live use.
References
- FER2013 dataset
- Keras Applications
- Grad-CAM example
- tf-keras-vis
- scikit-learn metrics
- ONNX Runtime Web
- OpenCV YuNet face detector
- Render Static Sites
- Hugging Face Static Spaces
License
This project is distributed under the MIT License. Third-party inference libraries and the bundled face detector keep their own licenses; see project_demo/THIRD_PARTY_NOTICES.md.
- Downloads last month
- 6