Detecting Smiling Faces with a Convolutional Neural Network

Author

Swaraj Khan P

Published

April 19, 2024

Building a CNN for Smiling Faces Detection

In this project, we build a Convolutional Neural Network (CNN) to detect smiling faces from images. The dataset we use is in HDF5 format and contains images of faces labeled as either happy or not happy. We will go through the steps of loading the data, visualizing it, preprocessing it, building and training the CNN model, and finally evaluating its performance.

Project Description

This project involves the following steps:

  1. Data Loading: Load the dataset from HDF5 files.
  2. Data Visualization: Visualize the images and labels.
  3. Data Preprocessing: Normalize the images.
  4. Model Building: Construct a CNN using Keras.
  5. Model Training: Train the CNN on the training dataset.
  6. Model Evaluation: Evaluate the model on the test dataset and visualize the results.
  7. Confusion Matrix: Generate a confusion matrix and classification report.

Data Loading

First, we import the necessary libraries and load the dataset from HDF5 files.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import h5py
import random

Loading the Dataset

We specify the path to the HDF5 file containing the training data and open the file.

filename = 'D:/Coding/NLP/proejct/Machine-Learning-Projects-master/Machine-Learning-Projects-master/CNN-Smiling Faces Detecto/train_happy.h5'
f = h5py.File(filename, 'r')

for key in f.keys():
    print(key)

This code snippet:

- Opens the HDF5 file in read mode.
- Prints the keys (datasets) available in the file.

We then load the training and testing data.

happy_training = h5py.File('D:/Coding/NLP/proejct/Machine-Learning-Projects-master/Machine-Learning-Projects-master/CNN-Smiling Faces Detecto/train_happy.h5', "r")
happy_testing = h5py.File('D:/Coding/NLP/proejct/Machine-Learning-Projects-master/Machine-Learning-Projects-master/CNN-Smiling Faces Detecto/test_happy.h5', "r")

X_train = np.array(happy_training["train_set_x"][:]) 
y_train = np.array(happy_training["train_set_y"][:]) 

X_test = np.array(happy_testing["test_set_x"][:])
y_test = np.array(happy_testing["test_set_y"][:])

Data Preprocessing

Before feeding the data into the model, we normalize the images by dividing by 255.


X_train = X_train / 255
X_test = X_test / 255

Normalization is crucial in deep learning as it ensures that the model trains faster and reaches a better performance. This step scales the pixel values to the range [0, 1].

Model Building

We use Keras to build a Convolutional Neural Network (CNN) model. The model consists of convolutional layers, pooling layers, dropout layers, and dense layers.


from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.optimizers import Adam
from keras.callbacks import TensorBoard

cnn_model = Sequential()

cnn_model.add(Conv2D(64, (6, 6), input_shape=(64, 64, 3), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))

cnn_model.add(Dropout(0.2))

cnn_model.add(Conv2D(64, (5, 5), activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))

cnn_model.add(Flatten())
cnn_model.add(Dense(units=128, activation='relu'))
cnn_model.add(Dense(units=1, activation='sigmoid'))

Model Compilation

We compile the model using the Adam optimizer and binary crossentropy loss function. Binary crossentropy is suitable for binary classification problems.

cnn_model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])

Model Training

We train the model on the training data for 5 epochs. During training, the model learns the features that distinguish happy faces from non-happy faces.

epochs = 5
history = cnn_model.fit(X_train, y_train, batch_size=30, epochs=epochs, verbose=1)

Model Evaluation

We evaluate the model on the test data and print the test accuracy. This step gives us an idea of how well the model generalizes to unseen data.

evaluation = cnn_model.evaluate(X_test, y_test)
print('Test Accuracy : {:.3f}'.format(evaluation[1]))

Predictions

We make predictions on the test data and visualize the predicted and true labels for a grid of images. This helps in understanding the performance of the model qualitatively.

predicted_classes = cnn_model.predict_classes(X_test)

L = 5
W = 5
fig, axes = plt.subplots(L, W, figsize=(12, 12))
axes = axes.ravel()

for i in np.arange(0, L * W):
    axes[i].imshow(X_test[i])
    axes[i].set_title("Prediction Class = {}\n True Class = {}".format(predicted_classes[i], y_test[i]))
    axes[i].axis('off')

plt.subplots_adjust(wspace=0.5)

Confusion Matrix and Classification Report

We generate a confusion matrix and classification report to evaluate the performance of the model. These metrics provide a detailed insight into the model’s classification abilities.

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, predicted_classes)
plt.figure(figsize=(10, 10))
sns.heatmap(cm, annot=True)

from sklearn.metrics import classification_report

print(classification_report(y_test.T, predicted_classes, target_names=target_names))

Source Code

For more details, visit the source code on GitHub.