Deep Learning Project in TensorFlow (Face Detection)

Deep Learning Nov 28, 2019

In this blog post, we are going to do a deep learning project in Tensorflow. We will implement Faceboxes in TensorFlow this deep learning Model will detect images faces present in the image. Faceboxes is used to build this deep learning model to learn more about Faceboxes consider the following resources.

1. Official Research paper

2. Simple Explanation of  research paper on DataDiscuss

Ok so let's get started :)

Tools and resources to be used in this project:

  • Google Colab
  • Google Drive For data storage
  • Github Account for storing scripts and code base
  • Happy and Excited person to execute all the steps

So In this tutorial, we are going to use a pre-trained model of faceboxes and if you want to learn how to train the model by yourself you can look in the GitHub repository of Faceboxes Implementation or If you want me to show step by step process of training just let me know in the comment box. I know that you are a serious student and want to learn💓.

Step 1: Learn About Faceboxes.

First of all, you need to have a proper understanding of neural networks architecture of Faceboxes, here  I have explained n detail How any face detection deep learning models work and what are the application of face detection. What is the architecture of Faceboxes and some problems that previous deep learning models were not able to solve and Faceboxes solved it.

Zoom Out Image by clicking on it to see a overview of neural network

Step 2 : Open a new Jupyter Notebook In google colab and attach your Google Drive.

i)  I hope that you know how to make a new jupyter Notebook Instance in google colab but if you don't know what is google colab that's fine just open this link and you'll see step by step procedure on how use google colab.

ii)  Once you opened New Jupyter Notebook in google colab attach your google drive into the google colab because we are going to store pre-trained model in the google drive and use it in our google colab instance. If you want to learn how to attach google drive to google colab go to this Link (its's google colab tutorial)

Step 3: Download the weight files and upload it into your google Drive

So now you have attached the google drive into google colab instance, Now we have to download the weight file of pre-trained model and upload it into google drive to use the pre-trained face detector.

For this tutorial, I'm taking reference from this GitHub repository if you want to show some appreciation go and give a star to this repository(🙇).

Go to this link and download (.pb File) weight files and Upload it into your Google drive, Or you can directly move this weight file into your google drive.

Step 4: Enough setup, Time to write some code(🤓)

Write each code section in the Code Shell of Jupyter Notebook and run in sequence.

%load_ext autoreload
%autoreload 2

# Well I'm not sure Why i'm using this code

Ignore this code don't try to understand just do it(😂)

Attach your Google Drive (if not already done) by using this code snippet, or step by step guide is here

# In this I'm attaching my google drive to the 
# It requires authentication from account
import warnings
warnings.filterwarnings('ignore')
from google.colab import drive
drive.mount('/content/gdrive')

After this authorize and past the given code into the input box .

Now you have to clone a repository and I selected only one required script from the whole repository because when we are using a pre-trained moidel we don't need everything we only need face_detector.py, Go to my Github repository and clone it into google colab by using the code given below.

# I am cloning my repository, I copied face_detector.py file from the TropComplique/FaceBoxes-tensorflow 
# repository and also downloaded weights provided

! git clone https://github.com/veshitala/face-detection.git  

# User--- Sheetala Prasad Tiwari

Now change your Present Working Directory to face-detection that you've just cloned.

## changing PWD to the cloned repository to use FACEDETECTOR
cd face-detection/

We are doing this so that the face-detection module can be used when we import it, Now let's Import all the required libraries

# Importing all the required library and also facebox to verify the installation
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
import numpy as np
from PIL import Image, ImageDraw
import os
import cv2
import time
from face_detector import FaceDetector

Optional Step

# returning back to my original working directory
cd ..

Return to previous folder because I've started my data in /content/drive. You can avoid this step if you stored it somewhere else.

Now provide the path to the .pb file that you've stored in the google drive, Make sure that the provided path is correct otherwise it will show error. We are making an instance face_detector to be used in the face detection task.

## Providing path for pretrained model of facebox
MODEL_PATH = './gdrive/My Drive/facenet/facebox/model.pb'
face_detector = FaceDetector(MODEL_PATH, gpu_memory_fraction=0.75, visible_device_list='0')
## Gpu memory can be adjusted by using gpu_memory_fraction parameters

Now I'm sure that you have some of your images uploaded on google drive before if not then upload some images of you on google drive so that we can test our model on Human Image, (I'm sure you are a human 🙃)

Remember to give exact path of your Image otherwise you'll get an error

You'll see output like this...

Code for this

### Checking simply on a custom image 
### printing just image after resizing and transformation
path = './gdrive/My Drive/facenet/images/sheetala1.png'
required_size=(600, 400)
image_array = cv2.imread(path)
image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image_array)
image = image.resize(required_size)
image

Now we are going to write a function that will use pre-trained model and will draw s a rectangular image around a face present in the image.

## Darawing boxes around the face present in the image
## Confidence score indicates how sure model is in recosgnising faces
def draw_boxes_on_image(image, boxes, scores):

    image_copy = image.copy()
    draw = ImageDraw.Draw(image_copy, 'RGBA')
    width, height = image.size

    for b, s in zip(boxes, scores):
        ymin, xmin, ymax, xmax = b
        fill = (255, 0, 0, 45)
        outline = 'red'
        draw.rectangle(
            [(xmin, ymin), (xmax, ymax)],
            fill=fill, outline=outline
        )
        draw.text((xmin, ymin), text='{:.3f}'.format(s))
    return image_copy.resize(required_size)

This function is using face_detector instance and finding the coordinates of faces present in the image and drawing a rectangular box around the face.

Finaly let's do face detection on my image (Moment of joy 😀)

### drawing a rectangle around my face .....
boxes, scores = face_detector(image_array, score_threshold=0.3)
draw_boxes_on_image(Image.fromarray(image_array), boxes, scores)

You can change score_thresold parameter aas per your requirement, If box is not being drawn then try to lower the score_thresold.

Final Output.

Homework:

  1. Implementation of this deep learning model on video.
  2. Implement this deep learning model on multiple faces.
  3. Share this article with your friends who are in need of deep learning project.
  4. Comment and like this will encourage me to do and share more such projects.

In next Blog-Post We'll make Deep Learning model face Face Recognition, Till then below given some Awesome resources to enhance you knowledge and skill.

sheetala tiwari

I am passionate about Data Science and Machine Learning. I am currently building an AI community on DataDiscuss and we are committed to providing free access to education for everyone.