Traceback (most recent call last): File "D:\23101\CCCCCCCCC\pt-onnx.py", line 11, in <module> onnx.export(model, dummy_input, "best.onnx", verbose=True, input_names=input_names, output_names=output_names) File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\site-packages\torch\onnx\utils.py", line 506, in export _export( File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\site-packages\torch\onnx\utils.py", line 1525, in _export with exporter_context(model, training, verbose): File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\contextlib.py", line 113, in __enter__ return next(self.gen) File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\site-packages\torch\onnx\utils.py", line 178, in exporter_context with select_model_mode_for_export( File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\contextlib.py", line 113, in __enter__ return next(self.gen) File "E:\ProgramFile\Anaconda\anaconda3\envs\python38\lib\site-packages\torch\onnx\utils.py", line 139, in disable_apex_o2_state_dict_hook for module in model.modules(): AttributeError: 'dict' object has no attribute 'modules'
时间: 2023-06-28 21:06:01 浏览: 249
这个错误可能是因为你的模型参数是以字典的形式给出的,而不是以 PyTorch 模型的形式给出的。可以尝试将字典转换为 PyTorch 模型,然后再导出 ONNX 模型。可以尝试以下代码:
```python
import torch
import torch.nn as nn
import onnx
# 定义模型结构
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.linear = nn.Linear(10, 1)
def forward(self, x):
return self.linear(x)
# 创建模型实例并加载参数
model_dict = torch.load('model_dict.pth')
model = MyModel()
model.load_state_dict(model_dict)
# 导出 ONNX 模型
dummy_input = torch.randn(1, 10)
input_names = ["input"]
output_names = ["output"]
onnx.export(model, dummy_input, "best.onnx", verbose=True, input_names=input_names, output_names=output_names)
```
其中,`model_dict.pth` 是你保存的模型参数文件。另外,你需要根据你实际的模型结构来定义 `MyModel` 类。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)