Face Landmarks Recognition

Shittu Olumide Ayodeji
4 min readFeb 24, 2023
Image source: Unsplash

We can achieve many things with data science and machine learning, and it is getting more interesting daily. We all interact with these inventions and products daily. For example: Have you ever wondered how Snapchat can customize beautiful filters for your face? It has been set up to recognize certain markings on your face and project a filter based on those marks. These are known as Face Landmarks in machine learning.

In this tutorial, we’ll examine how to use machine learning to find these facial landmarks and log our project to Comet.

Face landmark recognition

Face landmark recognition is used in computer vision and image processing to locate specific facial features, such as the eyes, nose, mouth, and jawline. This technique can be used for various applications, including facial recognition, emotion detection, and head pose estimation.

One popular invention in this field is the Viola-Jones algorithm, developed by Paul Viola and Michael Jones in 2001. This algorithm uses a cascade of simple classifiers to quickly and accurately detect faces in images.

For the sake of this tutorial, we will make use of PyTorch, an open-source machine learning framework that accelerates the path from research prototyping to production deployment, and Comet a machine learning platform that integrates with your existing infrastructure and tools so you can manage, visualize and optimize models — from training runs to production monitoring.

Here is the step we will follow in this article; I have carefully broken down the steps so it's easier for us to follow and understand.

  1. Import libraries.
  2. Import the dataset for the project.
  3. We will get the image and label.
  4. Create model
  5. Train the Model
  6. Test Model
  7. Log project to Comet.

Import libraries

We will need Pandas, Torch, Scikit-learn, Numpy, Matplotlib, and Comet for this project.

Import the dataset for the project

The dataset we will use in this tutorial was provided by Dr. Yoshua Bengio of the University of Montreal, containing 7049 facial images and up to 15 key points marked on them.

Let’s load the label dataset.

Then, we will also load the image dataset.

For the sake of this project, we will need to log some of the hyperparameters we will be using to Comet. We will initiate a connection to Comet and log the hyperparameter using a dictionary.

Split dataset train and test

For our model, we will split the label and image datasets into train and test datasets.

Create a class for face dataset

We will create a class in our project for the face dataset so that we can easily access and identify the landmarks on each face.

Let’s test the class.

Image result after testing the class method.

As we can see, after instantiating the FaceDataset class that we created earlier, we were able to test it using the X_train and y_train datasets. The next thing is to create a new dataset.

Creating a model

We have to select a device and load the model; we will use the Convolutional neural network.

The Convolutional neural network model.

Train the model

After the creation of the model, the next action is to actually train the model before testing it.

Model training.

Let’s have a quick visualization of the prediction.

Image showing the Loss and Epoch of the trained model.

Visualize model test

We successfully trained the model in the previous section, so we will test the model and see the images ourselves. After running this code, we will get a series of images printed in the terminal.

Model testing.
Image showing facial landmarks on two people.

Finally, we have to end the experiment.

Conclusion

Facial Landmark Detection can also be done using a deep learning approach by training a Convolutional Neural Network (CNN) on a dataset of annotated facial landmarks; this approach has shown promising results and is used in industry.

Face landmark detection is an active area of research, with ongoing developments in both traditional computer vision techniques and deep learning-based methods.

Here is the link to the notebook; feel free to leave a star.

Let's connect:

--

--

Shittu Olumide Ayodeji

Hello 👋 my name is Shittu Olumide, I am a skilled software developer and technical writer, compassionate about the community and its members.