【解决问题】Tensor a (80)和b (56)的尺寸不匹配:RuntimeError解决方法

这里写自定义目录标题

  • 一、错误1
  • 解决方案1
  • 二、 错误2
  • 解决方案2:
  • 一、错误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:


    注意看这两个是否对应:

    因为我运行代码的是后模型没有下载下来,所以自己去github上下载的模型,然后出错了,就是模型错了,这儿给出地址:
    https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
    下载到
    如图同级目录下就行了,然后就可以运行了!

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【解决问题】Tensor a (80)和b (56)的尺寸不匹配:RuntimeError解决方法

    发表评论