YOLOv5训练自己的数据集之详细过程篇 | 您所在的位置:网站首页 › yolov5使用自己的数据集 › YOLOv5训练自己的数据集之详细过程篇 |
软硬件配置:Ubuntu + Tesla m40 24GB + cuda10.2 + anaconda 一、环境部署 1、源码下载https://github.com/ultralytics/yolov5 2、环境配置创建虚拟环境 source create -n yolov5 python=3.7激活虚拟环境 source activate yolov5安装依赖库(cd到yolov5根目录requirements.txt下) pip install -r requirements.txtor 清华镜像源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txtwindows可能会出现以下错误: ERROR: Could not find a version that satisfies the requirement torch>=1.6.0 (from -r requirements.txt (line 12)) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2) ERROR: No matching distribution found for torch>=1.6.0 (from -r requirements.txt (line 12))原因是pytorch和torchvision的下载需要翻墙,我们需另外下载 附pytorch和torchvision网盘链接: 链接:https://pan.baidu.com/s/1Rm69ehzWaVAXmbXk4LND6w 提取码:sd3m Ubuntu上一切很顺利,不知道pytorch的安装是不是和系统有关 还素比较简单滴,集成好的依赖库就是香啊 3、设置环境路径选择File → Settings 数据存放位置和数据结构 trainval_percent = 0.8,train_percent = 0.8,可自己进行调整 (6)在labels中存放txt格式的标签信息,可用脚本读取Annotations生成txt import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join sets = ['train', 'test','val'] classes = ['corner'] 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('data/Annotations/%s.xml' % (image_id)) out_file = open('data/labels/%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: if not os.path.exists('data/labels/'): os.makedirs('data/labels/') image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split() list_file = open('data/%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() 2、修改文件(1)修改数据集方面的yaml文件 打开yolov5/data/coco.yaml (2)修改网络参数方面的yaml文件 打开yolov5/models/yolov5l.yaml yolov5s -> yolov5m -> yolov5l -> yolov5sx. 网络层数越深,宽度越宽 (3)修改train中的一些参数 等待~~~~~ 模型保存在yolov5/runs/exp/weights,保存有best.pt和last.pt 1、将需要检测的图片放在一个文件夹,修改yolov5/detect.py 2、可修改yolov5/utils/datasets.py,读取test.txt的检测图片路径进行检测 在训练过程中还是会因为服务器、显卡、工具包等遇到一系列问题,可以参考我的另一篇博文 YOLOv5训练自己的数据集之问题解决篇 嘿嘿嘿 ----------------------------------------------end---------------------------------------------------------- |
CopyRight 2018-2019 实验室设备网 版权所有 |