- 快速开始项目:
- 准备自己的数据集
- 创建对应的配置文件: 'data/xx.yaml'
- 修改预训练的配置文件:'models/yolov5s.yaml'
- 下载最新预训练模型到weights目录下
- 开始训练:
python train.py --img 640 --batch 16 --epochs 500 --data go.yaml --weights weights/yolov5s.pt
(可以修改train.py中的相关参数的默认配置,方便下次训练) - 测试:
python test.py --data data/go.yaml --weights E:\GDUT\python_project\ObjectDection\Yolov5\runs\train\exp13\weights\best.pt --augment
- 检测:
python detect.py --weight runs\train\exp14\weights\best.pt--source data/testImg
- tensbard查看:
tensorboard --logdir=runs
-
数据集的准备:
-
认识voc与yolo两种格式的数据集:
voc数据的格式 :参考- folder: 文件夹
filename:文件名
database: 数据库名
annotation: 标记文件格式
size:图像尺寸,width宽、height高,depth通道数
segmented: 分割
object, name: 标签名;
pose:是否是姿势
truncated:是否被截断;
difficult:是否识别困难。
bndbox, 边界框位置 - https://www.cnblogs.com/sdu20112013/p/10801383.html
- https://www.sohu.com/a/333069232_823210
- 验证集+训练集不一定等于你手头中的所有图片
https://www.itdaan.com/blog/2016/11/19/4ac13e9a711e3804c559f84e6bd922b3.html
- folder: 文件夹
-
yolo标签的格式(需要归一化) 参考
x,y是目标的中心坐标,width,height是目标的宽和高
-
-
训练集、验证集与测试集的选择:
- https://zhuanlan.zhihu.com/p/48976706
-
相关脚本:
- 文件统一命名
## 将目录下面的图片从起始编号开始按顺序命名 import os path=os.getcwd() print("当前所在路径:"+path) path=input("输入文件路径:") if(path[-1]!="\\"): path=path+"\\" # C:/Users/zh/Desktop/围棋数据集补充/数据集图片/ a=input("输入起始编号:") type=input("文件后缀:") ##创建文件夹 # if not os.path.exists(res_path): # os.makedirs(res_path) f=os.listdir(path) for index,i in enumerate(f): if os.path.isfile(path+i) and (path+i).endswith("."+type): ## 重命名并删除 if (os.path.exists(path+str(int(a)+index)+"."+type)): print("文件名冲突") else: os.rename(path+i,path+str(int(a)+index)+"."+type)
- voc训练集与验证集以及测试集的划分
# 参考:https://my.oschina.net/u/4870686/blog/4803148 # 作用: 划分xml文件名到四个txt文件去 # 说明: 数据集全部拿来训练(其中80%作为训练集20%作为验证集),不留测试集 train_and_valid=1.0 train_percent = 0.8 ##输入xml文件路径: xml_file_path=input("请输入xml文件路径:") txt_save_path=input("将要保存的路径:") if(xml_file_path==""): xml_file_path='Annotations' if(txt_save_path==""): txt_save_path='ImageSets/Main' # xml文件对象 total_xml = os.listdir(xml_file_path) if not os.path.exists(txt_save_path): os.makedirs(txt_save_path) num = len(total_xml) list_index = range(num) num_train_and_valid = int(num*train_and_valid) num_train = int(num_train_and_valid * train_percent) ## 从数据集中选择出用于训练的部分 index_train_and_valid = random.sample(list_index,num_train_and_valid) ## 训练集的编号(在上一步随机的基础上在随机挑选) index_train = random.sample(index_train_and_valid,num_train) file_train_and_valid = open(txt_save_path + '/trainval.txt', 'w') file_test = open(txt_save_path + '/test.txt', 'w') file_train = open(txt_save_path + '/train.txt', 'w') file_val = open(txt_save_path + '/val.txt', 'w') ## for i in list_index: ## 获取每个文件名(去除后缀) name = total_xml[i][:-4] + '\n' if i in index_train_and_valid : ## 训练+验证 file_train_and_valid.write(name) if i in index_train: ## 训练 file_train.write(name) else: ## 验证 file_val.write(name) ## 测试集 else: file_test.write(name) file_train_and_valid .close() file_train.close() file_val.close() file_test.close()
- voc格式转yolo格式
import xml.etree.ElementTree as ET import os from os import getcwd dir_type = ['train', 'val'] classes = ["wdj"] # 改成自己的类别 abs_path = os.getcwd() print(abs_path) ## voc to Yolo 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(image_id): ## 读取xml in_file = open('Annotations/%s.xml' % (image_id), encoding='UTF-8') ## 创建 将要保存的文件 out_file = open('labels/%s.txt' % (image_id), 'w') # xml工具 tree = ET.parse(in_file) root = tree.getroot() size = root.find('size') ## 图片的w\h 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)) b1, b2, b3, b4 = b # 标注越界修正 if b2 > w: b2 = w if b4 > h: b4 = h b = (b1, b2, b3, b4) bb = convert((w, h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd() ## E:\GDUT\python_project\ObjectDection\dataset\wdj2 test_file=open("file.txt","w") test_file.write("测试内容") ##分别读取三个划分文件: for dir in dir_type: if not os.path.exists('labels/'): os.makedirs('labels/') ##从划分的txt文件中获取图片id image_ids = open('ImageSets/Main/%s.txt' % (dir)).read().strip().split() ##根目录创建txt文件保存图片绝对路径: ##list_file = open('%s.txt' % (dir), 'w') for image_id in image_ids: ## 图片写入指定的目录 ##list_file.write(abs_path + '\\JPGEImages\\%s.jpg\n' % (image_id)) convert_annotation(image_id) ##list_file.close()
- yolo数据集中训练集、验证集、测试集的划分
## 将yolo数据集的格式 按照比例进行划分 用于训练 # 训练集、验证集和测试集的比例分配 test_percent = 0 valid_percent = 0.2 train_percent = 0.8 # 标注文件的路径 srcImg_path=input("源图片路径:") label_path=input("标签文件路劲:") if(srcImg_path==""): image_path = 'JPEGImages' if(label_path==""): label_path = 'labels' ##目标存储文件夹: save_path=input("Yolo数据集存储位置:") if(save_path[-1]!="\\"): save_path=save_path+"\\" ##获取文件夹下的文件对象 images_files_list = os.listdir(image_path) labels_files_list = os.listdir(label_path) total_num = len(images_files_list) test_num = int(total_num * test_percent) valid_num = int(total_num * valid_percent) train_num = int(total_num * train_percent) # 对应文件的索引 test_image_index = random.sample(range(total_num), test_num) valid_image_index = random.sample(range(total_num), valid_num) train_image_index = random.sample(range(total_num), train_num) dir=["train","valid","test"] sub_dir=["images","labels"] for d in dir: if not os.path.exists(save_path+d): os.makedirs(save_path+d) for sd in sub_dir: if not os.path.exists(save_path+d+"/"+sd): os.makedirs(save_path+d+"/"+sd) for i in range(total_num): if i in test_image_index: # 将图片和标签文件拷贝到对应文件夹下 shutil.copyfile('JPEGImages/{}'.format(images_files_list[i]), save_path+'test/images/{}'.format(images_files_list[i])) shutil.copyfile('labels/{}'.format(labels_files_list[i]), save_path+'test/labels/{}'.format(labels_files_list[i])) elif i in valid_image_index: shutil.copyfile('JPEGImages/{}'.format(images_files_list[i]), save_path+'valid/images/{}'.format(images_files_list[i])) shutil.copyfile('labels/{}'.format(labels_files_list[i]), save_path+'valid/labels/{}'.format(labels_files_list[i])) else: shutil.copyfile('JPEGImages/{}'.format(images_files_list[i]), save_path+'train/images/{}'.format(images_files_list[i])) shutil.copyfile('labels/{}'.format(labels_files_list[i]), save_path+'train/labels/{}'.format(labels_files_list[i]))