【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

一、错误1

Can't get attribute 'SPPF' on <module 'models.common' from 'D:\\Pycharm\\Code\\yolov5-5.0\\models\\common.py'>

解决方案1

你可以去github上,这儿我用的是YOLOv5.5的版本,就去Tags6里面的model/common.py里面去找到这个SPPF的类,把它拷过来到你这个Tags5的model/common.py里面,这样你的代码就也有这个类了,还要引入一个warnings包就行了

带你在这里插入图片描述
点开common.py文件

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

只需将其复制到相应的类即可。

二、 错误2

我刚刚解决了最后一个问题,结果又是另一个问题。我也很崩溃。花了很长时间才找到解决方案。终于找到了,哈哈,让我笑一会! ! !请参阅下面的错误:

RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

解决方案2:

【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
注意看这两个是否对应:
【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
因为我运行代码的是后模型没有下载下来,所以自己去github上下载的模型,然后出错了,就是模型错了,这儿给出地址:
https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
下载到【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
如图所示同级目录就行了,然后就可以运行了!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2022年3月23日 下午2:16
下一篇 2022年3月23日 下午2:35

相关推荐