YOLOv3训练自己的数据集(记录自己的学习过程)
这个内容借鉴于B站的一个博主比飞鸟贵重的多_HKL
1数据准备
采集数据集用的是一个屏幕捕获的脚本。
import numpy as np
import cv2
from PIL import ImageGrab
import time
count=0
while 1:
count=count+1
img = ImageGrab.grab(bbox = (430, 160, 1750, 920))
img_np = np.array(img)
frame = cv2.cvtColor(img_np,cv2.COLOR_BGR2RGB)
cv2.imshow('s',frame)
if(count==50):
t = time.time()*1000
cv2.imwrite('./data/'+str(int(t))+'.png',frame)
count=0
cv2.waitKey(5)
#cv2.destroyAllWindows()
我通过看王者荣耀直播的方式,用这个脚本自己截取屏幕作为训练的数据集。这里我采集了一百张数据集。接下来就是用标注软件labelImg对采集到的数据进行标记生成xml文件。
2.生成txt文件
这里我用的是一个maketxt.py的代码当然你也可以自行百度,代码如下
import os
import random
import shutil
if os.path.exists("./ImageSets/"): # 如果文件存在
shutil.rmtree("./ImageSets/")
os.makedirs('./ImageSets/')
else:
os.makedirs('./ImageSets/')
test_percent = 0.1
train_percent = 0.8
val_percent = 0.1
xmlfilepath = './xml'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = list(range(num))
num_val = int(num * val_percent)
num_test = int(num * test_percent)
num_train = int(num * train_percent)
train_list = random.sample(list, num_train)
for i in train_list:
list.remove(i)
test_list = random.sample(list, num_test)
for i in test_list:
list.remove(i)
val_list = list
ftest = open('./ImageSets/test.txt', 'w')
ftrain = open('./ImageSets/train.txt', 'w')
fval = open('./ImageSets/val.txt', 'w')
for i in range(num):
name = total_xml[i][:-4] + '\n'
if i in train_list:
ftrain.write(name)
elif i in test_list:
ftest.write(name)
else:
fval.write(name)
ftrain.close()
fval.close()
ftest.close()
这个maketxt.py的作用是生成ImageSet文件夹里面包含test.txt,train.txt,val.txt
接下来就是把voc_label.py文件中的第18行的类别改成你自己的类别。我这里是四类。注意66行的图片格式也改成你自己的格式
voc_label.py代码如下
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import shutil
if os.path.exists("./txt/"): # 如果文件存在
shutil.rmtree("./txt/")
os.makedirs('./txt/')
else:
os.makedirs('./txt/')
sets = ['train', 'test', 'val']
classes = ["hero","monster","dogface","tower"]
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('./xml/%s.xml' % (image_id))
out_file = open('./txt/%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:
os.remove("./"+image_set+".txt")
if not os.path.exists('./txt/'):
os.makedirs('./txt/')
image_ids = open('./ImageSets/%s.txt' % (image_set)).read().strip().split()
list_file = open('./%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('data/Images/%s.png\n' % (image_id))
convert_annotation(image_id)
list_file.close()
3修改cfg文件
找到每个yolo层相应的classes我这里等于4改成自己相应的类别。
及yolo的上一个卷积层filters=27这里27是3*(1+4+classes=4)
[convolutional]
size=1
stride=1
pad=1
filters=27
activation=linear
[yolo]
mask = 3,4,5
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=4
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
接下来看代码效果
只用于记录个人学习谢谢