Implementation of face mask detector using RetinaNet model

Face Mask Detector using RetinaNet Model


We are grappling with a pandemic that’s operating at a never-before-seen scale. This is why ,Researchers all over the globe are frantically trying to develop a vaccine or a cure for COVID-19 while doctors are just about keeping the pandemic from overwhelming the entire world. On other hand many countries have found social distancing, using mask & gloves a way to curb the situation a little.

I recently had an idea to apply my deep learning knowledge to the help the current situation a little. Therefore ,I’ve decided to build a face mask detector using RetinaNet model.

Table of Contents :-

  1. Introduction
    • Other possible applications of this use case
  2. Build Face mask detector using RetinaNet model
    • Gather Data
    • Create Dataset
    • Model Training
    • Model Testing

Other possible applications of this use case?:-

You can extrapolate the same idea to build an AI enabled solution for your smart home. So ,this AI enabled solution could open gate of your building to only people, who are wearing mask and gloves.

As the cost of drones is decreasing with time, we see a large volume of aerial data being generated. So, you can use this RetinaNet model to detect different objects such as automobile vehicles (bikes, cars etc.) or pedestrians in aerial images . In addition ,you may even leverage satellite images to solve your different business problems.

You see applications of object detection model is endless. Therefore ,without further ado let’s see implementation of RetinaNet model to build Face mask Detector in Python –

Build Face mask detector using RetinaNet model: –

Gather Data using Web Scraping technique: – 

Any deep learning model would require a large volume of training data to give good results on the test data. In this article , I’ve talked about Web Scrapping method to gather a large volume of images for your deep learning project.

Prepare Training Data Set with labelImg tool: –

We start by creating annotations for the training and validation dataset, using the tool LabelImg. This excellent annotation tool let you quickly annotate the bounding boxes of the objects to train the machine learning model.

You can install it using below command in annaconda command prompt

                       pip install labelImg

You can annotate each JPEG file using labelmg tool like below and it’ll generate XML files with coordinates of each bounding box. And we’ll use these xml files to train our model.

Image annotation using labelImg tool


Image : Image annotation using labelImg tool

Model Training: –

Step 1. Clone & install the keras-retinanet repository

Step 2. Import all required libraries

Step 3. import JPEG & xml data

Step 4. Write a function to show bounding boxes on training dataset

Bounding box on training image


Image : Bounding box on training image

Step 5. Train RetinaNet Model

It’s better to start with pretrained model in lieu of training a model from scratch. We’ll use a pretrained (on coco dataset) ResNet50 model 

Note: – You can use below snippet of code to train your model ,If you’re using Google Colab.

But ,If you’re training your model on your local Jupyter notebook or different IDE then you can use below command from your command prompt.

Let’s analyse each argument passed to the script

  1. freeze-backbone:- freeze the backbone layers, particularly useful when we use a small dataset, to avoid overfitting
  2. random-transform:- randomly transform the dataset to get data augmentation.
  3. weights:- initialize the model with a pretrained model (your own model or one released by Fizyr)
  4. batch-size:- training batch size, higher value gives smoother learning curve
  5. steps:- number of steps for epochs
  6. epochs:- number of epochs to train
  7. csv:- annotations files generated by the script above

Step 6. Load Trained Model

Now let’s load our trained model.

Model Testing: –

Since our model is trained , it’s time to test our trained model.

Step 7. Predict using trained model


Predicted Image by RetinaNet model with confidence score


                   Image :- Predicted Image with confidence score

In addition ,below are few more predicted images –

Images :- Predicted Images by RetinaNet Model

Final Notes: –

So ,we went through the complete journey to make face mask detector with implementation of RetinaNet . We’ve created a dataset, trained a model, and ran inference (here is my Github repo for the notebook and dataset).

I’ve used only 6 epcohs with 500 steps each , However I’ve got decent results for face mask detector with such limited dataset & very minimal training .

And you can always change threshold value & check your results on test dataset.


  • Make sure you you train your model atleast for 20 epochs to get good results.
  • Idea is to submit the approach to build face mask detector using RetinaNet model . One can always tweak the model , data & approach as per business requirement.

In general, RetinaNet is a good choice to start an object detection project. Especially when you need to quickly get good results.

End points:-

In this article , I’ve tried my bit to explain Implementation of Face Mask Detector using RetinaNet as simple as possible.

If you still have any query then please feel free to ask them comment section ,I’ll be more than happy to answer them. If you’ve enjoyed this article then leave a few claps, It’ll encourage me to explore further machine learning opportunities 🙂

References: –


Article Credit:-

Name:-  Praveen Kumar Anwla

Founder:– TowardsMachineLearning.Org Linkedin

Leave a Comment