AttributeError: Cant get attribute SPPF on module models

运行YOLOV5出现报错AttributeError: Can't get attribute 'SPPF'

问题

AttributeError: Can't get attribute 'SPPF'

运行yolov5下面Tags5的代码出现问题:
AttributeError: Cant get attribute SPPF on module models

搞了很久,最终得到解决方案,特此公布给遇到同样问题的人。

运行的是yolov5 Tags5的代码,出现了这个报错:

解决方案

  1. 到Tags6里面的model/common.py里面去找到这个SPPF的类,把它拷过来到Tags5的model/common.py里面,这样你的代码就也有这个类了,还要引入一个warnings

如果找不到SPPF这个类,那我现在直接粘贴在这里,你们只需要复制到你们的common.py里面即可,记得把import warnings放在上面去

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

猜你喜欢

转载自blog.csdn.net/qq_41704415/article/details/121860291
今日推荐