-
疾速开始我的项目:
- 筹备本人的数据集
- 创立对应的配置文件: ‘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/sdu20…
- https://www.sohu.com/a/333069…
- 验证集 + 训练集不肯定等于你手头中的所有图片
https://www.itdaan.com/blog/2…
- folder: 文件夹
-
yolo 标签的格局(须要归一化) 参考
<object-class> <x> <y> <width> <height>
x,y 是指标的核心坐标,width,height 是指标的宽和高
-
-
训练集、验证集与测试集的抉择:
- https://zhuanlan.zhihu.com/p/…
-
相干脚本:
-
文件对立命名
## 将目录上面的图片从起始编号开始按程序命名 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]))
-