点击 这里 获取项目完整开源代码
点击 这里 获取本项目数据集——密码: wreu
点击 这里 获取另外两个小项目:识别人民币、识别地下城的人物(数据集在项目中)
在阅读本项目前,推荐几个入门学习视频:
学习完以上的这些知识,就可以开始我们的项目了
首先,给大家看一看我们项目最终实现的结果:
为了便于大家理解,下文将分步骤教大家如何使用这个项目:
data/images目录:存放png/jpg文件
data/xml目录:存放标签xml文件
data/makeTxt.py:运行后划分训练集、测试集、验证集(见data/Imagesets目录)
data/voc_label.py:将xml文件转换为txt文件,运行后生成data/txt目录(注意:修改classes,你需要训练几个类就填写哪几个类)
模型的预处理:cfg目录下,更改全连接层(convolutional)的filters和yolo(yolo)层的classes(注意:有四处需要更改,直接搜索即可)
那么下面就要开始训练啦!
经过长时间的训练,大家会获得两个pt文件,在weights目录下(best.pt、last.pt)
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()
if os.path.exists("./txt/"): # 如果文件存在
shutil.rmtree("./txt/")
os.makedirs('./txt/')
else:
os.makedirs('./txt/')
sets = ['train', 'test', 'val']
classes = ['football', 'person']
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 = 1920
h = 1080
for obj in root.iter('object'):
difficult = obj.find('difficult').text
# 类别
cls = obj.find('name').text
# 如果类别不在这个类里面或者difficult
if cls not in classes or int(difficult) == 1:
continue
# 获取类别的index
cls_id = classes.index(cls)
# 得到xmlbox
xmlbox = obj.find('bndbox')
xmin = 0
xmax = 0
ymin = 0
ymax = 0
if float(xmlbox.find('xmin').text)>float(xmlbox.find('xmax').text):
xmin = float(xmlbox.find('xmax').text)
xmax = float(xmlbox.find('xmin').text)
else:
xmin = float(xmlbox.find('xmin').text)
xmax = float(xmlbox.find('xmax').text)
if float(xmlbox.find('ymin').text)>float(xmlbox.find('ymax').text):
ymin = float(xmlbox.find('ymax').text)
ymax = float(xmlbox.find('ymin').text)
else:
ymin = float(xmlbox.find('ymin').text)
ymax = float(xmlbox.find('ymax').text)
b = (xmin,xmax,ymin,ymax)
# 归一化
bb = convert((w, h), b)
# 写成txt文件
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('./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.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
其他的代码就不一一展示了,具体见文前的github项目,欢迎star