pytorch模型转ONNX模型及ONNX模型推理和可视化

  • pytorch模型转ONNX模型
import torch

class ConNet(torch.nn.Module):
    def __init__(self):
        super(ConNet, self).__init__()
        self.conv1 = torch.nn.Sequential(
            torch.nn.Conv2d(3, 16, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.AvgPool2d(2, 2)
        )
        self.conv2 = torch.nn.Sequential(
            torch.nn.Conv2d(16, 32, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2, 2)
        )
        self.fc = torch.nn.Sequential(
            torch.nn.Linear(32*7*7, 128),
            torch.nn.ReLU(),
            torch.nn.Linear(128, 64),
            torch.nn.ReLU()
        )
        self.out = torch.nn.Linear(64, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.reshape(x.shape[0], -1)
        x = self.fc(x)
        output = self.out(x)
        return output

convnet = ConNet()
convnet.eval()
x = torch.randn(1, 3, 28, 28)   # 生成输入张量
export_onnx_file = "../convnet.onnx"		# ONNX文件名
torch.onnx.export(
    convnet,
    x,
    export_onnx_file,
    opset_version=10,  #操作集版本,默认为9
    do_constant_folding=True,   # 是否执行常量折叠优化
    input_names=["input"],    # 模型输入名
    output_names=["output"],  # 模型输出名
)

运行后生成在这里插入图片描述

  • ONNX模型推理
import torch
import numpy as np
import onnxruntime as rt
import time

def onnx_runtime():
    start = time.time()
    imgdata = np.random.randn(1, 3, 28, 28).astype(np.float32)
    sess = rt.InferenceSession('../convnet.onnx')
    input_name = sess.get_inputs()[0].name
    output_name = sess.get_outputs()[0].name
    pred_onnx = sess.run([output_name], {input_name: imgdata})
    end = time.time()
    pred_onnx = pred_onnx[0][0]
    print("outputs:")
    print(pred_onnx)
    print("推理用时", end - start)
onnx_runtime()

在这里插入图片描述

  • ONNX模型可视化
    使用在线可视化工具即可
    https://www.machunjie.com/dl/Visualization/index.html
    在这里插入图片描述
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值