错误信息:
Namespace(augment=False, batch_size=32, conf_thres=0.001, data=‘/yolov5-5.0/data/coco128.yaml’, device=‘’, exist_ok=False, img_size=640, iou_thres=0.6, name=‘exp’, project=‘runs/test’, save_conf=False, save_hybrid=False, save_json=False, save_txt=False, single_cls=False, task=‘val’, verbose=False, weights=[‘/yolov5-5.0/weights/yolov5s.pt’])
YOLOv5 2021-4-11 torch 1.10.1+cu111 CUDA:0 (NVIDIA GeForce RTX 3090, 24268.3125MB)
Traceback (most recent call last):
File “/yolov5-5.0/test.py”, line 313, in
test(opt.data,
File “/yolov5-5.0/test.py”, line 56, in test
model = attempt_load(weights, map_location=device) # load FP32 model
File “/yolov5-5.0/models/experimental.py”, line 118, in attempt_load
ckpt = torch.load(w, map_location=map_location) # load
File “/opt/conda/lib/python3.8/site-packages/torch/serialization.py”, line 607, in load
return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
File “/opt/conda/lib/python3.8/site-packages/torch/serialization.py”, line 882, in _load
result = unpickler.load()
File “/opt/conda/lib/python3.8/site-packages/torch/serialization.py”, line 875, in find_class
return super().find_class(mod_name, name)
AttributeError: Can’t get attribute ‘SPPF’ on
方法一:
改正方法,在models/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))
方法二:检查自己下载的yolov5s.pt等模型是否与自己所用代码的版本一致。