Object Detection Using YOLO-V4
All about how to yolov4 in our custom dataset.
- How YOLO Works
- Object Detection Using YOLO-V4 on Custom Dataset
- Install Darknet by cloning the repository by following command
- Change makefile to have GPU and OPENCV enabled
- Configuring configuration file for training by making a copy of yolov4-tiny.cfg named namaste.cfg in my case.
- Creating obj.names and obj.data files
- Data creation part
How YOLO Works
-
YOLO works by deviding images into certain fixed cells and detect whether object is present their or not. If object is present, it will be detected and gives a output vector as below:
-
pc means probability of class whether there is dog or cat or both or neither. If present, it will be 1.0 else 0.0.
- bx,by,bh,bw means bounding box coordinates. It is a rectangle which encloses the object.
-
c means class. It is the class of the object. That means whether that is dog or cat in the case of detecting dog and cat.
-
After performing all above task it will perform a IOU which is Intersection Over Union in order to fine tune the output.
-
IOU is a measure of how much two boxes overlap. If two boxes overlap, it will be 1.0. If they don't overlap, it will be 0.0. Correct if IOU>=0.5 .
If there is not object in the cell, it will be 0.0 in all the output vector.
Architechture of YOLO-V4
There is mainly 4 layers in YOLO-V4 they are:
-
INPUT :
- This is the input layer. It is the input image.
-
Backbone :
- This is the backbone of the network. It is the feature extractor.
- For extracting features in the image, it uses CSPDarknet53 model
-
Neck :
- Here in this block extracted features are collected to feed to the Dense Prediction.
-
Dense Prediction :
- This is the dense prediction block. It is the final block of the network.
- Here YOLO-V3 used for detection. SO it does object detection, classification and plotting bounding box.
from google.colab import drive
drive.mount('/content/drive')
!ln -s /content/drive/My\ Drive/ /mydrive
!ls /mydrive
!git clone https://github.com/AlexeyAB/darknet
%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
!make
Configuring configuration file for training by making a copy of yolov4-tiny.cfg named namaste.cfg in my case.
- After running below code cell go inside darknet folder and then cfg folder, there u will find copy of yolov4-tiny.cfg, in my case it is named as namaste.cfg.
-
Open that in google docs and update the parameter as follows : Training-> subdivisions=16 max_batches = 6000 for two class and general formula to calculate batch size is given as:
steps = 80% of max_batches, 90% of max_batches
-
Now go to the bottom of the file where [yolo] resides, there classes are by default 80 you should set it according to how much classes you have. Do this for all classes which is under [yolo].
- Now Just above the [yolo] heading there is filters which you should change according to following formula :
- Do this for every [yolo] headings
!cp cfg/yolov4-tiny.cfg cfg/namaste.cfg
Creating obj.names and obj.data files
- Following code will create two files inside darknet/data/ and set the path to the train.txt and test.txt by creating it.
- It also create obj.data and obj.name file, where obj.data is the file which contains the classes path to train.txt, test.txt, backup folder where our model will reside and a path to the google drive folder and obj.names contains the classes name.
!echo $'Namaste' > data/obj.names
!echo -e 'classes= 2\ntrain = data/train.txt\nvalid = data/test.txt\nnames = data/obj.names\nbackup = /content/drive/MyDrive/yolo_data/' > data/obj.data
!mkdir data/obj
Data creation part
- This is the most crucial part of the project where you create a dataset for your project.
- Once you collect a data that you want to train yolo model, then you need to annotate it. Which can be done using labelimg tool and tutorial for this is here.
- After annotating your images upload it to github by making it in a zip file.
Following code will unzip the annotated data that you upload in a google drive.
!unzip /mydrive/yolo_data/multipleimages.zip -d data/obj
Following code will return a list of paths matching a pathname pattern.
import glob
images_list = glob.glob("data/obj/multipleimages/*.jpg")
print(images_list)
Following code will create train.txt file which is essential for training our model.
file = open("data/train.txt", "w")
file.write("\n".join(images_list))
file.close()
Following code will train YOLO-V4 model and save the model in your google drive path which you have given in a obj.data file.
!./darknet detector train data/obj.data cfg/namaste.cfg darknet53.conv.74 -dont_show