前言
之前写过一篇在win10下训练自己数据的博客,大家有需要的可以自行去查看:https://blog.csdn.net/qq_36417014/article/details/88577729
这一篇是在Ubuntu18.04环境下训练自己的数据。
目录
Yolov3:Ubuntu18.04下训练自己的数据
1、制作数据集
2、下载文件
3、处理数据
4、下载预训练权重
5、修改yolov3.cfg、voc.data和voc.names
6、可以训练了
关于制作数据集,这里就不再详细介绍了,大家可以自行查看上面那篇博客。主要是得到:.jpg数据和.xml数据。
在ubuntu的终端输入命令:
git clone https://github.com/pjreddie/darknet
cd darknet
make
在darknet/文件下面创建几个文件夹,如下所示:
VOCdevkit
——VOC2019
————Annotations(所有的.xml文件)
————JPEGImages(所有的.jpg文件)
————ImageSets
——————Main
————labels
————voc_2019.py
voc_2019.py内容如下:
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets/Main'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftest.write(name)
else:
fval.write(name)
else:
ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
在/home/humeng/darknet/VOCdevkit/VOC2019路径下运行voc_2019.py
在/home/humeng/darknet路径下运行voc_label.py,如下所示
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=[('2019', 'train'), ('2019', 'val'), ('2019', 'test')]
classes = ["cup"]
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def convert_annotation(year, image_id):
in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
list_file = open('%s_%s.txt'%(year, image_set), 'w')
for image_id in image_ids:
list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))
convert_annotation(year, image_id)
list_file.close()
则会在这个路径生成2019_train.txt、2019_val.txt、2019_test.txt文件。
数据处理完毕!!!
在终端输入:
wget https://pjreddie.com/media/files/yolov3.weights
这三个文件,我都是放在了darknet/目录下
(1)yolov3.cfg修改
修改四处:
#1、
# Training
batch=64
subdivisions=64
...
#2、3、4就是把每一个yolo层的classes改成自己的数据种类,我这里是1,然后把对应上一层convolutional的filters=3*(5+classes)=18
[convolutional]
size=1
stride=1
pad=1
filters=18
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=1
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
(2)voc.names
cup
(3)voc.data
classes= 1
train = /home/humeng/darknet/2019_train.txt
valid = /home/humeng/darknet/2019_val.txt
names = /home/humeng/darknet/voc.names
backup = backup/weights
./darknet detector train voc.data yolov3.cfg darknet53.conv.74 backup/weights