YOLOv4 Custom Object Detector with Darknet in Colab Notebook. FaceMask Detection.

Prashant Shekhar
6 min readJul 9, 2020
YOLOv4 performance on MS COCO.
Speed(AP) vs. Performance(FPS) graph of YOLOv4 and other models.

Introduction

Yolov4 is here, with more accuracy and optimal speed. What is YOLO?? You Only Look Once (YOLO) is an object detection system to detect multiple objects in real time and recognizes various objects in a single frame.
What’s new in YOLOv4?
According to the paper based on YOLOv4[1] the main objective of the authors was to optimize neural network detector for parallel computation, to gain speedily operating object detector which can give more accurate result.
YOLOv4 has CSPDarknet53 as backbone being most optimal model, Spatial Pyramid Pooling(SPP) additional module as it increases the receptive field with almost no reduction in operational speed of the network, PANet (Path Aggregation Network ) as Neck where in YOLOv3 Feature Pyramid Network) was used and YOLOv3 as Head.

Here we are going to implement YOLOv4 for Detecting FaceMask. You can get the same dataset from Kaggle. To start with the project we will need to gain a bit knowledge about Colab Notebook as we are going to use it to run our code and use the free cloud’s GPU.

Google Colab Notebook :
Colab is a Python development environment that runs in the browser using Google Cloud. In easy words, Colab is a virtual machine you can access directly.

Lets start with our Project!

  1. Enable the GPU within your Notebook.
    It will speed up the process over 100 times faster than CPU.
Click on Runtime then Change runtime type.
Then select GPU as Hardware Accelerator.

2. To access your google drive mount it with colab using :

3. Now we will begin with cloning and building Darknet.

!git clone https://github.com/AlexeyAB/darknet
!/usr/local/cuda/bin/nvcc — version
!make

4. Download pre-trained weights (YOLOv4 weights).

!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights

5. Here you can run your detector with pre-trained yolov4 weights with MS COCO classes.

!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg

You can see the image by using matplot library with imshow() function :

Output bythe pretrained YOLOv4 weights.

6. Create a folder in your drive named yolov4. Make a txt file named obj.data containing these :

Make a txt file named obj.names and add your classes inside it , like i have used two classes according to my dataset.

7. You can create your Dataset and label them with the bounding boxes using labelmg, Yolo_label, VOC etc. But make sure the label file must be in txt format which is a YOLO format. Should finally look like this :

8. Zip up your training file as obj.zip and testing file as test.zip and add them to your yolov4 folder in the drive :

Make another folder inside your yolov4 folder, name it as ‘backup’ as it will contain the trained weights of the your custom model.

9. Now let us move our custom dataset into the colab and unzip it.

It will unzip your zipped dataset in VM.

10. Configuring our files for training.

11. After running the above code you will have to make few changes in your config file which is newly created in your yolov4 folder as yolov4-obj.cfg.

Open it in text Editor.

Change the max_batch to 6000 if you have classes less than or equal to 3. If you have more than 3 classes then the preferred max_batch is equals to (class_no. * 2000).
Change Steps = 80% of the batch size, 90% of the max_batch. I have 4800,5400.
search for classes by pressing ctrl + f and change all the classes to the number of classes I have 2 classes.
The place where you will change the classes you will find the filters above it.
Change the filter as {(no. of classes + 5) * 3} I have 21. (2+5)*3 = 21.
You will have to do it for 3 times as you will find 3 classes and 3 filters.

For more details about the configuration file you can visit AlexeyAB(readme).

12. Upload back your .cfg file to cloud VM.

!cp /mydrive/yolov4/yolov4-obj.cfg ./cfg

13. Copy your obj.name and obj.data file into your VM.

14. Generate a train.txt file and test.txt file in your google drive.

15. Download the pre-trained weights for convolutional Layers.

16. Now train you custom object detector :)
But it will take a lot of time to get trained and google colab will kick you out of the notebook and disconnect the kernel. So before training your own model open the console of your colab site and paste the JS code to keep your colab awake. This code will let the colab know that you are still working on it.

Write it in the Console of the colab.

17. Start Training your object detector.

If you are kicked off the training then you can resume it as we were already storing the weights in our backup folder made previously in our goolge drive yolov4 folder.

Check the Mean Average Precision called mAP of the model.

If you think your final weights file has overfitted then it is important to run these mAP commands to see if one of the previously saved weights is a more accurate model for your classes.

18. Run your Custom Object Detector and See the result.

use plt.imshow() function to see the output image.

Input Image :

Output Image :

You can try it with your own dataset to build your own custom object detector using FREE gpu on the google colab notebook.

Acknowledgment :

I wish to express my sincere gratitude to NAANIZ ,my mentors Anshul Maheshwari and Deekshant Khitoliya and my teammates Mayuresh Sawant and Palak Agarwal for their generous guidance and useful suggestions. Naaniz has always helped each and every member to explore new and interesting field and resonate the best out of us.

Reference :
[1] YOLOv4: Optimal Speed and Accuracy of Object Detection.

Link to Colab :
https://drive.google.com/file/d/1TceG0HEf8ZzGszdwdWeALYW1f2NFVG3G/view?usp=sharing

Thanks for Reading.
Happy Learning..!!

--

--

Prashant Shekhar

Data Scientist | Big Data Consultant | Business Automation