TRAIN A CUSTOM YOLOv4-tiny OBJECT DETECTOR USING GOOGLE COLAB

Tutorial for beginners

Techzizou
Techzizou
Feb 24 · 13 min read

In this tutorial, we will be training our custom detector for mask detection using YOLOv4-tiny and Darknet. YOLOv4-tiny is preferable for real-time object detection because of its faster inference time.

My YouTube video on this!

HOW TO BEGIN?

FOLLOW THESE 12 STEPS TO TRAIN AN OBJECT DETECTOR USING YOLOv4-tiny

LET'S BEGIN !!!

Original Video by cottonbro from Pexels

1) Clone Darknet git repository

!git clone https://github.com/AlexeyAB/darknet
Cloned Darknet git repo on Colab VM

2) Create ‘yolov4-tiny’ and ‘training’ folders in your drive

3) Create & upload the following files which we need for training a custom detector

a. Labeled Custom Dataset
b. Custom cfg file
c. obj.data and obj.names files
d. process.py file (to create train.txt and test.txt files for training)

Labeling your Dataset

Original Photo by Ali Pazani from Pexels
labelImg GUI for Image1.jpg
OpenLabeling Tool GUI
Image1.txt

3(a) Create and upload the labeled custom dataset “obj.zip” file to the “yolov4-tiny” folder on your drive

obj folder containing both the input image files and the YOLO labeled text files

3(b) Create your custom config file and upload it to the ‘yolov4-tiny’ folder on your drive

3(c) Create your “obj.data” and “obj.names” files and upload them to your drive

obj.data

obj.names

3(d) Upload the process.py script file to your drive

process.py script

4) Mount drive and link your folder

Mount drive

%cd ..
from google.colab import drive
drive.mount('/content/gdrive')

Link your folder

!ln -s /content/gdrive/My\ Drive/ /mydrive

5) Make changes in the makefile to enable OPENCV and GPU

%cd darknet/
!sed -i 's/OPENCV=0/OPENCV=1/' Makefile
!sed -i 's/GPU=0/GPU=1/' Makefile
!sed -i 's/CUDNN=0/CUDNN=1/' Makefile
!sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile
!sed -i 's/LIBSO=0/LIBSO=1/' Makefile

6) Run make command to build darknet

!make

7) Copy all the files from the ‘yolov4-tiny' folder to the ‘darknet’ directory in Colab VM

%cd data/
!find -maxdepth 1 -type f -exec rm -rf {} \;
%cd ..
%rm -rf cfg/
%mkdir cfg
!cp /mydrive/yolov4-tiny/obj.zip ../!unzip ../obj.zip -d data/
!cp /mydrive/yolov4-tiny/yolov4-tiny-custom.cfg ./cfg
!cp /mydrive/yolov4-tiny/obj.names ./data
!cp /mydrive/yolov4-tiny/obj.data ./data
!cp /mydrive/yolov4-tiny/process.py ./

8) Run the process.py python script to create the train.txt & test.txt files inside the data folder

!python process.py
!ls data/
train.txt & test.txt files

9) Download the pre-trained YOLOv4-tiny weights

!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29

10) Training

Train your custom detector

!./darknet detector train data/obj.data cfg/yolov4-tiny-custom.cfg yolov4-tiny.conv.29 -dont_show -map

To restart your training (In case the training does not finish and you get disconnected)

!./darknet detector train data/obj.data cfg/yolov4-tiny-custom.cfg /mydrive/yolov4-tiny/training/yolov4-tiny-custom_last.weights -dont_show -map

Use this simple hack for Auto-Click to avoid being kicked off Colab VM

function ClickConnect(){
console.log("Working");
document
.querySelector('#top-toolbar > colab-connect-button')
.shadowRoot.querySelector('#connect')
.click()
}
setInterval(ClickConnect,60000)

11) Check performance

Define helper function imShow

def imShow(path):import cv2
import matplotlib.pyplot as plt
%matplotlib inline
image = cv2.imread(path)
height, width = image.shape[:2]
resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)
fig = plt.gcf()
fig.set_size_inches(18, 10)
plt.axis(“off”)
plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
plt.show()

Check the training chart

imShow('chart.png')
!./darknet detector map data/obj.data cfg/yolov4-tiny-custom.cfg /mydrive/yolov4-tiny/training/yolov4-tiny-custom_xxxx.weights -points 0

12) Test your custom Object Detector

Make changes to your custom config file to set it to test mode

%cd cfg
!sed -i 's/batch=64/batch=1/' yolov4-tiny-custom.cfg
!sed -i 's/subdivisions=16/subdivisions=1/' yolov4-tiny-custom.cfg
%cd ..

Run detector on an image

!./darknet detector test data/obj.data cfg/yolov4-tiny-custom.cfg /mydrive/yolov4-tiny/training/yolov4-tiny-custom_best.weights /mydrive/mask_test_images/image1.jpg -thresh 0.3imShow('predictions.jpg')
Original Photo by Norma Mortenson from Pexels

Run detector on webcam images

Detection on webcam image

Run detector on a video

!./darknet detector demo data/obj.data cfg/yolov4-tiny-custom.cfg /mydrive/yolov4-tiny/training/yolov4-tiny-custom_best.weights -dont_show /mydrive/mask_test_videos/test1.mp4 -thresh 0.7 -i 0 -out_filename /mydrive/mask_test_videos/results1.avi
Original Video by Pavel Danilyuk from Pexels

Run detector on a live webcam

Detection on live webcam

NOTE:

Original Video by Max Fischer from Pexels

My GitHub

My Labeled Dataset (obj.zip)

My Colab notebook for YOLOv4-tiny training

My YouTube video on this!

CREDITS

References

Dataset Sources

Mask Dataset Sources

Video Sources

Don’t forget to leave a 👏

Have a great day !!! ✌

♕ TECHZIZOU ♕

Original Video by Nothing Ahead from Pexels

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…