How to train YOLOv4 for custom objects detection in Google Colab

sam
AI WORLD
Published in
5 min readMay 12, 2020

This article is a step by step guide to make the custom object detection using yolo v4. Training the yolo in colab gives the advantage of utilizing the free gpu provided by google.When i started to train my project on colab i stumbled upon many things. Through this article i hope i could clear the path for many.If you are novice it could be a bit overwhelming but make a checklist containing each step and focus on completing one at a time.The links to the papers and references are given at the end.

PART A-setting up yolov4 files

In this part we are going to label the image,download the YoloV4 and prepare the contents.Finally we will upload it to Google Drive.If you have already done these please skip to part b

  1. I am assuming that you have the images of various class objects that you want to detect.So the next step is labeling,which is just creating a .txt file containing the class id(ie; the image belonging to which class object)and the position of the object in the image.you should go to the following link and get the tool for labeling Link
  2. The next step is to download the YoloV4 to download proceed to the following link and click on clone or download->download zip. After downloading extract the file
  3. Now we have to make changes to the file we have downloaded,This include steps as follows

a. Open darknet-master folder which we have just downloaded and from that open cfg folder now in the cfg folder make a copy of the file yolo4-custom.cfg now rename the copy file to yolo-obj.cfg

  • open the file yolo-obj.cfg and change max_batches to (classes*2000),if you have 6 object classes change max_batches=12000.
  • Then change the line steps to (0.8*max_batches ,0.9*max_batches) ie; if you have 6 classes steps=9600,10800.
  • set network size width=416 height=416
  • change line classes=80 to your number of objects in each of 3 yolo layers.
  • change [filters=255] to filters=(classes + 5)x3 in the 3 convolutional layer immediately before each 3 yolo layers.If you have 6 classes filters=33

b. Download the pre trained weights from the link yolov4.conv.137 and save it in the darknet-master folder

c. Open wordpad and type the name of each object in separate lines and save the file as obj.names in darknet-master->data folder

d. Create file obj.data in the folder darknet-master->data, containing the given text (replace classes = number of objects)

classes= 6
train = data/train.txt
names = data/obj.names
backup = backup/

e. Create a folder in the directory darknet-master->data named obj now inside this obj folder put all your images and the respective txt files you got from labeling in step1

f. Now we have to create a train.txt file.This file directs to all training images as shown in the below picture.The easiest way to achieve this is store all images in a folder in computer open command prompt navigate to the folder using ‘cd’ and type command ‘ls’ if in linux and ‘dir’ if in windows.This will display all image names copy that and paste in text file and add ‘data/obj/’ to each line for this Find and replace option could be used.The train.txt file is stored in the darknet-master->data folder.

data/obj/img1.jpg
data/obj/img2.jpg
data/obj/img3.jpg
data/obj/img4.jpg
..
..

g.In the darknet-master folder open Makefile in wordpad and change GPU=1,CUDNN=1,OPENCV=1 as shown in the following picture.This is done to make the training on GPU.

GPU=1
CUDNN=1
CUDNN_HALF=0
OPENCV=1
AVX=0
OPENMP=0
LIBSO=0
ZED_CAMERA=0 # ZED SDK 3.0 and above
ZED_CAMERA_v2_8=0 # ZED SDK 2.X

PART B-setting up google colab

In this part the training is taking place.If you are unfamiliar with the colab environment please checkout this video.To work on colab obviously you need a google account.I suggest creating a new google account so that all your personal stuff and project didn't get mixed up.

  1. Upload to google drive the darknet-master.To upload click the + icon on top left and choose uploadfolder option. The uploading process may take a while sit back and relax:)
  2. Open a colab notebook by clicking on the link.Now on the file option select new notebook.If you are already signed it will open the new jupyter notebook.

a) In the edit menu at top left select notebook settings and turn the hardware accelerator to GPU.This will help us to utilize a GPU in colab

3.Now we have to mount our drive to colab.Use the following code for that.This will generate a link open that and copy the confirmation code back to the cell.

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

4. Now we have to change the directory to darknet-master.For that run the following code in a new cell.If your path to darknet-master is different than the given.Just find the folder in the left side tab and right click and select option copy path and change code accordingly.

%cd "/content/drive/My Drive/darknet-master/"

5.Compile darknet by using following code

!make
!chmod +x ./darknet

6.Now we have to convert certain files to unix for that execute the following codes(lately without this step everything is working perfectly so you could skip this step)

!sudo apt install dos2unix

!dos2unix ./data/train.txt
!dos2unix ./data/obj.data
!dos2unix ./data/obj.names
!dos2unix ./cfg/yolo-obj.cfg

7.Now finally we come to train the network.The training process could take several hours even days.But colab only allow a maximum of 12 hours of running time in ordinary accounts.What we could do is training by parts.after each 1000 epoch weights are saved in the backup folder so we could just retrain from there.For starting the trining run the code.

! ./darknet detector train data/obj.data cfg/yolo-obj.cfg yolov4.conv.137 -dont_show

BONUSES

  • If you stopped training at a particular point you could always restart training from there by using following code just lookup in the Darknet-master->Backup folder in the google drive and find the last weight stored there.Run the following code by replacing the ‘4000’ in code with the latest you find in backup folder
!./darknet detector train data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_4000.weights -dont_show
  • Colab will disconnect after experiencing inactivity for some time.so to avoid that press ctrl+shift+i simultaneously.This will open console and in the console paste the following code.
function ClickConnect(){
console.log("Working");
document.querySelector("colab-toolbar-button#connect").click()
}setInterval(ClickConnect,60000)

TESTING

For testing run the following code(replace ‘12000’ with latest on the backup folder)and enter the path of images we want to test the results will be shown right away and will be stored as a image named predictions.jpg

!./darknet detector test data/obj.data cfg/yolo-obj.cfg backup/yolo-obj_12000.weights

If this article have helped you or you have any suggestions i am happy to hear from you mail me at samgeogi@gmail.com

References

--

--

sam
AI WORLD

Mechanical Engineer, M.Tech from Government engineering College Thrissur,Passionate about AI and Robotics