_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.SegmentationModel was not an allowed global by default. Please use `torch.serialization.add_safe_globals([SegmentationModel])` or the `torch.serialization.safe_globals([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.
时间: 2025-07-07 12:50:03 浏览: 33
<think>好的,我现在需要帮助用户解决PyTorch加载模型权重时遇到的_pickle.UnpicklingError问题。用户提到错误信息是“Weights only load failed”,并且参考了站内的一个解决方案,其中提到了添加numpy.core.multiarray._reconstruct到PyTorch的safe_globals中,同时保持weights_only=True的安全性。我需要仔细分析这个问题,并提供正确的解决步骤。
首先,我需要回忆一下PyTorch中torch.load函数的相关知识。在PyTorch 1.9之后,引入了weights_only参数,当设置为True时,只允许加载包含张量的pickle文件,这提高了安全性,防止潜在的恶意代码执行。但是,如果模型保存时包含了非张量对象或使用了某些第三方库(如numpy),可能会触发这个错误,因为默认的安全globals列表中没有这些模块的引用。
用户遇到的错误可能是因为加载的模型权重使用了numpy的某些功能,比如序列化时用到了numpy的数组结构。当使用weights_only=True时,PyTorch的安全机制无法识别numpy的_reconstruct函数,导致反序列化失败,从而抛出UnpicklingError。
接下来,我需要确定解决方案。根据用户提供的引用,正确的方法是将numpy.core.multiarray._reconstruct添加到PyTorch的safe_globals中。这可以通过torch.serialization.add_safe_globals函数来实现,该函数允许用户扩展安全允许的模块和函数列表。
步骤应该是这样的:首先导入必要的模块,然后调用add_safe_globals,将numpy的_reconstruct添加进去。之后,在加载模型时使用torch.load并设置weights_only=True。但需要注意,这可能会降低安全性,因为扩展了允许的globals,因此用户需要确保模型来源可信。
另外,我需要检查是否有其他可能的解决方案,例如是否可以不使用weights_only参数,或者将参数设置为False,但这会牺牲安全性,不是推荐的做法。因此,最佳实践还是通过添加必要的安全全局变量来解决问题。
同时,我需要提醒用户,在添加这些全局变量后,应该再次尝试加载模型,并确保代码的正确性,比如文件路径是否正确,PyTorch和numpy版本是否兼容等。此外,如果用户使用的是自定义类或第三方库的其他组件,可能需要将这些也添加到safe_globals中,但此问题中用户明确提到了numpy.core.multiarray._reconstruct,所以应该优先处理这个。
最后,我需要验证提供的代码示例是否正确。例如,是否正确地导入了numpy.core.multiarray模块,并正确调用了add_safe_globals。代码示例应该包括导入torch和numpy.core.multiarray,然后添加_reconstruct到safe_globals,最后加载模型。确保这些步骤准确无误,用户可以直接复制使用。</think>### 解决方案:解决PyTorch加载权重时的`_pickle.UnpicklingError`
#### 错误原因
当使用`torch.load`加载模型权重时,若设置`weights_only=True`,PyTorch会启用安全模式,仅允许反序列化张量和基础类型。若模型包含通过第三方库(如NumPy)序列化的对象(例如自定义层或特定数据结构),会因安全限制触发错误,提示缺少`numpy.core.multiarray._reconstruct`的引用[^1]。
#### 解决步骤
1. **扩展安全全局变量列表**
使用`torch.serialization.add_safe_globals`将NumPy的反序列化函数`_reconstruct`加入白名单:
```python
import torch
from numpy.core.multiarray import _reconstruct
torch.serialization.add_safe_globals([_reconstruct])
```
2. **加载模型时启用安全模式**
保持`weights_only=True`,但此时已允许识别NumPy相关操作:
```python
model = torch.load("model.pth", weights_only=True)
```
3. **验证加载结果**
确保模型正常初始化并执行推理:
```python
output = model(input_tensor)
```
#### 注意事项
- **安全性权衡**:添加`_reconstruct`会略微降低安全性,需确保模型来源可信。
- **兼容性检查**:确认PyTorch版本≥1.9且NumPy版本与模型训练环境一致。
- **自定义对象处理**:若模型包含其他第三方库组件,需将其反序列化函数一并加入白名单。
阅读全文
相关推荐

