utureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. args_old = torch.load(argname) C:\Users\omen\anaconda3\opencv\Debulr\DeepDeblur-PyTorch-master\src\train.py:147: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(self.args.amp):
时间: 2025-07-10 18:50:33 浏览: 51
<think>嗯,用户现在遇到了两个FutureWarning的问题,一个是关于torch.load的weights_only参数,另一个是torch.cuda.amp.autocast的弃用警告。我需要先理解这两个警告的具体内容,然后给出解决方法。
首先,关于torch.load的警告。根据用户提供的错误信息,PyTorch在加载模型时默认使用weights_only=False,这可能会导致安全风险,因为pickle模块可能执行恶意代码。未来的版本会将默认值改为True,所以用户需要提前适应。解决方法应该是建议用户将torch.load的参数设置为weights_only=True,同时注意可能的不兼容问题,比如如果模型包含自定义对象,需要允许列表。另外,如果用户确实需要加载旧模型,可能需要保持False,但要确保模型来源可信。
然后是torch.cuda.amp.autocast的弃用警告。PyTorch现在推荐使用torch.amp.autocast并指定设备类型,比如'cuda'。用户需要修改代码中的上下文管理器,将原来的torch.cuda.amp.autocast替换为torch.amp.autocast('cuda', ...)。同时,检查是否还有其他地方使用了旧的语法,比如GradScaler,可能需要更新为torch.amp.GradScaler。
接下来,我需要考虑用户的实际情况。用户可能在运行一个旧版本的代码(比如DeepDeblur-PyTorch-master),所以需要指导他们如何修改这两个地方。同时,用户可能需要保持代码兼容性,如果他们同时使用不同版本的PyTorch,可能需要条件判断。
另外,用户的安全问题也很重要,特别是如果他们加载的是不可信的模型文件,必须强调使用weights_only=True的重要性,即使这可能导致加载错误,需要处理可能的异常或调整模型保存方式。
可能还需要提醒用户检查PyTorch版本,因为新语法可能在旧版本中不可用,所以升级PyTorch可能也是解决方案的一部分,但要注意兼容性问题。
总结下来,回答的结构应该是分两部分,每个警告的原因和解决方案,然后给出修改代码的示例,最后可能加上版本升级的建议和注意事项。确保用户能够一步步按照指导修改代码,避免未来的警告和潜在的安全风险。</think>---
### **警告原因与解决方案**
你遇到的 `FutureWarning` 涉及 PyTorch 的两个关键问题:**模型加载安全性**和**自动混合精度 API 的更新**。以下是详细分析和修复方法:
---
### **一、`weights_only=False` 警告**
#### **原因**
- PyTorch 默认使用 `weights_only=False` 加载模型(`.pt` 文件),这依赖 Python 的 `pickle` 模块,存在执行恶意代码的风险(如加载不可信模型时)。
- 未来版本将默认 `weights_only=True`,仅允许加载纯张量和简单对象,禁止任意代码执行。
#### **修复方法**
**1. 安全加载(推荐)**
```python
# 显式设置 weights_only=True(适用于纯模型权重)
checkpoint = torch.load("model.pt", weights_only=True)
```
- **适用场景**:加载仅包含模型参数(`state_dict`)的 `.pt` 文件。
- **限制**:若模型包含自定义类对象(如优化器状态、复杂数据结构),需手动允许这些类(见下文)。
**2. 兼容性处理**
如果必须加载旧格式模型且无法修改代码:
```python
# 临时忽略警告(不推荐长期使用)
import warnings
warnings.filterwarnings("ignore", category=FutureWarning, message=".*weights_only=False.*")
# 继续使用旧方式加载
checkpoint = torch.load("model.pt")
```
**3. 自定义类白名单(高级)**
若模型包含自定义对象且需 `weights_only=True`,需显式声明允许的类:
```python
from torch.serialization import add_safe_globals
# 将自定义类加入白名单
add_safe_globals([YourCustomClass])
# 安全加载
checkpoint = torch.load("model.pt", weights_only=True)
```
---
### **二、`torch.cuda.amp.autocast` 弃用警告**
#### **原因**
PyTorch 从 2.0 版本开始统一自动混合精度(AMP)API,废弃了设备特定的 `torch.cuda.amp.autocast`,改为通用接口 `torch.amp.autocast`。
#### **修复方法**
更新代码中的 `autocast` 调用方式:
```python
# 旧方式(已废弃)
with torch.cuda.amp.autocast():
# 前向计算
# 新方式(统一指定设备类型)
with torch.amp.autocast(device_type="cuda", dtype=torch.float16):
# 前向计算
```
- **参数说明**:
- `device_type`: 指定设备类型(如 `"cuda"` 或 `"cpu"`)。
- `dtype`: 设置计算精度(如 `torch.float16`)。
#### **兼容性代码**
若需同时支持新旧版本 PyTorch:
```python
if hasattr(torch, "amp") and hasattr(torch.amp, "autocast"):
autocast = torch.amp.autocast(device_type="cuda", dtype=torch.float16)
else:
autocast = torch.cuda.amp.autocast
with autocast():
# 前向计算
```
---
### **三、完整代码修改示例**
以你的 `train.py` 中报错行 `with amp.autocast(self.args.amp):` 为例:
```python
# 原代码(触发警告)
with amp.autocast(self.args.amp):
# 训练逻辑
# 修改后代码
with torch.amp.autocast(device_type="cuda", dtype=torch.float16 if self.args.amp else torch.float32):
# 训练逻辑
```
---
### **四、附加建议**
1. **升级 PyTorch 版本**:
```bash
pip install torch --upgrade # 确保版本 >= 2.0.0
```
2. **检查依赖项**:
- 更新 `opencv-python`、`torchvision` 等库,避免版本冲突。
3. **安全实践**:
- 仅加载可信的 `.pt` 文件。
- 使用 `weights_only=True` 时,若加载失败,检查模型是否包含非标准对象。
---
### **总结**
- **安全性优先**:尽快迁移到 `weights_only=True`,避免潜在代码注入风险。
- **API 更新**:遵循 PyTorch 官方推荐写法,保持代码兼容性。
若问题仍未解决,建议提供完整代码片段和 PyTorch 版本信息(通过 `print(torch.__version__)` 获取),以便进一步分析!
阅读全文
相关推荐















