下载darknet主体
https://github.com/AlexeyAB/darknet
主体及权重百度云下载:1314
下载训练权重并放入darknet文件夹
yolov4-tiny.weights
yolov4-tiny.conv.29
修改darknet文件夹下的makefile
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
AVX=0
OPENMP=1
LIBSO=1
make 或 make -j4 (4个CPU一起编译)
新建文件夹object,在object目录下
mkdir -p VOCdevkit && cd VOCdevkit
mkdir -p VOC2020 && cd VOC2020
mkdir -p Annotations && mkdir -p ImageSets && mkdir -p JPEGImages && mkdir -p labels
将图像集放入到JPEGImages文件夹下
将打好标签的.xml文件放入到Annotations文件夹下
创建txt.py脚本和label.py脚本,修改路径并先后执行(label要修改sets,classes和os.system)
import os
import random
trainval_percent = 0.5
train_percent = 0.5
xmlfilepath = 'VOCdevkit/VOC2020/Annotations'
txtsavepath = 'VOCdevkit/VOC2020/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(txtsavepath + '/trainval.txt', 'w')
ftest = open(txtsavepath + '/test.txt', 'w')
ftrain = open(txtsavepath + '/train.txt', 'w')
fval = open(txtsavepath + '/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets=[('2020', 'train'), ('2020', 'val'), ('2020', 'test')]
classes = ["1"]
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()
os.system("cat 2020_train.txt 2020_val.txt 2020_train.txt 2020_val.txt > train.txt")
os.system("cat 2020_train.txt 2020_val.txt 2020_test.txt 2020_train.txt 2020_val.txt > train.all.txt")
在darknet文件夹下新建coco.names,填写识别物体的名称
在darknet文件夹下新建backup文件夹,存放训练完生成的权重
在darknet文件夹下新建coco.data,填写刚才运行py程序生成的txt文件地址
classes=
train = /2020_train.txt
valid = /2020_val.txt
names = /coco.names
backup = /backup/
修改cfg目录下的yolov4-tiny.cfg文件开头
batch=16
subdivisions=64
max_batches=训练次数
steps=max_batches × 80% ,max_batches × 90%
修改classes和filters,将classes及其最近的filters改成:
classes=
filters=(classes+5)*3=
训练代码
./darknet detector train coco.data cfg/yolov4-tiny.cfg yolov4-tiny.conv.29
使用训练完的权重进行预测
修改cfg目录下的yolov4-tiny.cfg文件开头
batch=64
subdivisions=1
darknet目录下输入
./darknet detector demo coco.data cfg/yolov4-tiny.cfg #训练完生成的权重位置# /dev/video0