torch模型导出onnx

记录一下torch模型转onnx,onnx的优点是速度快,并且文件小,并且可支持较多的引擎,本文简单介绍如何导出:

首先加载我们的模型,例如加载我的模型:

model = ResNetSE34V2(8222, device, encoder_type='ASP').to(device)
model = load_checkpoint(model, '../checkpoint/', checkpoint_name='model1.pt')
model.eval()

加载完成后一定要让模型进入eval状态 ,之后就可以进行导出操作了:

x = torch.randn((1, 80, 300))
torch.onnx.export(model,  # model being run
				  x,  # model input (or a tuple for multiple inputs)
				  "model.onnx",  # where to save the model (can be a file or file-like object)
				  export_params=True,  # store the trained parameter weights inside the model file
				  opset_version=10,  # the ONNX version to export the model to
				  do_constant_folding=True,  # whether to execute constant folding for optimization
				  input_names=['input'],  # the model's input names
				  output_names=['output']  # the model's output names)

如此即可导出模型,但如果我们的输入模型参数是不断变化的呢,例如第三个维度的长度不是300,又该如何导出?我们只需要指定一下 第二个维度为动态变化的即可 ,如下:

torch.onnx.export(model,  # model being run
				  x,  # model input (or a tuple for multiple inputs)
				  "model2.onnx",  # where to save the model (can be a file or file-like object)
				  export_params=True,  # store the trained parameter weights inside the model file
				  opset_version=10,  # the ONNX version to export the model to
				  do_constant_folding=True,  # whether to execute constant folding for optimization
				  input_names=['input'],  # the model's input names
				  output_names=['output'],  # the model's output names
				  dynamic_axes={'input': {2: 'int_height'}})

那如果有多个变化的,也是写上相应维度即可,并且输出也可以是变化的,如下:

torch.onnx.export(model,  # model being run
				  x,  # model input (or a tuple for multiple inputs)
				  "model2.onnx",  # where to save the model (can be a file or file-like object)
				  export_params=True,  # store the trained parameter weights inside the model file
				  opset_version=10,  # the ONNX version to export the model to
				  do_constant_folding=True,  # whether to execute constant folding for optimization
				  input_names=['input'],  # the model's input names
				  output_names=['output'],  # the model's output names
				  dynamic_axes= {
                       input_name: {0: 'batch_size', 2 : 'in_width', 3: 'int_height'},
                       output_name: {0: 'batch_size', 2: 'out_width', 3:'out_height'}})

模型导出完成,实际使用一般使用cpu进行infer,此处下载cpu版本的onnx,不能同时下载cpu和gpu版本!

pip install onnxruntime

使用模型:

x = torch.randn((1, 80, 300))
onnx_model = onnxruntime.InferenceSession("model2.onnx")
print(onnx_model.get_inputs()[0].name)
inputs = {onnx_model.get_inputs()[0].name: x.cpu().numpy()}
outs = onnx_model.run(None, inputs)
print(outs[0])

如此即可。over

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码匀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值