pickle.load() 时报错 raise AttributeError(attr + " not found")

本文解决了一个关于使用Python3的pickle.load()函数时遇到的错误。错误发生在尝试加载一个之前保存的数据集时,提示属性错误“shapenotfound”。解决办法是在pickle.dump()时添加protocol=0参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 问题:我用python3 的pickle.load() 时报错。错误信息如下代码,
  2. 错误日志:
Traceback (most recent call last):
  File "./ncf.py", line 179, in main
    train_dataset = pickle.load(save_traind)
  File "/anaconda3-5.0.1/lib/python3.6/site-packages/scipy/sparse/dok.py", line 244, in __setitem__
    if (isintlike(i) and isintlike(j) and 0 <= i < self.shape[0]
  File "/anaconda3-5.0.1/lib/python3.6/site-packages/scipy/sparse/base.py", line 576, in __getattr__
    raise AttributeError(attr + " not found")
AttributeError: shape not found

  1. 找到了对策:原来是dump 的时候需要加参数 protocol=0
  2. 具体的:
abc_file = list(XXX)
with open('abc_asve.pkl','wb') as sf:
  pickle.dump(abc_file, sf, protocol=0)
with open('abc_save.pkl', 'rb') as rf:
  abc_read = pickler.load(sf)

Done!

参考 :
https://github.com/joblib/joblib/issues/177

Traceback (most recent call last): File "D:\桌面\ultralytics-yolov8-main\test1\blueberry.py", line 4, in <module> a1 = YOLO('yolov8n.pt') # 官方提供的基础测试和训练模型 ^^^^^^^^^^^^^^^^^^ File "D:\桌面\ultralytics-yolov8-main\ultralytics\yolo\engine\model.py", line 106, in __init__ self._load(model, task) File "D:\桌面\ultralytics-yolov8-main\ultralytics\yolo\engine\model.py", line 145, in _load self.model, self.ckpt = attempt_load_one_weight(weights) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\桌面\ultralytics-yolov8-main\ultralytics\nn\tasks.py", line 396, in attempt_load_one_weight ckpt, weight = torch_safe_load(weight) # load ckpt ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\桌面\ultralytics-yolov8-main\ultralytics\nn\tasks.py", line 336, in torch_safe_load return torch.load(file, map_location='cpu'), file # load ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\李佳伟\AppData\Roaming\Python\Python312\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 ultralytics.nn.tasks.DetectionModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([ultralytics.nn.tasks.DetectionModel])` or the `torch.serialization.safe_globals([ultralytics.nn.tasks.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.解释代码
05-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值