1.环境安装
1.1.1安装Miniconda
1.1.2创建环境
conda create -n yolov5 python=3.8
conda activate yolov5
1.2.用pip安装PyTorch v1.8.2
验证torch是否安装成功
import torch
torch.ones(3).cuda()
tensor([1., 1., 1.], device=‘cuda:0’)
1.3.安装yolov5-7.0
通过github安装
并调整requiements.txt对应的版本
pip install -r requiements.txt --proxy http://127.0.0.1:10809
2.模型检测
2.1.1
weights 训练好的模型文件
YOLOv5x精度最大的模型
(yolov5) PS D:\yolov5-code> python .\detect.py --weights yolov5x.pt
Results saved to runs\detect\exp18
输出在runs\detect\exp18目录下
2.1.2
–source 检测目标,可以是单张图片,文件夹,屏幕或摄像头等
python .\detect.py --weights yolov5s.pt --source .\data\images\bus.jpg
2.1.3
–conf-thres 置信度阈值,值越低框越多,(0,1)
python .\detect.py --weights yolov5s.pt --conf-thres 0.8
2.1.4
–iou-thres IOU阈值,值越低框越少,(0,1)
2.1.5
–img 输入图片的大小
–max-det 检测的最大数量
–device 设备
–classes 可以检测指定的类别,而不检测其他类别
3.数据集构建
3.1标注工具使用
https://github.com/vietanhdev/anylabeling
classes.txt
labels/train/30.txt
labels/val/30.txt
3.2数据调整
classes.txt
images:存放图片
train:训练集图片 30.jpg
val:验证集图片
labels:存放标签
train:训练集图片标签文件,要与训练集图片名称一一对应 30.txt
val:验证集图片标签文件,要与验证集图片名称一一对应
4.模型训练
weights:预训练的权重文件
data:数据集的描述文件
python .\train.py --weights .\yolov5s.pt --data .\data\bvn.yaml --batch-size 2
更改虚拟缓存
tensorboard.exe --logdir runs 查看训练数值
检测
python .\detect.py --weights .\runs\train\exp2\weights\best.pt --source .\datasets\BVN.mp4
5.模型转换
CUDA 11.6.0
CUDNN 8.6.0.163
TensorRT 8.5.3.1
python .\export.py --weights yolov5x.pt --include engine --device 0
python detect.py --weights yolov5x.engine
6.模型速度对比
python detect.py --weights yolov5x.pt --source datasets\BVN.mp4
图片大小没匹配
python detect.py --weights yolov5x.engine --source datasets\BVN.mp4
全精度
python .\export.py --weights yolov5x.pt --include engine --device 0 --img 384 640
python detect.py --weights yolov5x.engine --source datasets\BVN.mp4 --img 384 640
半精度
python .\export.py --weights yolov5x.pt --include engine --device 0 --img 384 640 --half
python detect.py --weights yolov5x.engine --source datasets\BVN.mp4 --img 384 640 --half
半尺度
python .\export.py --weights yolov5x.pt --include engine --device 0 --img 192 320
python detect.py --weights yolov5x.engine --source datasets\BVN.mp4 --img 192 320
半精度,半尺度
python .\export.py --weights yolov5x.pt --include engine --device 0 --img 192 320 --half
python detect.py --weights yolov5x.engine --source datasets\BVN.mp4 --img 192 320 --half
pt的模型
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 384x640 1 person, 174.5ms
Speed: 0.9ms pre-process, 170.8ms inference, 1.9ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs\detect\exp40
图片尺度没设置
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 640x640 1 person, 159.0ms
Speed: 1.4ms pre-process, 154.6ms inference, 2.1ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs\detect\exp41
全精度
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 384x640 1 person, 92.7ms
Speed: 0.8ms pre-process, 87.4ms inference, 1.4ms NMS per image at shape (1, 3, 384, 640)
Results saved to runs\detect\exp43
半精度
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 384x640 1 person, 99.9ms
Speed: 0.7ms pre-process, 82.2ms inference, 1.8ms NMS per image at shape (1, 3, 384, 640)
Results saved to runs\detect\exp44
半精度,半尺度
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 192x320 1 person, 1 traffic light, 33.9ms
Speed: 0.5ms pre-process, 35.0ms inference, 1.9ms NMS per image at shape (1, 3, 192, 320)
Results saved to runs\detect\exp47
半尺度
video 1/1 (1243/1243) D:\dracode\yolov5-code\datasets\BVN.mp4: 192x320 1 person, 1 traffic light, 30.9ms
Speed: 0.5ms pre-process, 33.3ms inference, 1.6ms NMS per image at shape (1, 3, 192, 320)
Results saved to runs\detect\exp48
本文详细介绍了如何在Python环境中安装Miniconda、PyTorch和yolov5,包括创建环境、验证安装、模型检测(如调整参数、源码检测、速度对比)以及数据集构建和模型训练。还探讨了模型在不同精度和尺度下的性能表现。
1827

被折叠的 条评论
为什么被折叠?



