第一次使用yolov5-7.0版本实现界面可视化时,发现运行时会报很多错误,有的是没安装库,有的是无法从模块中导入库,原因是在新版的yolov5如版本7.0中,有些函数可能已经被移除,或是移到其他模块中,或者被重命名,现在将报的错误总结一下,方便以后的使用:
- 模块缺失
错误1: ModuleNotFoundError: No module named 'PyQt5'
解决方法: 直接在终端输入
pip install PyQt5
或者pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
使用后者镜像源下载的速度更快。
另外,也可以使用其他镜像源下载,在这里补充几个第三方库国内镜像:
1.豆瓣 https://pypi.douban.com/simple
2.阿里云 https://mirrors.aliyun.com/pypi/simple
3.清华大学 https://pypi.tuna.tsinghua.edu.cn/simple
4.中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple
错误2:ModuleNotFoundError: No module named 'utils.datasets'
错误原因:查看 yolov5/utils 文件夹中发现没有 datasets.py 文件,只有 dataloaders.py 文件,可能是被重命名了
解决方法:根据报错的地址,将 yolov5 文件夹里 detect_qt5.py 中第十行的
from utils.datasets import LoadStreams, LoadImages
改为from utils.dataloaders import LoadStreams, LoadImages
- 函数调用错误
错误1:ImportError: cannot import name 'scale_coords' from 'utils.general' (C:\Users\Administrator\yolov5\utils\general.py)
解决方法:将 utils 文件夹中 general.py 中对应位置的
'scale_coords'
改为'scale_boxes'
错误2:ImportError: cannot import name 'save_one_box' from 'utils.general' (C:\Users\Administrator\yolov5\utils\general.py)
错误原因:打开general.py文件,Ctrl+F搜索,发现文件中没有定义’save_one_box’函数,而在utils/plots.py文件中可以搜索到’save_one_box’,
解决方法:在 detect_qt5.py 文件中找到
from utils.general import xx
,将'save_one_box'
移至from utils.plots import xx
后面
错误3:ImportError: cannot import name 'plot_one_box' from 'utils.plots' (C:\Users\Administrator\yolov5\utils\plots.py)
错误原因:这是因为在 plots.py 文件中没有定义'plot_one_box'
解决方法:我们可以在程序中直接定义一个:
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
错误4:ImportError: cannot import name 'load_classifier' from 'utils.torch_utils' (C:\Users\Administrator\yolov5\utils\torch_utils.py)
解决方法:在utils所有的文件中都没有找到’load_classifier’函数,也无法确定是否被更名或移除,可以在torch_utils中添加一个定义,添加后目前还没有报错:
def load_classifier(name='resnet101', n=2):
"""在detect.py中调用
用于检测结束后可能需要第二次分类 直接修改torchvision中的预训练模型的分类类别即可
:params name: 分类模型名字 默认resnet101
:params n: 分类模型的分类类别数 需要在加载了预训练模型后将model的最后一层的类别数改为n
"""
#
# 加载torchvision中已经写好的pretrained模型 reshape为n类输出
model = torchvision.models.__dict__[name](pretrained=True)
# ResNet model properties
# input_size = [3, 224, 224]
# input_space = 'RGB'
# input_range = [0, 1]
# mean = [0.485, 0.456, 0.406]
# std = [0.229, 0.224, 0.225]
# 将加载的预训练模型的最后一层的分类类别数改为n Reshape output to n classes
# 总体的过程 = 将fc层的权重和偏置清0 + 修改类别个数为n
filters = model.fc.weight.shape[1]
model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True)
model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True)
model.fc.out_features = n
# 返回reshape后的模型 进行二次分类
return model
该办法参考这位博主的文章【YOLOV5-5.x 源码解读】torch_utils.py
错误5:ImportError: cannot import name 'time_synchronized' from 'utils.torch_utils' (C:\Users\Administrator\yolov5\utils\torch_utils.py)
解决方法:将utils文件夹中 torch_utils.py 中对应位置的
'time_synchronized'
改为'time_sync'
错误6:TypeError: attempt_load() got an unexpected keyword argument 'map_location'
错误原因:在新版本中函数定义如下:def attempt_load(weights, device=None, inplace=True, fuse=True)
,而在 detect_qt5.py 文件中代码为model = attempt_load(weights, map_location=device)
,所以会报错。
解决办法:删除 detect_qt5.py 文件中对应函数变量的
'map_location'
。
- 返回值错误
错误:ValueError: too many values to unpack (expected 4)
错误原因: dataset 的返回值与实际需要的值不匹配:
在代码中找到 dataset ,发现它来源自 LoadImages
或是 LoadSteams
这个两函数,那么很可能是这两个函数的返回值出了问题,我们顺藤摸瓜继续CTRL+F
搜索 LoadStreams
可以看到这两个函数都来自 utils 文件夹中的dataloaders.py
文件
打开文件后搜索这两个函数,找到其定义,看是哪个返回值超出了4,最后发现下图函数的返回值有5个。
解决方法: 将', s'
注释掉,发现可以正常运行。