Facial Expression Recognition with PyTorch: FER-2013 and 72% Validation Accuracy

Facial expression recognition model with PyTorch on FER-2013, ~72% validation accuracy, efficient CNN and deployment on AWS/Azure for real-time inference.

lunes, 18 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

Abstract: In this article, I explain how we built a facial expression recognition model with PyTorch on the FER-2013 dataset and achieved around 72% validation accuracy. The goal was to achieve a reproducible and efficient solution for real-time inference, considering deployments on AWS and Azure cloud services and custom applications.

Introduction: FER-2013 is a realistic dataset with 48x48 grayscale images and strong class imbalance. The main challenges are low resolution, label noise, and intra-class variability. Despite this, a compact architecture and good augmentation techniques allow for solid results for enterprise AI solutions.

Preprocessing and augmentation: For training, we applied common transformations: conversion to PIL image, random horizontal flip, small rotations up to 10 degrees, cropping and resizing to 48x48, and normalization with appropriate mean and standard deviation. For validation, only resizing and normalization. These transformations help generalization and are compatible with production pipelines and cloud inference services.

Model architecture: We designed a CNN with 3 blocks. Input 48x48x1. Block 1: Conv 3x3 64 filters, BatchNorm, ReLU, MaxPool 2x2 produces 24x24x64. Block 2: Conv 3x3 128 filters, BatchNorm, ReLU, MaxPool 2x2 produces 12x12x128. Block 3: Conv 3x3 256 filters, BatchNorm, ReLU, MaxPool 2x2 produces 6x6x256. Dropout2d 0.25, flatten to 9216, FC to 512, ReLU, Dropout 0.5, and FC output of 7 neurons with softmax at inference. This topology balances capacity and efficiency for real-time inference and edge or cloud service deployments.

Training recipe: CrossEntropyLoss, AdamW optimizer with lr 1e-3 and weight decay 1e-4, ReduceLROnPlateau or CosineAnnealingLR scheduler. Batch size 64, number of epochs between 30 and 60 with early stopping and patience 7 on validation loss. We save the best checkpoint by macro F1 metric or by validation loss.

Reproducibility and best practices: We set seeds for random, numpy, and torch and configure cudnn deterministic False or True as needed to balance reproducibility and performance. We record per-epoch metrics, confusion matrix, and per-class F1 to understand errors and classes with the worst performance.

Evaluation and results: In addition to overall validation accuracy close to 72%, we report confusion matrix and per-class F1 to measure sensitivity on emotions such as anger, disgust, fear, happiness, sadness, surprise, and neutral. These indicators are key when integrating the model into business solutions where each class has different value.

Deployment and next steps: The model can be adapted for real-time inference with webcam, integrated into an API with Django or Flask, or deployed on AWS and Azure cloud services using containers and managed endpoints for scaling. Other improvements include fine-tuning with pretrained models, pruning, quantization, and face detection pipelines for robust preprocessing.

About Q2BSTUDIO: Q2BSTUDIO is a software development and custom applications company specialized in artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, and customized solutions. We offer custom software, custom applications, artificial intelligence consulting and enterprise AI, AI agent development, Power BI integrations, and business intelligence solutions to turn data into value. If you are looking for a turnkey solution for facial recognition, cloud deployment, or integration with corporate systems, our team can help you take the prototype to production with security and scalability practices.

Contact and collaboration: If you want to adapt this project to a real case, integrate models into products, develop custom software, or hire cybersecurity services, AWS and Azure cloud services, AI agents, or Power BI, contact Q2BSTUDIO. We can offer proof of concept, MLOps pipelines, and secure deployment so your project can leverage artificial intelligence responsibly and efficiently.

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.