赞
踩
2024.4.15
yolo检测算法可以实现目标检测、分割和分类任务。
项目仓库地址:https://github.com/ultralytics/yolov5
跟练视频:目标检测 YOLOv5 开源代码项目调试与讲解实战
lux下载视频神器:https://github.com/iawia002/lux
参考链接:Github 上lux下载神器的安装及使用教程
(之前人家叫annie,现在叫lux…)
.pt文件是保存整个PyTorch模型的,而.pth文件只保存模型的参数。
Python=3.9:
conda install pytorch==1.13.0 torchvision==0.14.0 torchaudio==0.13.0 pytorch-cuda=11.7 -c pytorch -c nvidia
检查:
>>> import torch
>>> print(torch.__version__)
1.13.0
>>> print(torch.cuda.is_available())
True
>>> print(torch.cuda.get_device_name(0))
NVIDIA GeForce GTX 1060
根据requirements.txt安装依赖库:
pip install -r .\requirements.txt
选择了跟视频一样的yolov5-5.0版本,但是现在已经更新到v7.0版本了。最新版本问题会少很多,因为关于模型权重的下载是按照github tags里面最新内容下载的。
出现问题:
Can’t get attribute ‘SPPF’ on <module ‘models.common’ from’D:\code\yolov5-5.0\models\common.py’>
找到models/common.py文件,添加SPPF类,前面引入warrings库
import warrings class SPPF(nn.Module): # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * 4, c2, 1, 1) self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
出现新问题:
‘Upsample’ object has no attribute ‘recompute_scale_factor’
找到报错文件upsampling.py,将源代码报错位置改为:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)
删掉刚才下载的pt文件,手动下载权重文件将其替换。
继续出现问题:
504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at C:\cb\pytorch_1000000000000\work\aten\src\ATen\native\TensorShape.cpp:3191.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
找到文件位置:
# return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
# 添加 **indexing = 'ij'**
return _VF.meshgrid(tensors, **kwargs,indexing = 'ij') # type: ignore[attr-defined]
成功:
在exp中找到结果:
用lux下载视频,可以对视频进行预测。同样在参数–source中进行修改。
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
在–weight模型的选择上,尽管分辨率上不同(640和1280),但实际上输入和输出是保持不变的,可以得出在预测过程中图片有放缩。
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
控制输入图像的大小,进行裁剪方便统一输入。
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
显示阈值,默认值0.25是作者根据经验设置比较合理的参数阈值。设置过高,一些置信度比较低的预测不会被显示。
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
Non-Maximum Suppression:非极大值抑制(Non-Maximum Suppression,NMS)是计算机视觉和目标检测任务中常用的一种技术,用于消除冗余或重叠的边界框预测。
IOU = 两块区域的交集/两块区域的并集
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。