Cracking Faces: A Fun Dive into Emotion Recognition with CNNs
Ever wish your computer could tell how you’re feeling? No more yelling at the screen — it could just know you’re frustrated! Well, welcome…
Cracking Faces: A Fun Dive into Emotion Recognition with CNNs

Ever wish your computer could tell how you’re feeling? No more yelling at the screen — it could just know you’re frustrated! Well, welcome to the world of Facial Emotion Recognition (cue dramatic music). This is where we train machines to pick up on human emotions just by analyzing our facial expressions. My latest project dives into exactly that, using Convolutional Neural Networks (CNNs) to classify emotions from the FER-2013 dataset. Trust me, it’s as cool as it sounds.
Why Emotion Recognition?
Imagine a world where machines don’t just follow commands but respond based on your emotions. Whether it’s making customer service more empathetic or creating personalized entertainment experiences, emotion recognition is the next big thing in making tech more intuitive and human-friendly. So, naturally, I thought, “Why not teach a machine to understand the difference between a smile and a frown?” That’s where this project comes in.
The Dataset: FER-2013
Let’s talk data. For this experiment, I used the FER-2013 dataset — a collection of grayscale images of human faces. These images come with labels for seven emotions:
- Angry
- Disgust
- Fear
- Happy
- Sad
- Surprise
- Neutral
Basically, all the ways you look after a long week at work! This dataset is great for training models to recognize these expressions, and boy, did I put it to good use.
The Models: Showdown Between CNN Architectures
I decided to pit two architectures against each other: a Basic CNN model I designed and the classic LeNet (more on this in a second). Let’s see which one emerged victorious.
Basic CNN Model
In the first part of the project, we implemented a basic CNN model to classify facial emotions from grayscale images resized to 48x48 pixels. The training data is loaded from different folders, each representing a specific emotion. We used a series of Conv2D layers followed by MaxPooling2D and Dense layers to build a deep learning model. The data is preprocessed by reshaping the images and one-hot encoding the labels using LabelEncoder and TensorFlow utilities.
Gist of Code:
- Loading Images and Labels: The code scans through a directory, loading images from subfolders, each corresponding to a specific emotion (anger, happiness, etc.). The images are resized to 48x48 and converted to grayscale.
- Label Encoding and One-Hot Conversion: After loading, labels are encoded into numerical format using
LabelEncoder, and categorical data is converted into one-hot encoded vectors to train the model. - CNN Architecture: A CNN is designed with multiple convolutional layers, ReLU activations, and max pooling. Finally, the model outputs probabilities for the different emotion classes using a softmax layer.
- Training and Evaluation: The model is compiled using Adam optimizer and categorical cross-entropy loss, then trained over the dataset with accuracy as the key metric. After training, the model’s performance is evaluated using test data.
The basic CNN is like the MVP of this project. It’s simple yet effective, designed to capture the nuances of human emotion from facial features. After training it on the dataset, here’s what I got:
- Training Accuracy: 87.36%
- Training Loss: 0.3636
- Testing Accuracy: 67.42%
- Testing Loss: 1.2396
Not too shabby, right? It managed to learn the different emotions pretty well, though there’s still room for improvement on the test set.
LeNet Architecture
In the second part, a different architecture, LeNet, is used to perform the same emotion classification task. This architecture is simpler and more compact compared to the custom CNN model, primarily designed for smaller images (32x32) with fewer parameters. The key steps involve splitting the dataset into training and testing sets, normalizing the data, and then applying the LeNet architecture.
Gist of Code:
- LeNet Architecture: This model follows a classical LeNet structure with two convolutional layers followed by subsampling (pooling), a fully connected layer, and an output layer with softmax activation. It is suitable for lightweight models and performs well on smaller image sizes.
- Training Process: Similar to the earlier model, the data is normalized, and the model is trained with the Adam optimizer. It evaluates the performance by measuring accuracy on the test data.
- Predictions on Random Samples: After training, the model predicts emotions for random test samples and compares the predicted and actual labels. Visual results are shown using
OpenCV, where the image and predicted emotion label are displayed.
Now, I also tried using LeNet, which is a classic CNN model originally built for digit recognition (hello, handwritten digits!). While LeNet is a champ in its own league, when it came to emotions, it wasn’t the greatest:
- Training Accuracy: 70.52%
- Training Loss: 0.8036
- Testing Accuracy: 46.90%
- Testing Loss: 1.6407
Yeah, not exactly a show-stopper here. Turns out, faces are a little trickier to handle than digits!
Ace Emotion Recognition Using CNNs: Teaching Machines to Read Faces
Ever wish your computer could tell how you’re feeling? No more yelling at the screen — it could just know you’re frustrated! Well, welcome to the world of Facial Emotion Recognition (cue dramatic music). This is where we train machines to pick up on human emotions just by analyzing our facial expressions. My latest project dives into exactly that, using Convolutional Neural Networks (CNNs) to classify emotions from the FER-2013 dataset. Trust me, it’s as cool as it sounds.
Why Emotion Recognition?
Imagine a world where machines don’t just follow commands but respond based on your emotions. Whether it’s making customer service more empathetic or creating personalized entertainment experiences, emotion recognition is the next big thing in making tech more intuitive and human-friendly. So, naturally, I thought, “Why not teach a machine to understand the difference between a smile and a frown?” That’s where this project comes in.
The Dataset: FER-2013
Let’s talk data. For this experiment, I used the FER-2013 dataset — a collection of grayscale images of human faces. These images come with labels for seven emotions:
- Angry
- Disgust
- Fear
- Happy
- Sad
- Surprise
- Neutral
Basically, all the ways you look after a long week at work! This dataset is great for training models to recognize these expressions, and boy, did I put it to good use.
The Models: Showdown Between CNN Architectures
I decided to pit two architectures against each other: a Basic CNN model I designed and the classic LeNet (more on this in a second). Let’s see which one emerged victorious.
Basic CNN Model
The basic CNN is like the MVP of this project. It’s simple yet effective, designed to capture the nuances of human emotion from facial features. After training it on the dataset, here’s what I got:
- Training Accuracy: 87.36%
- Training Loss: 0.3636
- Testing Accuracy: 67.42%
- Testing Loss: 1.2396
Not too shabby, right? It managed to learn the different emotions pretty well, though there’s still room for improvement on the test set.
LeNet Architecture
Now, I also tried using LeNet, which is a classic CNN model originally built for digit recognition (hello, handwritten digits!). While LeNet is a champ in its own league, when it came to emotions, it wasn’t the greatest:
- Training Accuracy: 70.52%
- Training Loss: 0.8036
- Testing Accuracy: 46.90%
- Testing Loss: 1.6407
Yeah, not exactly a show-stopper here. Turns out, faces are a little trickier to handle than digits!
The Verdict: Basic CNN for the Win!
So, when we compare the two models, the Basic CNN comes out on top by a long shot. With a 67.42% testing accuracy, it outperformed LeNet’s 46.90%. This tells us that a more specialized model for this task makes a big difference. LeNet was good for digits, but emotions? Not so much.
메타데이터
- post_id
- da528a64108b
- slug
- cracking-faces-a-fun-dive-into-emotion-recognition-with-cnns-da528a64108b
- url
- https://medium.com/@shivaprakashsrinivasaniam/cracking-faces-a-fun-dive-into-emotion-recognition-with-cnns-da528a64108b
- canonical_url
- https://medium.com/@shivaprakashsrinivasaniam/cracking-faces-a-fun-dive-into-emotion-recognition-with-cnns-da528a64108b
- author_url
- https://medium.com/@shivaprakashsrinivasaniam
- status
- ok
- fetched_at
- 2026-06-27 07:40:21