Welcome to follow my public account [Jizhi Vision], reply 001 to get Google programming specification

O_o >_< O_o O_o ~_~ O_o

Hi, I’m Jizhi. This article introduces how to compile Darknet and YOLO training on Ubuntu.

1. Build Darknet

1.1 build opencv

Install CUDA and CUDNN. For details on how to compile And cross-compile OpencV for x86, AARCH64, and ARM32 environments, please refer to my previous article. It records the method of compiling OpencV on x86, AARCH64 and ARM32 platforms, concise and effective.

1.2 compile darknet

Clone source code:

git clone https://github.com/AlexeyAB/darknet.git

cd darknet
Copy the code

Alter Makefile, open GPU, OpencV, OpenMP

GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
AVX=0
OPENMP=1
LIBSO=1
ZED_CAMERA=0
ZED_CAMERA_v2_8=0
Copy the code

Then start compiling, very simple:

make -j32
Copy the code

Verify that the installation is successful:

./darknet detect cfg/yolov3.cfg cfg/yolov3.weights data/dog.jpg
Copy the code

Weights for yolov3.weights to download: pjreddie.com/media/files…

This will generate a classic applications.jpg chart in the

directory:

Yolo training

2.1 Make VOC data sets

You can make your own data set in VOC format, or you can train directly with VOC data.

How to make VOC format data, you can refer to my article: [Experience sharing] Target detection VOC format data set production, which is introduced in more detail.

2.2 Yolo training

Once you have the data set, then get the model structure files and pre-training weights and you can start the happy alchemy. In CFG folder is already provides many model structure of the document, such as yolov3. CFG, yolov3 – tiny. CFG, yolov4. CFG, yolov4 – tiny. CFG, etc., you just need to find the corresponding weights training beforehand, such as:

  • Yolov3.cfg –> Darknet 53.conv.74 Teleport: pjreddie.com/media/files…
  • Yolov3 – tiny. CFG – > yolov3 – tiny. Conv. 11 teleport: drive.google.com/file/d/18v3…
  • CFG –> yolov4-tiny.cfg –> yolov4-tiny.conv.29 下 载 : github.com/AlexeyAB/da…
  • CFG –> yolov4.conv.137 下 载 : drive.google.com/open?id=1JK…

Next, let’s take YoloV4 as an example to start our pleasant training journey.

I’m in a non-desktop environment, so I added the -dont_show pass parameter.

./darknet detector train cfg/voc.data cfg/yolov4.cfg yolov4.conv.137 -dont_show
Copy the code

The./ Darknet Detector Train is fixed, other:

  • CFG/VOC. data: transfer training data;
  • CFG /yolov4. CFG: pass-training model structure;
  • Yolov4.conv.137: Weight of pretraining

The command to execute the training above is very clear, look at voc.data:

Classes = 20 # number target detection type "train" = / home/pjreddie/data/voc/train. TXT # valid training dataset = / home/pjreddie/data/voc/test. TXT # test data set Names = data/voc.names # category name backup = /home/pjreddie/backup/Copy the code

In.cfg we can also make some changes to our training situation, mainly the parameters in [net] :

[net] Batch =64 # Batch =32 # Add more Data to the Batch /subdivision divisions. If the GPU memory is insufficient, add more data to the subdivision divisions# TrainingDecay =0.0005 # Weight decay regular term Angle =0 # increase the number of training samples by rotating the Angle saturation = 1.5 # Increase the number of training samples by adjusting the saturation of pictures Exposure = 1.5 # Increase the number of training samples by adjusting the exposure = 1 # Burn_in =1000 # Burn_in =1000 # Burn_in =1000 # Burn_in =1000 # Burn_in =1000 # Burn_in =1000 # If the number of batches is larger than this parameter, a policy update is used to assign a batch of batches to 500500. When this parameter is used, the training batches stop. Here it means that the learning rate decreases by 10 times when scales are 400000 and 450000, respectively, because the scales gradually converge to.1 and.1
#cutmix=1                 The # cutMix transform is a way to enhance dataMosaic =1 # Mosaic transform, is a way of data enhancementCopy the code

In addition to this, if you are training your own data set, the number of categories detected may not be the official 20, so there are also some changes to be made to the YOLO layer. Take one of the YOLO layers:

. [convolutional] size=1 stride=1 pad=1 filters=75 # filters= 3*(classes+5), Activation =linear [yolo] mask = 6,7,8 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72 146, 142, 110, 192, 243, 459, Num =9 jitter=.3 ignore_thresh =.7 Truth_thresh =1 random=1 scale_x_y = 1.05 iou_thresh=0.213 Cls_normalizer =1.0 iou_normalizer=0.07 iou_loss= Ciou nMS_kind = Greedynms beta_NMS =0.6 max_delta=5Copy the code

Many anchors in the YOLO layer need to be modified for different tasks, such as the human anchor frame, which needs to be tall and thin, or the car anchor frame, which needs to be narrow and wide, and parameters such as the confidence threshold and NMS threshold need to be adjusted during training.

Then explain why you need to modify the convolutional filters in the yOLO layer. I have done some analysis in this article “[Experience Sharing] Analyzing darknet entry_index pointer offset logic”. We need to take the data layout of the YOLO layer as an example:

(1) According to the four dimensions [N, C, H, W], N is Batch, C is 3 * (5 + classes), and H/W is feature_map height and width. C = 3 * (1 + 4 + classes), where 1 is the confidence level, 4 is the location information of the check box, classes is the number of classes, i.e. each class gives a check score, multiplied by 3 means that each cell has three anchor boxes. In this way, the four-dimensional data layout accepted by YOLO layer is formed, that is, the output data layout of the upper layer of YOLO.

(2) As for the output of YOLO layer, Darknet will use one-dimensional dynamic array to store the data of YOLO layer, which involves how to convert four-dimensional data into one-dimensional data. This is done in darknet, assuming that the four-dimensional data is [N, C, H, W] and the corresponding index of each dimension is [N, C, H, W], then the expansion is N *C*H*W + C*H*W + H*W + W, and stored in *output according to such logic.

It should be easy to understand why the filters convolved on yOLO are 3 * (classes + 5).

Ok, now let’s start training, or execute:

./darknet detector train cfg/voc.data cfg/yolov4.cfg yolov4.conv.137 -dont_show
Copy the code

If you need to keep a training log, you can do this:

./darknet detector train cfg/voc.data cfg/yolov4l.cfg yolov4.conv.137 2>1 | tee visualization/train_yolov4.log
Copy the code

The console will output the training log:

Backup = /home/pjreddie/backup/ saves the final and intermediate weight files of the training. If the effect is satisfactory, it can be deployed. For target detection, the indicator to measure the effect is usually MAP.


Well, I’ve shared with you how to compile Darknet and train YOLO on Ubuntu. I hope my sharing has helped you a little bit.


Ubuntu Darknet and YOLO Training