pytorch模型转onnx模型

pytorch框架自带onnx模型转换代码,如下使用resnet34和mobilenetV2为例

resnet34:

首先下载resnet34的pytorch模型,各版本resnet模型下载地址如下

model_urls = {
    'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
    'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
    'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
    'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
    'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}

下载完后使用如下代码进行模型转换:

import torch

def main():
    model = torch.load("resnet34.pth")
    imput = torch.randn(1,3,224,224)
    torch.onnx.export(model, input, "resnet34.onnx", verbose=True, opset_version=11, export_params=True)

if __name__ == '__main__':
    main()

mobilenetV2:

各类轻量化模型的下载地址如下:

GitHub - murufeng/awesome_lightweight_networks: The implementation of various lightweight networks by using PyTorch. such as:MobileNetV2,MobileNeXt,GhostNet,ParNet,MobileViT、AdderNet,ShuffleNetV1-V2,LCNet,ConvNeXt,etc. ⭐⭐⭐⭐⭐

下载完后使用如下代码进行模型转换:

import torch
from light_cnns import mbv1


def main():
    model = mbv1()
    imput = torch.randn(1,3,224,224)
    torch.onnx.export(model, input, "mobilenetV2.onnx", verbose=True, opset_version=11, export_params=True)

if __name__ == '__main__':
    main()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值