YOLOV5 V6.1 详细训练方法

安装

conda create -n yolov5-6.1 python=3.8
conda activate yolov5-6.1
cd 你的目标目录
git clone GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
mv yolov5 yolov5-6.1
cd yolov5-6.1
pip install -r requirements.txt

Note:导出模型需要按照export.py 内说明安装对应的包,下面以导出TRT为例。

pip install onnx onnx-simplifier onnxruntime-gpu

训练

1、标注

一般工业还是需要使用如labelme等标注,格式为coco格式(class_id x y w x)xywx均需要归一化,类别号从0开始,一个框一行。

2、目录的组织形式:

以“images” 命名图片路径,标签路径仅仅是将“images” 变为“labels”;代码自动对应images和labels,所以一张图片的标签需要和图片同名(如果没有对应的label 就是认为是背景图,没有目标)。

高阶使用

1)使用文件夹(列表)文件夹存储所有的训练图片(由于磁盘文件系统,单个文件夹文件过多影响吞吐速度)。

单个文件夹对应配置文件的写法

path: ../datasets/VOC

train: # train images (relative to 'path')

  - images/train2007

val: # val images (relative to 'path')

  - images/test2007

test: # test images (optional)

  - images/test2007

多个文件夹对应配置文件的写法

path: ../datasets/VOC

train: # train images (relative to 'path')

  - images/train2012

  - images/train2007

val: # val images (relative to 'path')

  - images/test2007

test: # test images (optional)

  - images/test2007

2)使用文本文件(列表)存储图片

单个文本文件对应配置文件的写法

path: ../datasets/VOC

train: # train images (relative to 'path')

  - train2017.txt

val: # val images (relative to 'path')

  - val2017.txt

test: # test images (optional)

  - test-dev2017.txt

多个文本文件对应配置文件的写法

path: ../datasets/VOC

train: # train images (relative to 'path')

  - train2017.txt

  - train2012.txt

val: # val images (relative to 'path')

  - val2017.txt

test: # test images (optional)

  - test-dev2017.txt

参考源代码:

解析images

f = []  # image files
for p in path if isinstance(path, list) else [path]:
    p = Path(p)  # os-agnostic
    if p.is_dir():  # dir
        f += glob.glob(str(p / '**' / '*.*'), recursive=True)
        # f = list(p.rglob('*.*'))  # pathlib
    elif p.is_file():  # file
        with open(p) as t:
            t = t.read().strip().splitlines()
            parent = str(p.parent) + os.sep
            f += [x.replace('./', parent) if x.startswith('./') else x for x in t]  # local to global path
            # f += [p.parent / x.lstrip(os.sep) for x in t]  # local to global path (pathlib)
    else:
        raise FileNotFoundError(f'{prefix}{p} does not exist')
self.im_files = sorted(x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in IMG_FORMATS)

解析labels

def img2label_paths(img_paths):
    # Define label paths as a function of image paths
    sa, sb = f'{os.sep}images{os.sep}', f'{os.sep}labels{os.sep}'  # /images/, /labels/ substrings
    return [sb.join(x.rsplit(sa, 1)).rsplit('.', 1)[0] + '.txt' for x in img_paths]

更改配置文件

如 data/coco128.yaml,里面的每一项都需要按需更改。

3、训练

按需选择模型结构(s m l x)等参数,训练结果存储在runs/train/exp* 目录。

python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt

测试时间

python val.py --weights yolov5l.pt --batch-size 32 --task speed

4、评估模型

可以看run根目录下,对应的训练结果的保存。主要参考混淆矩阵、精度曲线、召回曲线、P_R曲线等

参考链接:

技术博客丨使用YOLOv5模型进行目标检测!

yolov5/dataloaders.py at f8722b4429e80f96be04b36e4efd84ce6583bfa1 · ultralytics/yolov5 · GitHub

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TigerZ*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值