PyTorch学习记录(三):pth模型转onnx
原则:
Python for Training
C++ for Inference
PyTorch模型导出:
torch.save()
: 保存模型的时候保存哪些东西,除了模型权重以外的其他变量- https://github.com/alibaba/cascade-stereo/blob/master/CasMVSNet/train.py#L98-L102
- https://github.com/yihuacheng/GazeTR/blob/master/trainer/total.py#L136-L143
PyTorch使用.pth
文件来对神经网络的权重进行保存,.pth
文件中的模型权重则是按照字典格式进行保存的,但是.pth
文件中没有网络的结果信息。需要借助开放神经网络交换(Open Neural Network Exchange, ONNX)框架将模型导出为结构和权重完整的.onnx
文件。
模型参数量
model = FPN()
num_params = sum(p.numel() for p in model.parameters())
print("num of params: {:.2f}k".format(num_params/1000.0))
# torch.numel()返回tensor的元素数目,即number of elements
打印模型
model = FPN()
num_params = sum(p.numel() for p in model.parameters())
print("num of params: {:.2f}k".format(num_params/1000.0))
print("===========================")
#for p in model.parameters():
# print(p.name)
print(model)
参考资料
- 文档
https://pytorch.org/docs/stable/onnx.html - 教程
https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html - 代码 (Python)
https://github.com/pytorch/tutorials/blob/master/advanced_source/super_resolution_with_onnxruntime.py - ONNXRuntime C++ API
https://github.com/leimao/ONNX-Runtime-Inference - PyTorch保存/加载模型
https://pytorch.org/tutorials/beginner/saving_loading_models.html - Netron Issue
Not able to visualize PyTorch model
分类:
PyTorch学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY