南京网站设计与制作,广告网站模板免费下载,芯片联盟最新消息,丹东静态管理onnx文件转pytorch pt模型文件 1.onnx2torch转换及测试2.存在问题参考文献 从pytorch格式转onnx格式#xff0c;官方有成熟的API#xff1b;那么假如只有onnx格式的模型文件#xff0c;该怎样转回pytorch格式#xff1f;
https://github.com/ENOT-AutoDL/onnx2torch提供了… onnx文件转pytorch pt模型文件 1.onnx2torch转换及测试2.存在问题参考文献 从pytorch格式转onnx格式官方有成熟的API那么假如只有onnx格式的模型文件该怎样转回pytorch格式
https://github.com/ENOT-AutoDL/onnx2torch提供了一种解决方法。
1.onnx2torch转换及测试
整体使用非常简单
import torch
import onnxruntime
import numpy as nponnx_filemodel.onnx
pt_filemodel.ptDEVICE cpu
_input torch.rand([1, 3, 512, 512]).to(DEVICE)providers [CPUExecutionProvider]
session onnxruntime.InferenceSession(onnx_file, providersproviders)
output_names [x.name for x in session.get_outputs()]
onnx_output session.run(output_names, {session.get_inputs()[0].name: _input.cpu().numpy()})print(onnx_output[0].shape)modeltorch.load(pt_file)
model.to(DEVICE)
model.eval()
with torch.no_grad():torch_output model(_input)
print(torch_output.shape)print(torch.allclose(torch.tensor(onnx_output[0]),torch_output, atol1e-3)) 打印结果可知转换精度是很高的。
2.存在问题
转换成的torch模型使用时batch要与导出onnx时使用的batch一致否则会出错。
有机会再研究如何修改。
Anyway这已经提供了非常好的方案了。
参考文献
[1] https://github.com/ENOT-AutoDL/onnx2torch [2] MMdnn