Attendance System Based On Face Recognition (Part I)
By: Harsh Sharma
Tags: Object Detection, Heartbeat, Machine Learning, Face Recognition

With the digitization of the world, we have moved towards wanting our identification to be digitized too. Earlier, our signature was considered as one of the most unique identifiers we had, but as technology developed, we moved towards an easier way of life and came up with some ways to use our face as an identifier.

The first step for classification of faces is to detect faces in a given image. Early Haar Cascade based classifiers were used to detect faces which were a kind of hard-coded feature extractor for faces. Then, neural networks became popular and different architectures gave far more accurate and quicker results. One of them is an architecture called RetinaFace which is a state-of-the-art model for face detection. I have explained about RetinaFace in detail in my previous article here. You can go through that and have a better understanding of the architecture.

After the detection of faces, we need some way to classify each face belonging to a different person and then, if that face appears in some other image, we need to identify it as the same person. This is handled by converting each face into a vector and we want those vectors to be similar for the faces of the same person and very different for the faces of a different person. You can imagine these vectors in 3D space (in reality it is much higher dimension), where the vectors for one person are close to each other in a space and vectors for different people are far apart from each other.

Vector representation of different faces

If you have gone through my previous articles, then you know that if we want something from an image we use Convolutional Neural Networks. In RetinaFace we use a combination of CNNs which are able to extract face coordinates from an image.

Now, we need one more architecture which can use these faces and generate some vector representation for all the faces, with a unique marker for each face. This is where ArcFace comes in. It has a special loss function which enables the vectors to be understood as far apart from each other for different people, and close to each other for faces belonging to the same person. It is an improvement over CosFace and SphereFace which were the previous state-of-the-art model for face recognition and were the first of few which brought the idea of such kind of training.

I will now explain how this architecture, combined with special loss function, gives such good representation of face vectors. Then I will provide you the code implementation along with some modifications to make it as an attendance system.

ArcFace

The paper that I am going to discuss is this paper. This explanation will be from the geometrical side of the mathematics involved. In the start it might not seem obvious, but as I connect it all you will understand it better and if you have gone through my previous articles, then you know that my idea of life rests on this: “If you know what you are doing, it will always be easier for you to do it.”

The above-mentioned paper has just extended the ideas which SphereFace brought up. According to the authors of SphereFace, we can imagine that the vectors that are generated using an architecture can be thought of being embedded into a hypersphere, and, if we somehow can manage that, then the vectors on the hypersphere are farther for different identities and closer for the same identity. With this, our task of face recognition will be done. I know that you might have lots of confusion about the above statement, but rest assured, it will all be cleared up in next few paragraphs.

Let’s break the idea into multiple parts and understand each of them separately and then connect them all.

The vectors generated by the architecture

We know that after doing convolutions on an image we extract feature representation of that image. These feature representations are then used in various ways according to our tasks. In case of classification, we connect these feature representations to a dense layer which has n number of neurons where n is the number of classes. Typically, before connecting it to n layers for classification, we connect it to higher number of neurons to add some nonlinearity for classification. For example, we connect the output of convolutions with a 512 neuron dense layer and then the output of that dense layer is connected to the n neurons which is the final layer for classification.

Now, if we pass an image through all the convolutions and this 512 dimensional layer, we will get an output which will contain all the information about the image in a 512 dimensional vector form. This is what is referred to as vector generated by the architecture.

These vectors can be thought of being embedded into a hypersphere

The vector representation of the image that we obtained after the dense layer is nothing but a point lying in 512 dimensional vector space having some directional information from the origin. If we normalize this vector and make the length of the vector as 1, then we can be sure that this vector is lying somewhere in a high dimensional space but it will be exactly 1 unit away from the origin. So, if we normalize all the vectors for all the images then they all will lie exactly 1 unit away from the origin in any direction. So, these vectors can be thought of being on a hypersphere with a radius of 1 unit. That is what it means to be embedded in a hypersphere.

So, if we have an architecture which is able to generate rich feature vectors in such a way that the vectors for different face images of one person are closer to each other compared to the vectors for face images of different people, then we can have a very good face recognition system. These are referred to as intraclass compactness and interclass discrepancy.

Now we know what we have to do. Next I will discuss how we do it.