Face-Mask-Detection using Deep Learning

Devashi Choudhary
Analytics Vidhya
Published in
5 min readJun 27, 2020

The novel COVID-19 virus has forced us all to rethink how we live our everyday lives while keeping ourselves and others safe. Face masks have emerged as a simple and effective strategy for reducing the virus’s threat and also, application of face mask detection system are now in high demand for transportation means, densely populated areas, residential districts, large-scale manufacturers and other enterprises to ensure safety. Therefore, the goal of the today’s article is to develop a face mask detector using deep learning.

Table of Content

  1. About Dataset
  2. Convolutional Neural Network (CNN) Architecture
  3. Training and Evaluation of CNN model
  4. Experiments and Results
  5. What’s Next?

About Dataset

The images used in the dataset are real images of people wearing mask i.e. the dataset doesn’t contain morphed masked images. The dataset consists of 3835 images belonging to two classes:

  • with_mask: 1916 images
  • without_mask: 1919 images

The images were collected from the following sources:

CNN Architecture

The convolutional neural network or convnets is a major back through in the field of deep learning. CNN is a kind of neural network, and they are widely used for image recognition and classification., They are mainly used for identifying patterns in the image. We don’t feed features into it, they identify features by themselves. The main operations of CNN are Convolution, Pooling or Sub Sampling, Non-Linearity, and Classification.

CNN Architecture
  1. Convolution: ConvNets derive their name from the convolution operator. The primary purpose of Convolution in the case of a ConvNet is to extract features from the input image. Convolution preserves the spatial relationship between pixels by learning image features using small squares of input data.
  2. Pooling: The main purpose of pooling is to reduce the size of the input image but retains the important information. It can be done in different types like Max, Sum, Average, etc. In the case of Max Pooling, we define a window and take the largest element from that, we could also take the average (Average Pooling), the sum of all elements (Sum Pooling) within that window.
  3. Non Linearity: Activation function is a function that is added into a neural network to help the network learn complex patterns in the data i.e. to introduce non-linearity. For example, activation function ReLU(Rectified Linear Unit) is used to introduce non-linearity in CNN. It replaces all negative value pixels in an image to zero by performing an element-wise operation.
  4. Fully Connected: The Fully Connected layer is a traditional Multi-Layer Perceptron that uses a softmax activation function in the output layer (other classifiers like SVM can also be used). The term Fully Connected implies that every neuron in the previous layer is connected to every neuron on the next layer. The output from the convolutional and pooling layers represent high-level features of the input image. The purpose of the Fully Connected layer is to use these features for classifying the input image into various classes based on the training dataset.

Training and Evaluation of CNN model

The training of the CNN model requires the following steps :

  1. Pre-processing: It was applied to all the raw input images to convert them into clean versions, which could be fed to a neural network machine learning model. The input image is resized to 224 x 224 (Line 8) and pass input image to preprocess_input function in (Line 9), which is meant to adequate your image to the format the model requires (you guarantee that the images you load are compatible with preprocess_input ). Finally converting data and labels to NumPy arrays for further processing in (Line 14). One hot encoding is performed on labels to convert categorical data to numerical data(Line 17–19).
  2. Model Creation: The CNN model (Line 23–33) includes two convolutional layers followed by activation function ReLU(to add non-linearity) and Max Pooling(to reduce the feature map). Dropout is added to Prevent Neural Networks from Over-fitting. Then, fully connected layers are added at the end. Finally, we compiled our model to the loss function, the optimizer, and the metrics (Line 36). The loss function is used to find error or deviation in the learning process. Keras requires loss function during the model compilation process. Optimization is an important process that optimizes the input weights by comparing the prediction and the loss function and Metrics is used to evaluate the performance of your model.
  3. Model Training: Before start training of model we need to split the data into Train-Test data. In our case, there was 90% of training data and 10% of testing data (Line 21). Models are trained (Line 41) by NumPy arrays using the fit function. The main purpose of this fit function is used to evaluate your model on training.
  4. Model Prediction: This is the final step, in which we will evaluate the model’s performance by predicting the test data labels (Line 44).

Results

  1. Analysis of CNN model.

2. Implementing face mask detector for images.

First, the input image is loaded from the disk and then, face detection is done using Caffe based face detector. The Caffe model have prototxt file(s) which define the model architecture (i.e., the layers themselves)

Caffe model file which contains the weights for the actual layers. Finally, the prediction is done on the input image as shown in the figure.

3. Similarly, face-mask-detector is applied to Real-Time video streaming.

What’s Next?

In this article, we have developed CNN based face mask detector, which can contribute to public healthcare, Airports, and Offices to ensure safety.
We can also use other training models like VGGNet and MobileNet as base models and using YOLO with darknet to perform face detection.

References

It’s always good to give references

  1. Dataset
  2. Model

The code is available at github.com/Devashi-Choudhary/Face_Mask_Detection. For any questions or doubts, feel free to contact me directly at github.com/Devashi-Choudhary.

--

--

Devashi Choudhary
Analytics Vidhya

Data Scientist at NetApp | IIIT-DELHI | IET-DAVV | Machine Learner, Researcher and Part Time Photographer💯