《------往期经典推荐------》
二、机器学习实战专栏【链接】,已更新31期,欢迎关注,持续更新中~~
三、深度学习【Pytorch】专栏【链接】
四、【Stable Diffusion绘画系列】专栏【链接】
五、YOLOv8改进专栏【链接】,持续更新中~~
六、YOLO性能对比专栏【链接】,持续更新中~
《------正文------》
问题如下
在使用YOLO('model_path')
加载YOLOv8/v11模型时,报错:
WeightsUnpickler error: Unsupported global:GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed global by default.
报错截图如下:
原因分析
根据报错提示信息:
In PyTorch 2.6, we changed the default value of the weights_only argument in torch.load from False to True. Re-running torch.load with weights_only set to False will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
由信息可知,这是由于Pytorch在最新版本中,将torch.load
加载模型时的默认参数 weights_only
从False
改为了True
导致报错,我们只需要将该参数重新设置为False
即可。
修改方法如下
根据上图框选出的报错提示源文件路径和行数,找到报错的代码行,然后修改即可。
上述截图中提示的文件路径为:
“D:\FireSmoke\WildfireSmokeDetectionv8\ultralytics\nn\tasks.py”
【这里需根据自己的报错路径查找】
根据路径找到该文件tasks.py
,然后找到提示错误的行代码,这里提示的是708行:【行数可能会不一样】
ckpt= torch.load(file, map_location="cpu")
在torch.load里面加个参数weights_only=False
,修改为:
ckpt= torch.load(file, map_location="cpu",weights_only=False)
保存修改后的文件,程序即可正常运行。
好了,这篇文章就介绍到这里,喜欢的小伙伴感谢给点个赞和关注,更多精彩内容持续更新~~
关于本篇文章大家有任何建议或意见,欢迎在评论区留言交流!