A Repository to Train a Custom Yolov4 based object detector for road damage detection using the RDD2020 dataset.
-
Dataset is taken from RDD2020: https://data.mendeley.com/datasets/5ty2wb6gvg/1
Deeksha Arya, Hiroya Maeda, Sanjay Kumar Ghosh, Durga Toshniwal, Hiroshi Omata, Takehiro Kashiyama, Toshikazu Seto, Alexander Mraz, Yoshihide Sekimoto
-
RDD2020 dataset comprising 26,336 road images from India, Japan, and the Czech Republic with more than 31,000 instances of road damage.
-
Here we consider a subset of the dataset, i.e. the images from India alone, to reduce complexity.
-
There are four types of road damage: longitudinal cracks (D00), transverse cracks (D10), alligator cracks (D20), and potholes (D40).
-
The data is present in the PascalVOC format as bounding boxes labelled as xmin, ymin, xmax and ymax, stored as xml files.
YOLO: you look only once. YOLO is an object detection system in real-time. Please read the paper for more information here .
At the root of the project clone the darknet repository using the command:
git clone https://github.com/AlexeyAB/darknet
This will create a folder called 'darknet' at the root of the project.
-
Collect the images and xml annotation files from RDD2020 into a single folder 'Data_India'.
-
The annotation of the bounding boxes are currently in an xml format, for example :
<annotation> <folder>images</folder> <filename>India_000312.jpg</filename> <size> <depth>3</depth> <width>720</width> <height>720</height> </size> <object> <name>D00</name> <bndbox> <xmin>420</xmin> <ymin>489</ymin> <xmax>546</xmax> <ymax>715</ymax> </bndbox> </object> </annotation>
-
This has to be converted into the YOLO format. The YOLO format for the above xml file is :
0 0.6694444444444445 0.8347222222222223 0.17500000000000002 0.3138888888888889
-
In order to perform this, open the script
pascal_yolo_conversion.py
and edit the variables below. Here 'dir_path' denotes the folder where all images and xml files are stored, and 'classes' denotes the names of class of objects to detect.dir_path = 'Data_India/' classes = ['D00', 'D10', 'D20', 'D40', 'D44']
-
Open command line cmd at the root of the repository.
-
Run the command
python pascal_yolo_conversion.py
-
A new folder called 'YOLO' is created in the 'dir_path' folder. Copy the contents of the 'YOLO' folder to the 'dir_path' folder.
-
You can now remove all the xml files.
-
Finally, your data for training the YOLOv4 model is now ready.
-
Create a configs folder at the root. This will contain all config files related to configuring the YOLO model.
-
obj.data
: Change the number of classes to number of classes you are working on. Create a training folder at the root, and this will store your training weigths. Make sure to check all other paths. It is best to provide path relative to root. Check the sample provided. -
obj.names
: On every new line mention the names or labels of the objects to be detected. Check the sample provided. -
yolov4-custom.cfg
: This is a very important file and requires 5 important parameters changes. This file is also present in the 'darknet/cfg' folder as 'yolov4-custom.cfg'.- Recommended having batch = 64 and subdivisions = 16 for ultimate results. If you run into any issues then up subdivisions to 32.
- Set max_batches = (# of classes) * 2000 (but no less than 6000). So if you are training for 1, 2, or 3 classes it will be 6000, however detector for 5 classes would have max_batches=10000.
- Set steps = (80% of max_batches), (90% of max_batches) (so if your max_batches = 10000, then steps = 8000, 9000).
- Search for classes and set it to number of classes. (You should find it 3 times).
- Above every classes, some lines above you should find a parameter names filters. Set filters = (# of classes + 5) * 3 (so if you are training for one class then your filters = 18, but if you are training for 4 classes then your filters = 27).
These files contain the path of the images to be used for training and testing relative to the darknet folder.
-
Inside the folder 'Data_India/' create two folders 'train' and 'test'.
-
Divide all images and xml files present in the folder 'Data_India/' into the 'train' and 'test' folders in a 90:10 ratio (or 80:20, if there are sufficient training images). You can do this manually or write a script for the same. The python package
split-folders
at pypi can also be helpful in this case. -
Once the folders 'train' and 'test' are created, now run the commands at the root of the project:
python gen_train.py
python gen_test.py
-
Make sure that your paths present in the scripts are with respect to the root of the darknet folder.
-
Sample 'train.txt' and 'test.txt' is provided in the 'configs' folder.
Download and save pre-trained weights and save it in the configs folder using the command:
!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137
-
Open the Notebook
Training_Notebook.ipynb
to follow all the steps for training the model. The command for training the yolo model is:!./darknet detector train [path to obj.data] [path to yolov4-obj.cfg] [path to pre-trained weights] -dont_show -map
-
Every 100 iterations the weigths are stored in the folder mentioned in the 'obj.data' as parameter backup. And for every 1000 iteration a new weights file is created.
-
The training can be stopped once the MAP value reduces to around 1 and stabilizes (varies from application to application). This graph is shown in the next section.
-
In case your notebook crashes or your server is down while training, you can resume your training with the following command:
!./darknet detector train [path to obj.data] [path to yolov4-obj.cfg] [path to training/backup/yolov4-obj_last.weights] -dont_show
-
For checking the final MAP value of your model, enter the command:
!./darknet detector map [path to obj.data] [path to yolov4-obj.cfg] [path to weights file which you want to check MAP for]
-
The training curve of MAP (Mean Average Precision) vs the iteration number is stored in the folder darknet as 'chart.png'.
This can be used to understand when to stop the training while evaluating the MAP value.
MAP is an evaluation metric, commonly used in the field of computer vision used for object detection (i.e. localisation and classification)
-
The training in the start is visualized in the graph below.
-
The final results of our trained models is shown in image below.
Some samples of images tested with our model are shown below.
As we can see from the below samples, the model is not in its best version and need to train for longer duration of time and iterations and also we can use data from remaining countries as well, i.e. Japan and the Czech Republic.
Use of Darknet
References: The model for YOLOv4 is taken from repository of AlexeyAB.
The whole project is developed with python version Python 3.7.7
and pip version pip 19.2.3
.
In case of error, feel free to contact us over Linkedin at Adnan and Niloy.