Imagine the Ideal. Make it Happen.
AI and RoboticsProjects

Facial Feature Recognition with Vanilla Neural Networks

Facebook DeepFace (FAIR) can recognize faces in any pictures with an extremely high accuracy rate and precision. Thanks to the deep learning technology and sophisticated algorithm combined with a large amount of data, it is now possible to train systems to achieve near human image processing capabilities.

In this post, I will give an overview of a very simple face recognition system developed in MATLAB®  following sample codes for you to try.


We want to build a perceptron to identify the following features in a face:

  • Gender of the person in the picture
  • Whether the person is wearing glasses and
  • Whether the person is smiling


  • Windows PC/ MAC OS X/ Linux with MATLAB preinstalled
  • A large database of Face images, divided into two sets, train images and test images, both with an attribute file for the desired features.
  • All face images need to be aligned for their eye position which we will discuss later


Perceptron. A perceptron is the basic building block of any Artificial Neural Network.

Image source

It consists of a single neuron with several inputs and often a single output. The following Wikipedia article gives a lot more details about the perceptron:

Training Data Import (file_importer.m)

Colour Images are in the form of a 3D matrix with the image width, image height and colour depth. However, a perceptron only takes input in the form of a column vector. Also, it is important for all images to be of the same size so there is no matrix size mismatch error. So we do the following:

  • Crop the images to be of the same size (101x101x3)
  • Convert the images to grayscale to reduce the resolution (101×101)
  • Reshape the image matrix to a column vector
  • Repeat the process for test images
  • Attribute files are simple text files containing the attribute values for all the 3 features.
  • We simply open the file and read the contents and store this in a matrix for all the images
  • Repeat the process separately for test images
  • Once all the information is obtained store the workspace for future use (image_import.mat)

Training (face_reco_perceptron.m)

In this step, we want to learn the weights for our perceptron. So, we simply use the training image and attribute files information to perform the training process. We can repeat the process until there is zero error in the training set. Store the workspace for testing purposes as (face_perceptron-workspace.mat)

Converting the value of w in the form of an image shows the following:


Testing (face_reco_perceptron_test.m)

Now we can use the weights already obtained in the training phase and run that across the test set to verify how well our system has learnt the features.

Application (face_recognizer.m)

I have developed an app  that does the following:

  • Requests the user to select an image file (.jpg) as an input.
  • it uses the trained to weights to then recognize the features of that image and displays the information using a dialog box.

Note that this will work well only if the image is well aligned with respect to the eyes.

Feature recognized incorrectly – Image not alignedFeature recognized correctly – Image aligned


All the resources are available in the following Github Project:

Dark Mode

citrus-ai (this link opens in a new window) by SivamPillai (this link opens in a new window)

This is a collection of useful codes/ Algorithms/ Libraries/ Experiments related to AI (Particularly aiming at Deep Learning, Machine Learning, Fuzzy Systems and Neural Networks

Let me know if you have any suggestions for improvements through comments below. Of course, remember that this is a simple algorithm that uses a perceptron with no special optimization technique as that was the primary purpose of this post and not getting too sophisticated with the design.

Feel free to fork the Github project to develop your own facial attribute recognizer.

Article written by:

I am a Research Scholar in NUS Singapore working in the field of Computational Intelligence and Deep Learning. A Technology Admirer; I am curious to know more, to explore - and to share and socialize


  1. Harshini

    Can I get the code for the above program using matlab?

  2. Dr Spill

    Hi Harshini, thanks for reading. Have you tried the GitHub link (citrus-ai) at the end of the article? Unfortunately, I do not work on matlab now, so I am not able to verify the contents of the code. I would request you to kindly check the baseline code there and let me know if it is useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.