Object Detection using YOLO and Car Detection Implementation
By: Bhavesh Singh Bisht
Tags: AI, Detection, Car Detection, Machine Learning, Yolov3

There are several drawbacks of using a sliding window for object localization such as selecting appropriate kernel size, stride etc. which leads to high computational cost.

Introduction

YOLO (You only look once) was introduced by Joseph Redmon, Santosh Divvala, Ross Girshick and Ali Farhadi in 2015. The best things about YOLO is that it achieves high accuracy while also being able to run in real time. The approach of YOLO is to frame object detection as a regression problem and passing it to fully a connected neural network . The system divides the input image into an S×S grid and each grid cell predicts bounding boxes, confidence for those boxes, class probabilities. These predictions are encoded as an S×S×(B∗5 +C) tensor.

Fig 1. Shows the working of YOLO model

Each bounding box has 5 additional values apart from class probabilities which are as follows:

Pc : confidence of an object being present in the bounding box bx, by : Center coordinates of the object present if any. This value is “do not care” if no object is present. bh, bw : height and width of the object present. This value is “do not care” if no object is present. C : Class of the object being detected

Fig 2. Represents the parameters of a bounding box

The YOLO architecture consists of 24 convolution layers followed by 2 fully connected layers. Alternating 1×1convolutional layers reduce the features space from preceding layers. The final output of the network is 7×7×30 tensor of predictions.

Non-max Suppression

The concept of non-max suppression comes when the same object is present in several grid cells. The algorithm might then detect several bounding boxes around the same object with different confidence scores. Non-max suppression suppresses the values (detection) where Pc values are below a certain threshold. First, it selects the highest Pc or IOU value and suppress all the values less than that. Then selects the second highest and continue this process. Following are the steps:

Step 1: Predict [Pc, bx, by, bw and bh] for all the grid objects.

Step 2: Discard where Pc≤0.6 (threshold). This is conditional discard with low probabilities.

Step 3: While there are remaining boxes:

Step 3.1: Pick the box with the largest Pc and output that as the prediction.

Step 3.2: Discard boxes (remaining) that have IOU≥0.5 with the output predicted in Step 3.2.

Step 3.3: Go back to Step 3

Car Detection using (Implementation in Keras):

The input is a batch of images, and each image has the shape (m, 608, 608, 3)

is a batch of images, and each image has the shape (m, 608, 608, 3) The output is a list of bounding boxes along with the recognized classes. Each bounding box is represented by 6 numbers (pc, bx, by, bh, bw, c)(pc, bx, by, bh, bw, c). The output dimension is (19, 19, 5, 85)

is a list of bounding boxes along with the recognized classes. Each bounding box is represented by 6 numbers (pc, bx, by, bh, bw, c)(pc, bx, by, bh, bw, c). The output dimension is (19, 19, 5, 85) We have chosen 5 anchor boxes to cover 80 classes

The YOLO architecture is: IMAGE (m, 608, 608, 3) -> DEEP CNN -> ENCODING (m, 19, 19, 5, 85)

Then we select only a few boxes based on: Score-thresholding: throw away boxes that have detected a class with a score less than the threshold

Non-max suppression: Compute the Intersection over Union and avoid selecting overlapping boxes

The above steps give the final YOLO Output.

Let’s start coding:

Import the required libraries

Importing the required libraries

2. Next, we read the classes, anchor boxes and set the input image shape.

Refer link for classes.txt and link for anchors.txt

Reading the classes, anchors and image shape

3. Since it would take a very long time for training the large data-set, let’s download the trained YOLO model and load it. Refer the link to download the pickle file.

Loading the downloaded pretrained YOLO model

4. The following command processes and converts the output tensor

Implementing YOLO head for processing the output

5. Next we need to implement a function to filter the boxes. The following function performs two things. First, it calculates the score by multiplying the box confidence with box class probability. By doing this we can reject boxes that are below a certain threshold. Secondly, it outputs the class to which the box belongs by selecting the max value.

Implementing the filter box function

Calling the implemented function

Now we have initialized all the required parameters and now we can write the predict function to test our model on an image. The following function process an input image run the session on the initialized scores, boxes and classes. Then we generate and draw the bounding boxes on different class names using different colors and save the image file. Now we can also see the output image using imshow and coordinates for the bounding boxes with class labels

Predict function for detecting objects in an image

Calling the predict function by passing the required parameters