Day 92(DL) — YOLOv4 setup for Custom object detection

Photo by Ebuen Clemente Jr on Unsplash

Having discussed some of the Nitty-gritty of the YOLOv4 architecture, let’s explore how the pre-trained network of YOLOv4 can be leveraged for custom object detection. Similar to YOLOv5, every image is associated with a ‘.txt’ file which contains details in the form of object-class, x_center, y_center, width, height. we’ll go through the 9 steps explained in the original doc.

Step1: Creating custom configuration files for our task. For training, download the pre-trained weights yolov4.conv.137, this will be used for the custom config file cfg/yolov4-custom.cfg.

Step2: Now, we can make a copy of the custom configuration file and rename it as yolo-obj.cfg and the below modifications are made.

  • update batch size to 64 and subdivisions to 16.
  • max_batches to (classes * 2000) but not less than the number of training images.
  • line steps can be updated to 80% and 90% of max_batches.
  • The size of the network can be modified as width=416 and height=416 or it can be a multiple of 32.
  • Since it was trained with the coco dataset, the number of classes = 80, but this has to be modified according to the number of custom classes in 3 [yolo] layers.
  • The filter size should also be altered before .3 Yolo layers in accordance with the input number of classes. filters=(classes + 5)x3 in 3 convolutional layers just right before the [yolo] layers.
  • With these updates, the custom configuration file yolov4-custom.cfg differs from yolo-obj.cfg(modified version).

Step3: After copying the darknet repo, a new file obj.names should be created in the directory build\darknet\x64\data\ that contains each class name in each line.

Step4: The next step is to specify the location of the train and test images along with the class names. All of this information are added to obj.data in the same data directory as above.

Step5: All of the images have to be placed in the directory build\darknet\x64\data\obj\. The label details in the format of ‘.txt’ file should be moved to this location. Notes: The name of the text file and the image file should be the same.

Step6: Creating train.txt and test.txt files. It should contain the list of the image paths(training & testing).

Step7: The training can be kickstarted by using the following command, darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137

Step8: Once the training is completed, the weights can be retrieved from the backup folder for prediction.

Step9: The command for prediction using the trained weights, darknet.exe detector test data/obj.data yolo-obj.cfg yolo-obj_8000.weights

We’ll implement a custom detector yolov4 in the subsequent articles for more clarity.

Recommended Reading:

https://github.com/AlexeyAB/darknet

https://medium.com/analytics-vidhya/train-a-custom-yolov4-tiny-object-detector-using-google-colab-b58be08c9593

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store