欢迎关注我的公众号 [极智视界],回复001获取Google编程规范
O_o
>_<
o_O
O_o
~_~
o_O
Hello everyone, I am Jizhi Vision. This article introduces the method of compiling darknet and yolo training on ubuntu.
1. Compile darknet
1.1 Compile opencv
I won't say much about the installation of cuda and cudnn. For the compilation of opencv, please refer to the " [Experience Sharing] x86, aarch64, arm32 environment compilation / cross-compilation method of opencv " which I wrote before, which records in x86, aarch64 and arm32 The method of compiling opencv on the platform is concise and effective.
1.2 Compile darknet
clone source code:
git clone https://github.com/AlexeyAB/darknet.git
cd darknet
复制代码
Modify the Makefile to open gpu, opencv, and openmp:
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
AVX=0
OPENMP=1
LIBSO=1
ZED_CAMERA=0
ZED_CAMERA_v2_8=0
复制代码
Then start compiling, it's very simple:
make -j32
复制代码
After that, verify that the installation was successful:
./darknet detect cfg/yolov3.cfg cfg/yolov3.weights data/dog.jpg
复制代码
Of course yolov3.weights needs to be downloaded and sent by yourself: pjreddie.com/media/files…
After the operation is successful, a very classic detection map predictions.jpg will be generated in the <darknet-path>
directory :
2. Yolo training
2.1 Making a VOC dataset
You can make your own dataset in VOC format, or you can directly train with VOC data.
For how to create VOC format data, you can refer to my article: " [Experience Sharing] Target Detection VOC Format Dataset Production ", which is described in more detail.
2.2 Yolo training
After you have the data set, you can start a pleasant journey of alchemy with the model structure file and pre-training weights. In fact, a lot of model structure files have been provided in the cfg folder, such as yolov3.cfg, yolov3-tiny.cfg, yolov4.cfg, yolov4-tiny.cfg, etc. You only need to find the corresponding pre-training weights, such as :
- yolov3.cfg ---> darknet53.conv.74 传送:pjreddie.com/media/files…
- yolov3-tiny.cfg ---> yolov3-tiny.conv.11 传送:drive.google.com/file/d/18v3…
- yolov4-tiny.cfg ---> yolov4-tiny.conv.29 传送:github.com/AlexeyAB/da…
- yolov4.cfg ---> yolov4.conv.137 传送:drive.google.com/open?id=1JK…
接下来我们以 yolov4 为例,开启咱们愉快的训练之旅吧。
我这里是非桌面环境,所以加了 -dont_show
传参。
./darknet detector train cfg/voc.data cfg/yolov4.cfg yolov4.conv.137 -dont_show
复制代码
来看以上命令,./darknet detector train
是固定的,其他:
- cfg/voc.data:传训练数据;
- cfg/yolov4.cfg:传训练模型结构;
- yolov4.conv.137:传预训练权重
以上执行训练的命令十分清晰,来看一下 voc.data:
classes= 20 # 目标检测类别数
train = /home/pjreddie/data/voc/train.txt # 训练数据集
valid = /home/pjreddie/data/voc/test.txt # 测试数据集
names = data/voc.names # 类别名称
backup = /home/pjreddie/backup/ # 训练过程中间权重备份目录
复制代码
在 .cfg 中我们也可以针对自己的训练情况做一些改动,主要是 [net] 内的一些参数:
[net]
batch=64 # batch 设置
subdivisions=32 # 每次传进 batch/subdivision 的数据,若gpu显存不够用,把这个参增大
# Training
width=608 # 图片宽
height=608 # 图片高
channels=3 # 通道数
momentum=0.949 # 动量,影响梯度下降到最优值得速度
decay=0.0005 # 权重衰减正则项,用于防止过拟合
angle=0 # 通过旋转角度增多训练样本
saturation = 1.5 # 通过调整图片饱和度来增多训练样本
exposure = 1.5 # 通过调整曝光度来增多训练样本
hue=.1 # 通过调整色调来增多训练样本
learning_rate=0.0013 # 学习率,这个参数比较重要,决定训练收敛快慢及是否能达到好的效果
burn_in=1000 # 学习率设置相关,当小于该参时更新有一种方式,大于该参时采用policy更新方式
max_batches = 500500 # 训练批次到这个参的时候停止训练
policy=steps # 学习率调整策略
steps=400000,450000 # step和scales是配合使用,这里的意思到400000和450000的时候学习率分别衰减10倍,因为后面慢慢收敛了
scales=.1,.1
#cutmix=1 # cutmix变换,是数据增强的一种方式
mosaic=1 # mosaic变换,是数据增强的一种方式
复制代码
除了这些外,如果你是训练自己的数据集,检测的类别数就不一定是官方给的 20 了,所以对于 yolo 层也需要做一些修改,拿其中一个 yolo 层来说:
...
[convolutional]
size=1
stride=1
pad=1
filters=75 # filters = 3*(classes+5),这个需要根据你的 classes 数目进行相应修改
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, 401
classes=20 # 检测类别数
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=5
复制代码
yolo 层中的 anchors 对于不同的检测任务也需要做一定的修改,比如检测人,锚框就需要瘦高型的,如检测车,可能更倾向于窄宽型的锚框,然后像一些置信度阈值、nms阈值等参数也需要训练的时候做一些调参。
然后解释一下为什么需要修改 yolo 上一层卷积的 filters,这个我在这篇《【经验分享】剖析 darknet entry_index 指针偏移逻辑》有做过一定的分析,需要从 yolo 层的数据排布来说:
(1)数据按四维 [N, C, H, W] 来说,N 为 batch,C 为 3 * (5 + classes)
、H / W 为 feature_map 高和宽。需要解释一下 C,C = 3 * (1 + 4 + classes)
,其中 1 表示置信度,4 为检测框位置信息,classes 为类别数,即每个类别给出一个检测得分,乘 3 表示每个格子有 3 个锚框。这样就形成了 yolo 层接受的四维数据排布,也就是 yolo 上一层的输出数据排布;
(2)至于 yolo 层的输出,darknet 里会用一维动态数组来存放 yolo 层的数据,这里就涉及到怎么将四维数据转换为一维数据的问题。darknet 里是这么做的,假设四维数据为 [N, C, H, W] ,每个维度对应的索引为 [n, c, h, w],那么展开就是 n*C*H*W + c*H*W + h*W + w
,按这样的逻辑存放到 *output 中。
这样回过头来看应该比较好理解为什么 yolo 上一层卷积的 filters 为 3 * (classes + 5)
了。
好了,接下来我们开始训练吧,还是执行:
./darknet detector train cfg/voc.data cfg/yolov4.cfg yolov4.conv.137 -dont_show
复制代码
如果需要保存训练日志,可以这么做:
./darknet detector train cfg/voc.data cfg/yolov4l.cfg yolov4.conv.137 2>1 | tee visualization/train_yolov4.log
复制代码
控制台会输出训练日志:
等训练完就会在 backup = /home/pjreddie/backup/
保存训练得到的最终及中间权重文件。如果效果满意的话就可以拿去部署,对于目标检测来说,衡量效果怎么样的指标一般就是 map 了。
好了,以上分享了 ubuntu 上编译 darknet 以及训练 yolo 的方法,希望我的分享能对你的学习有一点帮助。
[Official account transmission] " [Model training] Ubuntu compiles Darknet and YOLO training "