TiantongWang / MyBlogs

Personal Notebook
1 stars 0 forks source link

How to train your own YOLO-v3 #5

Open TiantongWang opened 5 years ago

TiantongWang commented 5 years ago

How to train your own YOLO-v3

前段时间为了进行车辆检测,用自己的数据集训练了YOLO-v3,参考了很多博客和教程,现在总结一下训练步骤以及遇到的问题。

安装darknet

官网地址: https://pjreddie.com/darknet/yolo/

  1. clone darknet

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

  1. 修改makefile
GPU=1  #0或1
CUDNN=1  #0或1
OPENCV=0  #0或1
OPENMP=0
DEBUG=0
  1. 编译

make

准备训练数据

YOLO的标注格式为txt,每张图片对应着一个相同名字的txt,txt内容如下:

0

txt中每一行对应着图片中的一个目标,每行中的元素由空格分隔开,第一个为该目标的类别(此例中,0代表Car,1代表Truck);第2到5个元素分别为x, y, w, h,代表该目标bbx 的中心相对横坐标,中心相对纵坐标,相对宽度,相对高度。

当时我的数据集的标注为JSON格式,所以自己写了脚本把里面的标注信息提取出来并生成txt。

在darknet/下新建 yolo3_dataset文件夹,在yolo3_dataset里面新建JPEGImages文件夹和labels文件夹。JPEGImages里面存放着训练以及测试的图片,共2954张,labels里面存放所有图片的txt标注。

yolo3_dataset文件夹中还存放着train.txt和test.txt,里面分别存着训练图片的完整路径和测试图片的完整路径。

修改cfg文件中的voc.data

classes= 2    #修改为自己的类别数
train  = /home/learner/darknet/data/voc/train.txt   #修改为自己的路径 
valid  = /home/learner/darknet/data/voc/test.txt   #修改为自己的路径
names = /home/learner/darknet/data/voc.names  #修改见voc.names
backup = /home/learner/darknet/backup   #修改为自己的路径,输出的权重信息将存储其内

修改/darknet/data/voc.names

Car
Truck

下载预训练卷积层权重

wget https://pjreddie.com/media/files/darknet53.conv.74

下载后的权重放在darknet/文件夹下。

修改cfg/yolov3-voc.cfg

[net]
# Testing
# batch=64
# subdivisions=32   #每批训练的个数=batch/subvisions,根据自己GPU显存进行修改,显存不够改大一些
# Training
batch=64
subdivisions=16
width=416
height=416
channels=3
momentum=0.9
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1

learning_rate=0.001
burn_in=1000
max_batches = 50200  #训练步数
policy=steps
steps=40000,45000  #开始衰减的步数
scales=.1,.1

[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=leaky

.....

[convolutional]
size=1
stride=1
pad=1
filters=21   #filters = 3 * ( classes + 5 )   here,filters=3*(3+5)
activation=linear

[yolo]
mask = 6,7,8
anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
classes=2    #修改为自己的类别数
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1

[route]
layers = -4

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[upsample]
stride=2

[route]
layers = -1, 61

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=512
activation=leaky

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=512
activation=leaky

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=512
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=21    #filters = 3 * ( classes + 5 )   here,filters=3*(3+5)
activation=linear

[yolo]
mask = 3,4,5
anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
classes=2  #修改为自己的类别数
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1

[route]
layers = -4

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[upsample]
stride=2

[route]
layers = -1, 36

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=256
activation=leaky

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=256
activation=leaky

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=256
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=21    #filters = 3 * ( classes + 5 )   here,filters=3*(3+5)
activation=linear

[yolo]
mask = 0,1,2
anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
classes=2   #修改为自己的类别数
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1

训练模型

  1. 单GPU训练

运行

./darknet -i <gpu_id> detector train <data_cfg> <train_cfg> <weights>

例如:

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74
  1. 多GPU训练

运行

./darknet detector train <data_cfg> <model_cfg> <weights> -gpus <gpu_list>

例如:

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 -gpus 0,1,2,3

批量测试图片

参考https://blog.csdn.net/mieleizhi0522/article/details/79989754

Q&A

  1. YOLOv3无法加载图片 "cannot load image"

参考 https://blog.csdn.net/qq_34806812/article/details/81537842

  1. 测试时报错(error darknet: ./src/cuda.c:36: check_error: Assertion `0' failed.)

参考 https://blog.csdn.net/qq_26564783/article/details/84293003