YoloV4 own sample production and training

1 Introduction

The previous article "Two ways to compile YOLOv4 with VS2015 under Windows, and the solution to the undefined problem of __func__" records the problems involved in the compilation of YoloV4 under Windows.
The author of YOLO comes with several weight files for download, including common daily objects, which can be directly used for testing and recognition, but if you want to recognize the objects we specified, you need to make samples and train yourself. This article records myself The process of making samples, labeling samples, and training samples.

2 downloads

Before labeling and training, you need to download several files:
Sample labeling software: yolo_mark , after downloading the source code, compile it with VS.
Pre-training weight file: yolov4.conv.137

3 Directories and files to be prepared

3.1 Contents

The author of YOLO suggested placing it under ...build\darknet\x64, but it can be placed in any directory, because there are too many files in this darknet\x64 directory, so I built a d:\YoloV4\My directory to put all the work document.
Then create the following directory under this directory:
d:\YoloV4\My\data directory, which is used to store training data files.
d:\YoloV4\My\data\obj directory, used to store training sample images.
d:\YoloV4\My\Backup directory, used to store temporary weight files during training.

3.2 CFG files


Function: Network configuration file, you need to use the yolov4-custom.cfg attached to the source code during training and recognition, copy a copy to the d:\YoloV4\My directory, and then rename it to your own file name, such as yolov4- my .cfg.
Before making the next modification, you must first determine the number of object types you want to recognize, because the following parameters are related to this, we assume classes=4 classes.
Open yolov4-my.cfg with a text editor, and modify as follows:
batch=64
subdivisions=16, the author suggested, but later found that this value is not working, I changed it to subdivisions=64
max_batches=8000, this value=classes*2000, but This value cannot be less than the number of training pictures, nor less than 6000
steps=6400,7200, these two values ​​are 80% and 90% of max_batches.
width=416 height=416, network size, these two values ​​must be a multiple of 32.
classes=4, there are 3 places in the file that need to be modified, and you can modify them after searching for classes.
filters=27, the formula filters=(classes + 5)x3, and there are 3 places in the corresponding classes. Note that it must be the immediate neighbor after the classes are searched. The filters in the previous [convolutional] section need to be modified like this

3.3 obj.names file

Function: The object name file needs to be used during training and recognition.
Under d:\YoloV4\My\data, create a new text file and rename it to obj.names (you can take your favorite name, including the extension, as follows All files can be named by themselves, the same below), according to the determined number of classes, write an object name per line, for example

plane
car
bus
pizza
...

When the network recognizes the object, the result is the serial number of the object, and the text in the line corresponding to the serial number in this document is the name of the recognized object.

3.4 obj.data file

Function: training data file, used during training
Under d:\YoloV4\My\data, create a new text file, rename it to obj.data, and its content is

classes = 4
train  = data/train.txt
valid  = data/test.txt
names = data/obj.names
backup = backup/

The function of each line in the file is:
classes: specify the number of object types to be trained and recognized
train: specify the path and file name of the training sample description file, see below
valid: validation_dataset file path and file name, not used in this example, here Know that this is proposed to reduce the occurrence of overfitting.
names: specify the path and file name of the object name file
backup: the directory used to store temporary weight files during training

4 Sample preparation and labeling

4.1 Sample preparation

Prepare pictures for labeling and training, in JPG format, and put them in the d:\YoloV4\My\data\obj directory.

4.2 train.txt file

Function: Training sample description file, used
in d:\YoloV4\My\data during labeling and training, create a new text file, rename it to train.txt, write one per line in this file according to the sample prepared above The path and name of the image file, similar to the following:

data/obj/img1.jpg
data/obj/imga.jpg
data/obj/img2.jpg
...

4.3 Sample labeling

After the above samples and files are ready, we need to use the yolo_mark sample labeling software downloaded earlier. For convenience, we copy yolo_mark.exe to d:\YoloV4\My, and then run the command line (or the most convenient The method: create a mark.cmd file in this directory, copy the following text into it, and then double-click this cmd to run):

yolo_mark.exe data/obj data/train.txt data/obj.names

Then you can frame by frame, specify the name of the object, and select it with the mouse, as shown in the figure below, so I won’t go into details here.
insert image description here

After the annotation is completed, we will see that in the directory where the sample images are saved, a txt file with the same name appears for each image, which records the result of the sample annotation. Open it to see the content is roughly as follows:

3 0.849609 0.839583 0.111719 0.120833
3 0.130859 0.591667 0.177344 0.058333
0 0.692578 0.313889 0.042969 0.016667
1 0.685547 0.568056 0.035156 0.036111

Each line corresponds to a label box, meaning:
< object-class> < x_center> <y_center> < width> < height>
object-class: the serial number of the object category, starting from 0
<x_center><y_center>: the center X of the label box and Y coordinates, the proportions of the image width and height respectively
<width><height>: the width and height of the annotation box, the proportions of the image width and height respectively.
Knowing this principle, you can write your own program to generate sample annotation results if necessary.

5 training

After all the above are prepared, you can start training.
At this time, we need to use the yolov4.conv.137 downloaded before, the pre-training weight file, copy it to d:\YoloV4\My, and run the following command in this path (because our directory is not where darknet.exe is located directory, so write the full path name) (the most convenient way is to create a cmd file and double-click to run)

D:\YoloV4\darknet-master\build\darknet\x64\darknet.exe detector train data/obj.data yolov4-my.cfg yolov4.conv.137

If it is normal, the following interface will appear and the training process will start.
insert image description here
The graph shows the change curve of the loss function, the horizontal axis is the number of iterations, and the vertical axis is the loss rate. The average loss rate will start from more than 1,000, gradually decrease, and start to decrease very quickly. The figure below shows the curve after I trained for more than an hour, but the decline becomes slower as it goes forward.
insert image description here
During the training process, the trained weight file will be generated in the d:\YoloV4\My\Backup directory. The training software will automatically save the weight file every 100 iterations, and a new weight file will be generated every 1000 iterations, which is convenient for training. compare the best.
Judgment of the end of training: Usually each category corresponds to 2000 iterations, and the average loss rate is generally 0.xxx, and basically no change after multiple iterations, it is almost possible to stop training.
After the training, the generated weight file *.weights, and the aforementioned yolov4-my.cfg and obj.names can be used for recognition.
The training process is a long one, and it usually takes more than ten or even dozens of hours. For testing, I used more than 200 samples to train 4 targets. The training took about 3 hours and iterated 1200 times. The Avg loss was about When it dropped to 0.8, the training was stopped halfway. After a test, the recognition has been very good.

Guess you like

Origin blog.csdn.net/hangl_ciom/article/details/119645009