写博客仅仅是为了记录自己的实验过程,详细过程建议参见以下两位博主的博客。
参考博文:
YOLOV3训练自己的数据集(PyTorch版本)
Pytorch 版YOLOV3训练自己的数据集
1.环境配置
git clone https://github.com/ultralytics/yolov3.git
并根据 requirements.txt 下载需要的依赖文件。
2.yolo
使用pytorch框架,yolo代码在https://github.com/ultralytics/yolov3下载。
将Annotations和JPEGImages放到data目录下,新建ImageSets又来存放和labels。将JPEGImages复制重命名为images。
图片标注不会的可以看一下labelImg使用。
3.makeTxt.py
在根目录下新建makeTxt.py
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'data/Annotations' # xml文件
txtsavepath = 'data/ImageSets' # 生成的训练集,测试集,验证集位置
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('data/ImageSets/trainval.txt', 'w')
ftest = open('data/ImageSets/test.txt', 'w') # 生成测试集
ftrain = open('data/ImageSets/train.txt', 'w') # 生成训练集
fval = open('data/ImageSets/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()
4.voc_label.py
在根目录下新建voc_label.py,得到labels的具体内容以及data目录下的train.txt,test.txt,val.txt,这里的train.txt与之前的区别在于,不仅仅得到文件名,还有文件的具体路径。voc_label.py的代码如下
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test','val']
classes = ['dog','person'] #填写类别的名字,与后面data/voc.names相同
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
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(image_id):
in_file = open('data/Annotations/%s.xml' % (image_id))
out_file = open('data/labels/%s.txt' % (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()
print(wd)
for image_set in sets:
if not os.path.exists('data/labels/'):
os.makedirs('data/labels/')
image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()
list_file = open('data/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('data/images/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
有时会出现labels有××××××.txt,但里面没有内容,可以查看一下文件目录是否正确。
5.配置文件
在data目录下新建voc.data和voc.names
voc.data内容
classes=1
train=data/train.txt
valid=data/test.txt
names=data/voc.names
backup=backup/
voc.names内容
dog
person
网络参数配置
本次使用的是yolov3-tiny.cfg
需要修改两部分
[net]
# Testing
# batch=1
# subdivisions=1
Training
batch=64
# subdivisions=2
...
[convolutional]
size=1
stride=1
pad=1
filters=21
activation=linear
[yolo]
mask = 3,4,5
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=2
num=6
jitter=.3
ignore_thresh = .7
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, 8
[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky
[convolutional]
size=1
stride=1
pad=1
filters=21 #3*(class +5)
activation=linear
[yolo]
mask = 0,1,2
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=2
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
权重参数
使用 yolov3-tiny.weights,下载地址https://pjreddie.com/media/files/yolov3-tiny.weights,需要对yolov3-tiny.weights进行改造,因而需要下载官网的代码https://github.com/pjreddie/darknet,运行一下脚本,并将得到的yolov3-tiny.conv.15导入weights目录下,脚本如下
./darknet partial cfg/yolov3-tiny.cfg yolov3-tiny.weights yolov3-tiny.conv.15 15
6.训练
打开终端
python train.py --data data/rbc.data --cfg cfg/yolov3-tiny.cfg --epochs 10
未完…