解决yolov5版本 Cant get attribute SPPF on module models.common等问题

1.报错: TypeError: not all arguments converted during string formatting

代码如下:

string = ''
# 读取数据集的图片
iml = cv2.imread('./yolo/zuo/%szuo%d.bmp' %(string,i) )  # 左图
imr = cv2.imread('./yolo/you/%syou%d.bmp' %(string,i) ) # 右图

2.报错: WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(140)

WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(140): Could not remove or rename D:\anaconda\pkgs\pytorch-1.6.0-py3.7_cuda101_cudnn7_0\Lib\site-packages\torch\lib\torch_cuda.dll. Please remove this file manually (you may need to reboot to free file handles)

手动把这个文件夹的 torch_cuda.dll 这个文件删除即可

3.报错: google-auth<2,>=1.6.3 not found and is required by YOLOv5

卸载重装即可

4.报错: AttributeError: Cant get attribute SPPF on module models.common
在 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))

使用coco数据集就没问题了,如果使用自己训练的权重文件,会报下方第5个例子的错
这是因为加入了sppf模块之后,前后传输的张量发生了变化
解决方法:找到yolo.py文件,把

@staticmethod
def _make_grid(nx=20, ny=20):
    yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
    return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()

替换成

def _make_grid(self, nx=20, ny=20, i=0):
    d = self.anchors[i].device
    #if check_version(torch.__version__, '1.10.0'):
    if (torch.__version__ >= '0.9.1'):
        yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)])
    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

大家看我注释的这句话

if check_version(torch.__version__, '1.10.0'):

我是torchvision版本低,用check_version会报错,所以改成了我自己的torchvision版本

if (torch.__version__ >= '0.9.1'):

5.报错: RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton
这是由于用了版本5.1,需要替换成6.1的,下载V6.1的版本点击下载
下载好之后替换原来的yolov5s

6.报错: TypeError: meshgrid() got an unexpected keyword argument ‘indexing‘
把报错部分的 indexing=‘ij’ 删除即可

7.报错:AttributeError:‘tuple’ object has no attribute ‘to’

报错代码:

if self.grid[i].shape[2:4] != x[i].shape[2:4]:
    self.grid[i] = self._make_grid(nx, ny).to(x[i].device)

修改为:

if self.grid[i].shape[2:4] != x[i].shape[2:4]:
    self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

笔记(不用理会)
用yolov5s用下边代码

@staticmethod
def _make_grid(nx=20, ny=20):
    yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
    return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
    self.grid[i] = self._make_grid(nx, ny).to(x[i].device)

用自己训练的权重文件将上边分别替换成

def _make_grid(self, nx=20, ny=20, i=0):
    d = self.anchors[i].device
    #if check_version(torch.__version__, '1.10.0'):
    if (torch.__version__ >= '0.9.1'):
        yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)])
    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
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
    self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

你可能感兴趣的:(YOLO,yolov5报错,pytorch,深度学习,python)