yolo v5 数据标注和训练

yolo v5 数据集标注

安装labelimg软件

1.进入虚拟环境
2.pip install labelimg
3.直接输入labelimg就可以打开软件

conda activate your-name
pip install labelimg
labelimg

在这里插入图片描述

labelimg软件设置

这边数据格式一定要设置成yolo
在这里插入图片描述

开始标注

这个部分看labelimg使用(2.1-2.2部分)

yolo v5 数据集处理

1.标注完成后的图和生成的txt文件一一对应
在这里插入图片描述
2.数据集的格式

YOLO
├─ images
    ├─ test # 下面放测试集图片
    ├─ train # 下面放训练集图片	
	└─ val # 下面放验证集图片
└─ labels
	├─ test # 下面放测试集标签
	├─ train # 下面放训练集标签
	└─ val # 下面放验证集标签

3.在data目录下创建一个data.yaml的文件

# Custom data for safety helmet

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: d:\Users\zlx\Desktop\yolo\image\train
val: d:\Users\zlx\Desktop\yolo\image\val

# number of classes
nc: 10

# class names
names: ['准备', '直拳','摆拳','勾拳','鞭拳','肘击','横踢','前蹬','侧踹','膝法']

在这里插入图片描述

yolo v5 模型训练

1.在models下建立一个yolov5s.yaml的模型配置文件,如图:
在这里插入图片描述

# Parameters
nc: 10  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# YOLOv5 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Focus, [64, 3]],  # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4
   [-1, 3, C3, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8
   [-1, 9, C3, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16
   [-1, 9, C3, [512]],
   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32
   [-1, 1, SPP, [1024, [5, 9, 13]]],
   [-1, 3, C3, [1024, False]],  # 9
  ]

# YOLOv5 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, C3, [512, False]],  # 13

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, C3, [256, False]],  # 17 (P3/8-small)

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 14], 1, Concat, [1]],  # cat head P4
   [-1, 3, C3, [512, False]],  # 20 (P4/16-medium)

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 10], 1, Concat, [1]],  # cat head P5
   [-1, 3, C3, [1024, False]],  # 23 (P5/32-large)

   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

2.训练
在训练前先确认一下以下文件都有:
在这里插入图片描述
运行以下代码开始训练:
如果使用cpu训练的话,对应的改成--device cpu

python train.py --data data.yaml --cfg yolov5s.yaml --weights pt/yolov5s.pt --epoch 300 --batch-size 16 --device 0

在这里插入图片描述
所有训练结果都保存在runs/train/递增的运行目录中,例如runs/train/exp2runs/train/exp3等等。
其中模型在runs/trains/exp2/weightsbest.pt/last.pt,在exp2中还有各种指标图的图片。
在模型结束之后,可以在runs/trains中找到三张图,分别表示我们模型在验证集上的召回率、准确率和均值平均密度。
P_curve.pngPR_curve.pngR_curve.png
如果你的目录下没有这样的曲线,可能是因为你的模型训练一半就停止了,没有执行验证的过程,你可以通过下面的命令来生成这些图片。

python val.py --data data/data.yaml --weights runs/train/exp_yolov5s/weights/best.pt --img 640

遇到的问题

在这里插入图片描述
第一次使用wandb,选择1创建个账号,然后选择2输入 key就可以了。
按照提示打开链接:You can find your API key in your browser here: https://wandb.ai/authorize

登录并获取key,输入到终端: Paste an API key from your profile and hit enter:

配置成功(重新训练发现错误消失):Appending key for api.wandb.ai to your netrc file: /home/zlx/.netrc
2.OSError:[WinError 1455]页面文件太小,无法完成操作。
参考这个博主的解决方案
3.UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa4 in position 4: illegal multibyte sequence
在这里插入图片描述
解决方案:
labelImg-master\libs\yolo_io.py

classesFile = open(self.classListPath, 'r')
self.classes = classesFile.read().strip('\n').split('\n')
改为:
classesFile = open(self.classListPath, 'r',encoding='gbk')
self.classes = classesFile.read().strip('\n').split('\n')

在这里我最开始的解决方案是改成UTF-8,后面又报了一个错我尝试的改成gbk就可以了。所以图片没改过来。
在这里插入图片描述

模型使用

模型的使用全部集成在了detect.py目录下,你按照下面的指令指你要检测的内容即可

 # 检测摄像头
 python detect.py  --weights runs/train/exp_yolov5s/weights/best.pt --source 0  # webcam
 # 检测图片文件
  python detect.py  --weights runs/train/exp_yolov5s/weights/best.pt --source file.jpg  # image 
 # 检测视频文件
   python detect.py --weights runs/train/exp_yolov5s/weights/best.pt --source file.mp4  # video
 # 检测一个目录下的文件
  python detect.py --weights runs/train/exp_yolov5s/weights/best.pt path/  # directory
 # 检测网络视频
  python detect.py --weights runs/train/exp_yolov5s/weights/best.pt 'https://youtu.be/NUsoVlDFqZg'  # YouTube video
 # 检测流媒体
  python detect.py --weights runs/train/exp_yolov5s/weights/best.pt 'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream                            

若需要训练中文标签

1.修改utils/general.py文件

with open(data, errors='ignore') as f:
修改为:
with open(data, encoding="utf-8",errors='ignore') as f:

2.修改utils/metrics.py文件

sn.set(font_scale=1.0 if self.nc < 50 else 0.8)  # for label size
修改为:
sn.set(font="SimHei",font_scale=1.0 if self.nc < 50 else 0.8)  # for label size

3.utils/plots.py文件
在plots.py文件的开头添加

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

在这里插入图片描述

  • 11
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值