pytorch模型转onnx

pytorch模型转其他的格式一般需要先转成通用的格式,onnx就是一种通用的格式,下面介绍转onnx的方法

1.torch模型转onnx

  直接通过torch官网:https://pytorch.org/docs/stable/onnx.html,提供的方法转onnx

import torchvision

dummy_input = torch.randn(10, 3, 224, 224, device='cuda')
model = torchvision.models.alexnet(pretrained=True).cuda()

# Providing input and output names sets the display names for values
# within the model's graph. Setting these does not change the semantics
# of the graph; it is only for readability.
#
# The inputs to the network consist of the flat list of inputs (i.e.
# the values you would pass to the forward() method) followed by the
# flat list of parameters. You can partially specify names, i.e. provide
# a list here shorter than the number of inputs to the model, and we will
# only set that subset of names, starting from the beginning.
input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
output_names = [ "output1" ]

torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)

# not assign the input name and output name
#torch.onnx.export(model, dummy_input, "alexnet.onnx")

转换的过程中容易出问题,一般是torch和torchvision的版本导致,下面的版本验证没有问题

  pip install torch==1.2.0 -i  https://pypi.tuna.tsinghua.edu.cn/simple

  pip install torchvision=0.4.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

2. onnx 推理

  pip install onnx==1.7.0 -i  https://pypi.tuna.tsinghua.edu.cn/simple

  pip install onnxruntime-gpu=1.4.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

  安装onnx,然后使用onnxruntime进行推理

# ...continuing from above
import onnxruntime as ort

ort_session = ort.InferenceSession('alexnet.onnx')

outputs = ort_session.run(None, {'actual_input_1': np.random.randn(10, 3, 224, 224).astype(np.float32)})

print(outputs[0])

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值