记录贴:运行Yolov5过程及遇到的问题

一、label的格式

Yolov5的label格式要求的是txt格式,如果遇到xml格式的代码,可以使用下列代码进行转换。

import os
import xml.etree.ElementTree as ET
dirpath = ''#存放xml的目录
newdir =  ''#存放txt的目录
if not os.path.exists(newdir):
    os.makedirs(newdir)
nameList = ['a','b']#数据有多少类别
for fp in os.listdir(dirpath):
    root = ET.parse(os.path.join(dirpath,fp)).getroot()
    xmin, ymin, xmax, ymax = 0,0,0,0
    sz = root.find('size')
    width = float(sz[0].text)
    height = float(sz[1].text)
    filename = root.find('filename').text
    for child in root.findall('object'):
        sub = child.find('bndbox')
        xmin = float(sub[0].text)
        ymin = float(sub[1].text)
        xmax = float(sub[2].text)
        ymax = float(sub[3].text)
        name = child.find('name').text
        if name not in nameList:
            nameList.append(name)
        idx = nameList.index(name)
        try: 
            x_center = (xmin + xmax) / (2 * width)
            y_center = (ymin + ymax) / (2 * height)
            w = (xmax - xmin) / width
            h = (ymax - ymin) / height
        except ZeroDivisionError:
            print(filename,'的 width有问题')
        with open(os.path.join(newdir, fp.split('.')[0]+'.txt'), 'a+') as f:
            f.write(' '.join([str(idx), str(x_center), str(y_center), str(w), str(h) + '\n']))

二、数据存放位置

可以创建一个新的文件夹,里面需要包含三部分文件。分别为train文件夹、valid文件夹和一个yaml格式的文件。trian和valid文件下面都包含有名为images的文件夹和名为labels的文件夹,用以储存图片和标签。
三个文件中train为训练集,valid为验证集,yaml用来存放数据的位置信息,具体需要包含以下四个信息,训练集图片的位置,验证机图片的位置,类别的数量,以及数据的类别以及名称。
示例:
数据存放在voc文件夹,voc文件夹与train.py文件同级别,voc文件夹里面有train、valid和名为data.yaml。
假设数据仅有a和b两个类别,那么mydata.yaml里需要包含以下四个信息,书写方式如下:

train: ./voc/train/images
val: ./voc/valid/images
nc: 2
names: ['a', 'b']

bug 1、Dataset not found.

yaml文件里的路径不对,按照报错修改路径即可。

bug 2、x names found for nc=x dataset in xx.yaml

yaml里的类别对不上,可能是yaml里n或names有问题。

三、开始训练

准备完数据后打开train.py文件,然后对一些参数进行修改。
首先需要修改参数位置,需要修改的代码如下:

parser.add_argument('--data', type=str, default='修改为前面写好的yaml的相对路径', help='data.yaml path')

接着就可以运行了。不过在第一次运行时,建议先将epoch和权重改小一点,运行成功了再改大参数。

bug 3、module ‘test’ has no attribute ‘test’

打开test.py文件,把里面的库和定义test的函数全部复制到train.py文件。

四、检测

打开detect.py,里面需要修改的参数有三个,weight,source和output。
需要需改的代码如下:

    parser.add_argument('--weights', nargs='+', type=str, default='修改为runs文件下最新的一次迭代里weights文件夹里的best.pt', help='model.pt path(s)')
    parser.add_argument('--source', type=str, default='需要检测的图片位置,为相对路径', help='source')
    parser.add_argument('--output', type=str, default='检测结果输出的位置,需要创建一个空的文件夹,不是空的会被清空', help='output folder')

bug4、'Image Not Found ’ + path

这里参考这个解决方案
将utils/datasets.py文件中的
p = str(Path(path).absolute())
改为p = str(Path(path)),重新运行代码即可

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而性能高,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。 本课程的YOLOv5使用ultralytics/yolov5,在Windows系统上做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集、修改配置文件、使用wandb训练可视化工具、训练自己的数据集、测试训练出的网络模型和性能统计。 希望学习Ubuntu上演示的同学,请前往 《YOLOv5(PyTorch)实战:训练自己的数据集(Ubuntu)》课程链接:https://edu.csdn.net/course/detail/30793  本人推出了有关YOLOv5目标检测的系列课程。请持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测实战:训练自己的数据集》Ubuntu系统 https://edu.csdn.net/course/detail/30793Windows系统 https://edu.csdn.net/course/detail/30923《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284       
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而高性能,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。  本课程的YOLOv5使用ultralytics/yolov5,在Windows和Ubuntu系统上分别做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集(自动划分训练集和验证集)、修改配置文件、使用wandb训练可视化工具、训练自己的数据集、测试训练出的网络模型和性能统计。 除本课程《YOLOv5实战训练自己的数据集(Windows和Ubuntu演示)》外,本人推出了有关YOLOv5目标检测的系列课程。请持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209 《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284  
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值