1.进入虚拟环境
2.pip install labelimg
3.直接输入labelimg就可以打开软件
conda activate your-name pip install labelimg labelimglabelimg软件设置
这边数据格式一定要设置成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:UserszlxDesktopyoloimagetrain val: d:UserszlxDesktopyoloimageval # 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 100 --batch-size 4 --device 0
所有训练结果都保存在runs/train/递增的运行目录中,例如runs/train/exp2,runs/train/exp3等等。
其中模型在runs/trains/exp2/weights中best.pt/last.pt,在exp2中还有各种指标图的图片。
在模型结束之后,可以在runs/trains中找到三张图,分别表示我们模型在验证集上的召回率、准确率和均值平均密度。
P_curve.png、PR_curve.png、R_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]页面文件太小,无法完成操作。
参考这个博主的解决方案
模型的使用全部集成在了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



