1.1报错详情
在搭建好的虚拟环境中使用YOLOv8进行目标检测时,需要用数据集进行训练。在启动训练脚本时,遇到了报错UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
(1) 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.
(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([DetectionModel])` or the `torch.serialization.safe_globals([DetectionModel])` context manager to allowlist this global if you trust this class/function.
1.2报错图片
报错图片如下:
其中,训练使用的bvn_train.py文件如下(参考自B站/github——你可是处女座啊——手把手带你实战YOLOv8):
from ultralytics import YOLO
model=YOLO('yolov8n.pt')
model.train(data='bvn.yaml',workers=0,epochs=50,batch=16)
1.3解决方案
在报错的文件后追加代码语句“, weights_only=False”,详情如下所示:
(1)根据报错代码,找到倒数第二个file,ctrl+单击进入第634行代码。
(2)在第634行代码torch.load()中添加“, weights_only=False”。
return torch.load(file, map_location="cpu"), file # 原始代码
return torch.load(file, map_location="cpu", weights_only=False), file # 修改后的代码
再次运行,发现原有报错消失,程序开始训练。
此处省略50条epoch的内容。
50条epoch跑完,程序尝试traceback时,再次遇到相同报错。
2.1报错详情
启动训练脚本后,完成50条epoch后,遇到了报错。
UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
(1) 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.
(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([DetectionModel])` or the `torch.serialization.safe_globals([DetectionModel])` context manager to allowlist this global if you trust this class/function.
Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.
2.2报错图片
报错图片如下:
2.3解决方案
同上,在报错的文件后追加代码语句“, weights_only=False”,详情如下所示:
(1)根据报错代码,找到倒数第二个file,ctrl+单击进入第484行代码。
(2)在第484行代码torch.load()中添加“, weights_only=False”。
x = torch.load(f, map_location=torch.device("cpu"))#原始代码
x = torch.load(f, map_location=torch.device("cpu"), weights_only=False)#修改后的代码
再次运行,发现原有报错消失,程序开始训练。训练完成后不再报错,成功将结果存回train7。完美!