记一次灵活的模型训练生成的pth转onnx文件失败
生活随笔
收集整理的這篇文章主要介紹了
记一次灵活的模型训练生成的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 exportcustom_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 exportuse_external_data_format=use_external_data_format)File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 639, in _exportdynamic_axes=dynamic_axes)File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\utils.py", line 421, in _model_to_graphdynamic_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_graphgraph = 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_functionreturn 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_functionreturn symbolic_fn(g, *inputs, **attrs)File "C:\Users\deep\anaconda3\envs\swin\lib\site-packages\torch\onnx\symbolic_opset9.py", line 1314, in index_putsym_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
哪位大佬會這道題或者也遇到過這種情況可否留言解答一下,萬分感謝!
總結
以上是生活随笔為你收集整理的记一次灵活的模型训练生成的pth转onnx文件失败的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nn.LayerNorm的参数
- 下一篇: PyTorch-torch.nn.Ada