训练YOLO模型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30440627/article/details/80732536

摘自https://github.com/AlexeyAB/darknet

如何训练自己的数据模型

1、创建自己的yolo-obj.cfg文件:在同一目录下,复制yolo-voc.2.cfg的内容到yolo-obj.cfg。更改如下内容:

    batch = 64

    subdivision = 8   ,如果在训练过程中出现out of memory错误,将subdivision设置为更高,比如16\32

    【region】下的classes = 类型的数量,根据实际需求

    【region】的前一个filters = (classes+5)×5

2、在darknet/data目录下创建obj.names文件,文件每一行为每个类的类名称。

3、在darknet/data目录下创建obj.data文件,内容为:

classes= 类的数量
train  = data/train.txt
valid  = data/test.txt
names = data/obj.names
backup = backup/

其中,train.txt、valid.txt内的每一行,分别为训练集、验证集的每一张图片的路径+图片名,比如

/home/ru/yolo_releated_data/data/yolo_data/train/TR00000000.jpg

4、新建文件夹存放训练集数据,放入图片的同时,为每个图片创建同名的.txt文件,内容为

<class> <x> <y> <width> <height>

class:该物体属于哪一类,数字形式,从0开始。

剩下4个均为归一化的数值,从0.0-1.0

x= absolute_x/image_width.    height = absolute_height/image_height

0 0.687109 0.379167 0.255469 0.158333

5、下载pre_trained weights文件,放入darknet/下

6、输入命令,开始训练。

训练到何时停止

1、随着训练的进行,avg(平均误差)不再降低。avg越小越好。

2、选择最合适的weights文件。

    训练会产生多个weights文件,为了防止overfitting(过拟合),要选取其中最合适的一个。

    过拟合:对训练集的效果很好,对之外的输入预测结果很差。

    为达到效果,需要做的有:

    2-1、训练时要设置验证集(validation set),如果没有数据,就用练集代替。

    2-2、对每一个weights文件进行对比

            darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-oby_7000.weights

            darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-oby_8000.weights

            darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-oby_9000.weights

           比较最后一行的输出,选择IoU(intersect of union)和mAP(mean average precision)最大的那个。

           

如何提高目标检测效果

训练前:

1、设置配置文件(.cfg)中的 random =1(是否随机确定最后一个预测框),可以提高训练精度。

2、提高配置文件中network resolution(分辨率),height = 608,width = 608或者任意32的倍数。

3、确保数据集中每个类都带有标签。

4、重新计算数据集中宽、高的anchors。

5、保证训练数据足够多,每个类至少有200张图片。确保图片有多种角度、亮度、背景、比例等。

6、确保训练数据中含有不带标签的不想被检测到的物体图片,即负样本。

7、如果每张图片中检测的物体数量较多,设置.cfg文件中最后一层【region】中的参数max=200或更大。

8、检测小物体时,设置【route】下layers =-1,11,【upsample】下stride =4。

9、为了加速训练,做微调,而不是迁移学习。设置stopbackward=1。

训练后:

Increase network-resolution by set in your .cfg-file (height=608 and width=608) or (height=832 and width=832) or (any value multiple of 32) - this increases the precision and makes it possible to detect small objects: link

  • you do not need to train the network again, just use .weights-file already trained for 416x416 resolution
  • if error Out of memory occurs then in .cfg-file you should increase subdivisions=16, 32 or 64

猜你喜欢

转载自blog.csdn.net/sinat_30440627/article/details/80732536