YOLO版本不兼容,报错AttributeError: Can’t get attribute ‘SPPF’ on <module ‘models.common’

AttributeError: Can’t get attributeSPPFon models.common

3.0的代码中不含SPPF函数。加上就好。

model文件夹中的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))

会继续报错的话

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

用6.0的代码替换3.0的代码部分,_make_grid整个用以下替换。

check_version要改成自己相对应的版本。

   def _make_grid(self, nx=20, ny=20, i=0):
        d = self.anchors[i].device
        if check_version(torch.__version__, '1.10.0'):  # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
            yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)], indexing='ij')
        else:
            yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)])
        grid = torch.stack((xv, yv), 2).expand((1, self.na, ny, nx, 2)).float()
        anchor_grid = (self.anchors[i].clone() * self.stride[i]) \
            .view((1, self.na, 1, 1, 2)).expand((1, self.na, ny, nx, 2)).float()
        return grid, anchor_grid

还会继续报错

tuple' object has no attribute 'to 

解决办法 : self.grid[i] = self._make_grid(nx, ny).to(x[i].device) 换成 self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

你可能感兴趣的:(人工智能,pytorch)