Yolov4 environment setup

The environment is basically the same as yolov3, the difference being pre-train weights and conV connections

Cloning and Building Darknet

clone darknet from AlexeyAB’s famous repository,

git clone https://github.com/AlexeyAB/darknet
Copy the code

adjust the Makefile to enable OPENCV and GPU for 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
sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile
Copy the code

build darknet

make
Copy the code

Pre-trained yolov4 weights

YOLOv4 has been trained already on the coco dataset which has 80 classes that it can predict.

wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
Copy the code

Test env Enabled

./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg
Copy the code

Multiple Images at Once

  1. make a .txt file which has the paths to several images want to be detected at once

    data/person.jpg
    data/horses.jpg
    data/giraffe.jpg
    data/dog.jpg
    Copy the code
  2. save result to .json file

    ./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -ext_output -dont_show -out result.json < list.txt
    Copy the code
    [{
            "frame_id": 1."filename": "data/person.jpg"."objects": [{
                    "class_id": 17."name": "horse"."relative_coordinates": {
                        "center_x": 0.783550."center_y": 0.566949."width": 0.335207."height": 0.486880
                    },
                    "confidence": 0.997604
                },
                {
                    "class_id": 16."name": "dog"."relative_coordinates": {
                        "center_x": 0.206590."center_y": 0.722102."width": 0.229715."height": 0.210134
                    },
                    "confidence": 0.994348
                },
                {
                    "class_id": 0."name": "person"."relative_coordinates": {
                        "center_x": 0.364771."center_y": 0.558493."width": 0.134738."height": 0.669826
                    },
                    "confidence": 0.999949}},/ /...
    ]
    Copy the code


Yolo command line flags

  • -thresh: add a threshold for confidences on the detections, only detections with a confidence level above the threshold will be returned

  • -dont_show: not have the image outputted after running darknet

  • -ext_output: output bounding box coordinates

    dog: 99%	(left_x:   59   top_y:  262   width:  147   height:   89)
    person: 100%	(left_x:  190   top_y:   95   width:   86   height:  284)
    horse: 100%	(left_x:  394   top_y:  137   width:  215   height:  206)
    Copy the code


Yolov4 trains custom data sets

The general method is the same as that of YOLOv3, but yesterday’s training used more mature solutions, and I didn’t fully understand the specific steps, so I decided to learn from the beginning again in detail today

  • Labeled Custom Dataset
  • Custom .cfg file
  • obj.data and obj.names files
  • train.txt file (test.txt is optional here as well)

Gathering and Labeling a Custom Dataset

Using Google’s Open Images Dataset

Because the task of the laboratory is aimed at several specific toys, and there is no strong requirement on the expansibility. Therefore, Google’s open source data set is only used for learning, and it still adopts the method of annotating the data set to construct the data set

  • Google’s Open Images Dataset
  • OIDv4 toolkit

Manually Labeling Images with labelImg(Annotation Tool)

  • LabelImg tool to use


The data sets for train and VALID are now ready

Configuring Files for Training

cfg file

edit the yolov4.cfg to fit the needs based on the object detector

  • Bash =64 & SubDivisions =16: Online comparison of recommended parameters

    Subdivisions are set to 32 due to server capacity, but still slow

  • classes=4 in the three YOLO layers

  • filters=(classes + 5) * 3: three convolutional layers before the YOLO layers

  • width=416 & height=416: any multiple of 32, 416 is standard

    • improve results by making value larger like 608 but will slow down training
  • max_batches=(# of classes) * 2000: but no less than 6000

  • steps=(80% of max_batches), (90% of max_batches)

  • random=1: if run into memory issues or find the training taking a super long time, change three yolo layers from 1 to 0 to speed up training but slightly reduce accurancy of model

obj.names

one class name per line in the same order as dataset generation step

NOTE: don’t have spaces in class name, use _ for replacement

sheep
giraffe
cloud
snow
Copy the code

obj.data

classes= 4
train  = data/train.txt
valid  = data/test.txt
names = data/obj.names
backup = backup
Copy the code
  • backup: where save the weights to of the model throughout training

train.txt and test.txt

hold the reletive paths to all the training images and valididation images, it contain one line for each training image path or validation image path

Train Custom Object Detector

Download pre-trained weights for the convolutional layers. By using these weights it helps custom object detector to be way more accurate and not have to train as long.

wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137
Copy the code

train

./darknet detector train .. /.. /data/obj.data cfg/yolov4_custom.cfg yolov4.conv.137 -dont_showCopy the code

Checking the mAP of the Model

mAP: mean average precision

./darknet detector map .. /.. /data/obj.data cfg/yolov4_custom.cfg backup/yolov4_custom_last.weightsCopy the code

the highest mAP, the most accurate is