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

Original article was published on Artificial Intelligence on Medium

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

Speed(AP) vs. Performance(FPS) graph of YOLOv4 and other models.


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 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. We have collected the 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 for run our code and use the free cloud 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 being with cloning and building Darknet.

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

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 this :

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 then to the yolov4 folder in your drive :

Make a backup folder too it will contain the trained weights of the you custom model.

9. Now lets move our custom dataset into the colab.

It will unzip your zipped dataset in VM.

10. Now configuring our files for training.

11. After running the above code you will have to make few changes with you config file 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 more then 8000–10000.
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.

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

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

13. Now 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 and google colab will kick you out of the notebook and disconnect the kernel. So before training your own model open the console of the 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 have 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.

Reference :
YOLOv4 🙂

Thanks for Reading.
Happy Learning..!!