In PyTorch 2.6, we changed the default value of the weights_only argument in torch.

问题描述:

D:\anaconda\envs\yolov5_mogui\python.exe D:/Instance_Segmentation/yolov5_moguimianju/yolov5-seg-master/train.py
train: weights=weights\yolov5s-seg.pt, cfg=models/yolov5s-seg.yaml, data=data\dc.yaml, hyp=data\hyps\hyp.scratch-low.yaml, epochs=1, batch_size=4, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, image_weights=False, device=0, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=4, project=runs\train-seg, name=exp, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, mask_ratio=4, no_overlap=False
github: skipping check (not a git repository), for updates see https://github.com/ultralytics/yolov5
YOLOv5  2025-5-4 Python-3.9.21 torch-2.7.0+cu118 CUDA:0 (NVIDIA GeForce RTX 4060 Laptop GPU, 8188MiB)

hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0
TensorBoard: Start with 'tensorboard --logdir runs\train-seg', view at http://localhost:6006/
Traceback (most recent call last):
  File "D:\Instance_Segmentation\yolov5_moguimianju\yolov5-seg-master\train.py", line 658, in <module>
    main(opt)
  File "D:\Instance_Segmentation\yolov5_moguimianju\yolov5-seg-master\train.py", line 554, in main
    train(opt.hyp, opt, device, callbacks)
  File "D:\Instance_Segmentation\yolov5_moguimianju\yolov5-seg-master\train.py", line 114, in train
    ckpt = torch.load(weights, map_location='cpu')  # load checkpoint to CPU to avoid CUDA memory leak
  File "D:\anaconda\envs\yolov5_mogui\lib\site-packages\torch\serialization.py", line 1524, in load
    raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_pickle.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 models.yolo.SegmentationModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([models.yolo.SegmentationModel])` or the `torch.serialization.safe_globals([models.yolo.SegmentationModel])` 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.

Process finished with exit code 1

问题解答:

降级 PyTorch 版本,可以100%绕过你目前的这个坑。


🔧 核心逻辑:

  • 你的 yolov5-seg-master 代码其实是兼容 torch 1.13 ~ 2.5 这一段。

  • torch 2.6 开始,加入了新的反序列化安全机制(weights_only=True),你没修改代码的话,它在加载模型权重时就会报这个 UnpicklingError

  • 所以降到 2.5.x 就完全回到老逻辑,模型文件可以直接正常读入。


注意:

下载torch一定要从官网下载,不要直接pip install torch.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神笔馬良

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值