YOLOv5-5.0学习过程之detect.py运行报错

声明:
算法版本:YOLOv5-5.0
源码原作者地址:https://github.com/ultralytics/yolov5/tree/v5.0
出现的error:
一、
AttributeError: Can’t get attribute ‘SPPF’ on YOLOv5-5.0学习过程之detect.py运行报错_第1张图片
---------------------图一
error出现原因(个人猜想)
应该是YOLOv5原作者已经出了更新版本的算法了,如下图二,而我用的是5.0版本,detect.py运行会自行下一个权重文件,如下图三,可以看出来,他下载的是v6.1版本的,而不是我所需的5.0版本的,所以就出现了这些错误。
YOLOv5-5.0学习过程之detect.py运行报错_第2张图片
----------------------------图二
在这里插入图片描述
-----------图三

https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt

正确解决方法

去以下网址下载对应版本的权重文件放在工程里就行了
https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt

YOLOv5-5.0学习过程之detect.py运行报错_第3张图片
这时再运行,就不需要再下载这个权重文件,所以就不会下载其他版本的了


**

以下为错误解决方式

**
到models文件下的common.py添加v6.1的SPPF类并添加import warnings包
YOLOv5-5.0学习过程之detect.py运行报错_第4张图片

import warnings

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))

如果像上面这样改,会出现新的error
RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton dimension 3
在这里插入图片描述
以上是本人搞毕设所理解的见解,如果对你有帮助,请给个赞,谢谢!

你可能感兴趣的:(python基础,yolo,python,github)