记一次灵活的模型训练生成的pth转onnx文件失败

代码1

import torch
import torchvision
dummy_input = torch.randn(1, 3, 224, 224)
model = torch.load(r'D:\***\swin_transformer_flower\weights\model---0.pth')
model.eval()
input_names = ["input"]
output_names = ["output"]
torch.onnx.export(model,
                  dummy_input,
                  "model---9.onnx",
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names)

出错信息

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

参考:【已解决】Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

代码2

import torch
import torchvision
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dummy_input = torch.randn(1, 3, 224, 224).to(device)
model = torch.load(r'D:\***\swin_transformer_flower\weights\model---0.pth')
model.eval()
input_names = ["input"]
output_names = ["output"]
torch.onnx.export(model,
                  dummy_input,
                  "model---9.onnx",
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names)

出错信息:

D:\***\swin_transformer_flower\model.py:117: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  pad_input = (H % self.patch_size[0] != 0) or (W % self.patch_size[1] != 0)
D:\***\swin_transformer_flower\model.py:119: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if pad_input:
D:\***\swin_transformer_flower\model.py:461: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  Hp = int(np.ceil(H / self.window_size)) * self.window_size
D:\***\swin_transformer_flower\model.py:462: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  Wp = int(np.ceil(W / self.window_size)) * self.window_size
D:\***\swin_transformer_flower\model.py:357: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  assert L == H * W, "input feature has wrong size"
D:\***\swin_transformer_flower\model.py:85: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  B = int(windows.shape[0] / (H * W / window_size / window_size))
D:\***\swin_transformer_flower\model.py:396: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if pad_r > 0 or pad_b > 0:
D:\***\swin_transformer_flower\model.py:161: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  assert L == H * W, "input feature has wrong size"
D:\***\swin_transformer_flower\model.py:167: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  pad_input = (H % 2 == 1) or (W % 2 == 1)
D:\***\swin_transformer_flower\model.py:168: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if pad_input:
Traceback (most recent call last):
  File "D:/***/swin_transformer_flower/测试.py", line 19, in <module>
    output_names=output_names)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\__init__.py", line 230, in export
    custom_opsets, enable_onnx_checker, use_external_data_format)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 91, in export
    use_external_data_format=use_external_data_format)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 639, in _export
    dynamic_axes=dynamic_axes)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 421, in _model_to_graph
    dynamic_axes=dynamic_axes, input_names=input_names)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 203, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\__init__.py", line 263, in _run_symbolic_function
    return utils._run_symbolic_function(*args, **kwargs)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 934, in _run_symbolic_function
    return symbolic_fn(g, *inputs, **attrs)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\symbolic_opset9.py", line 1314, in index_put
    sym_help._onnx_opset_unsupported('index_put', 9, 11)
  File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\symbolic_helper.py", line 192, in _onnx_opset_unsupported
    'opset {}. Please try opset version {}.'.format(op_name, current_opset, supported_opset))
RuntimeError: Unsupported: ONNX export of index_put in opset 9. Please try opset version 11.

代码3

改为

import torch
import torchvision
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dummy_input = torch.randn(1, 3, 224, 224).to(device)
model = torch.load(r'D:\***\swin_transformer_flower\weights\model---0.pth')
model.eval()
input_names = ["input"]
output_names = ["output"]
torch.onnx.export(model,
                  dummy_input,
                  "model---9.onnx",
                    export_params=True,
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names,
                  opset_version=11,
                  keep_initializers_as_inputs=True
                  )

出错信息

D:\***\swin_transformer_flower\model.py:117: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  pad_input = (H % self.patch_size[0] != 0) or (W % self.patch_size[1] != 0)
RuntimeError: Exporting the operator roll to ONNX opset version 11 is not supported. Please open a bug to request ONNX export support for the missing operator.

分析:

原因是训练模型的代码中好像是太多if else语句了(我的理解)
如果你用Pytorch定义的网络结构太过于灵活,那么转成ONNX的时候很有可能出错。
这个报错通常情况下是你的网络结构中出现if else 语句。
最好不要这样写,能避免的话尽量避免。不要写if else判断语句。

以上分析参考:
1、https://www.codeleading.com/article/37523519244/
2、pytorch转onnx遇到的一些问题
3、Pytorch转ONNX采坑记:Converting a tensor to a Python boolean might cause the trace to be incorrect. We…
4、pytorch报错Converting a tensor to a Python integer

哪位大佬会这道题或者也遇到过这种情况可否留言解答一下,万分感谢!
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

饿了就干饭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值