Article directory
The script file and program download link corresponding to this article is an example of the whole process of darkent yolo custom data labeling training , which can also be obtained by sending a private message.
1. Label data
Use the labelImg.exe tool to label the data. By default, the voc dataset format is used, and then the script is used to convert it to the yolo-specific data format, and generate some training-dependent preparation data.
Preparation:
- (1) Execute script 0—clean up the img, xml, and txt files under the train file, and delete all the data of the last training (backup if necessary)
- (2) Copy the training image to the train/JPEGImages directory (it is recommended to scale the image to below 1080 if the image is too large)
Directory structure of exe and train
1.1. Labeling
Use script 1——LabelImg.cmd to open the tool,
button Open Dir to select the image data directory to be annotated, here is to_path/train/JPEGImages
button Change Save Dir , select the xml directory to store the annotation file, to_path/train/Annotations
After loading the picture, click the Edit label button to label the category label of the target
. Press a and d to switch pictures, press w to start drawing a standard frame, and ctrl+s to save the current labeled picture data.
Refer to the Internet content in detail, and search for yolov3 to mark custom data. After the annotation is completed, the xml annotation file corresponding to each image will be obtained from train/Annotations in the directory.
1.2. Generate the training list file train.txt
Training needs to give the absolute addresses of all pictures. Note here that the labeling machine may not be on the same machine as the later training, and the path will change. Later, batch modification and replacement will be performed according to the actual path.
Just execute script 2——Label_generate_traintxt.cmd , and the train.txt file will be generated in the current directory. The directory in
the red box may change as follows, and can be automatically generated by modifying the parameters of the script
1.3. Convert data annotation format
exe/label_conver_voc_2_yolo.py
First modify the classes list in the script file to be the label name in the labeling tool. For example, if there are 4 labels car, huoche, guache and keche, then comment the previous line with # and modify it as shown in the figure below .
After modification and saving, execute script 3——Label_conver_voc_2_yolo.cmd, and then you can see the label file in txt format in the directory train\labels. The
format is 标签序号 x y w h
: here the label serial number starts from 0, and the value is the subscript of the classes list in the python script; position It is a percentage of the relative image width and height.
This is actually obtained by converting the xml file of train/Annotations in the annotation file directory, one-to-one correspondence ( txt, xml, and pictures must be one-to-one correspondence ).
Problems that may arise:
- The width and height of the image in the xml file marked by LabelImage.exe is 0, resulting in a txt conversion error, and an error is reported for training. The script uses the PIL library to add a verification and repair link to ensure that the conversion is correct.
- Prompt that the label does not exist: because the lable input error when labeling, it is inconsistent with the elements of the classes list in the script
2. Organize training data
Under the training tool directory darknet on the training machine, copy the label file directory train and the list file train.txt to the new training directory such as data-vehicle-0926-772 . The directory structure here is as follows
2.1. Modify the train.txt path
Modify the real path of the picture list file path, as explained in 1.2, Generate training list file train.txt. Pay attention to the newline format, which requires unix and utf8 formats . It may make mistakes in training, but it will prompt STB similar errors.
Cannot load image “…” STB Reason: can’t fopen… Segmentation fault (core dumped)
2.2. Modify yolov3.cfg
Copy a 原始yolov3.cfg
file from the darknet directory to the current training directory. There are two quick ways to modify
-
(1) Modify network structure parameters
Find in the cfg file
所有[yolo]标签段(共3个)
, modify the data classes in the red box
to the number of categories when marking, for example, 4 categories , and modify the filters to (number of categories + 5) * 3, here it is modified to (4 + 5) * 3 = 27 . (Other categories can be modified accordingly)
Note that there are 3 sets of data to be modified in total. -
(2) Modify training parameters
The parameters here are relatively professional. When training with a large amount of data, it is necessary to continuously adjust the parameter design. Currently we use the default design. Use batch=64, subdivision=16 during training . If the GPU performance of the machine is good and the video memory is sufficient, the subdivision can be reduced to 8, 4, 2, etc. (3090 can be modified to 2).
2.3, obj.name and obj.data
just modify it
2.4. Training script file trian.sh
The script content is
cd ..
./darknet detector train data-vehicle-0926-772/obj.data data-vehicle-0926-772/yolov3.cfg /yolov3.cfg darknet53.conv.74 -i 0 -clear
For example, the existing script file on the machine is
2.5. Test script file test.sh
(After the training is completed) Use the specified model file for training to test the picture and generate the result picture.
For example, execute in the darknet directory of the server
./darknet
detector
test
car-0612-400/obj.data
car-0612-400/yolov3-tiny.cfg
car-0612-400/weights/yolov3-tiny_best.weights
-i 0 -thresh 0.25
./car-0612-400/train/JPEGImages/11.jpg
-ext_output
The console will print the detection results, and after the operation is complete, a predictions.jpg file will be generated.
3. Training
In the training data directory, just execute the train.sh training script , check whether there is any error according to the console training data input, and make targeted adjustments.
During the formal training, it will be executed in the background, and nohup train.sh &
the nohup.out training log can be generated by using the command.
For the output in the middle of training, pay attention to the avg loss in the middle. Intuitively, the lower the better.
You can use the script drawLossPlot.py to draw the loss (train) curve later