4.1 构建onnx结构模型-Reshape

前言

构建onnx方式通常有两种:
1、通过代码转换成onnx结构,比如pytorch —> onnx
2、通过onnx 自定义结点,图,生成onnx结构

本文主要是简单学习和使用两种不同onnx结构,
下面以reshape 结点进行分析

方式

方法一:pytorch --> onnx

固定shape
import torch
 
class JustReshape(torch.nn.Module):
    def __init__(self):
        super(JustReshape, self).__init__()
 
    def forward(self, x):
        # x = x.view((x.shape[3], x.shape[1], x.shape[0], x.shape[2]))
        x= x.reshape(x.shape[3], x.shape[1], x.shape[0], x.shape[2])
 
        return x 
 
net = JustReshape()
model_name = 'just_reshape.onnx'#保存ONNX的文件名字
dummy_input = torch.randn(1, 31, 42, 5)
torch.onnx.export(net, dummy_input, model_name, input_names=['input'], output_names=['output'])

结果如图所示:
请添加图片描述

动态shape

将第一维度设置为动态shape

# 只需要在这里对应位置修改即可
torch.onnx.export(net, dummy_input, model_name, 
                  input_names=['input'], 
                  output_names=['output'],
                  dynamic_axes={'input': {0: 'batch_size'},
                                'output': {0: 'batch_size'}})

# 可以将得到的模型,进一步进行简化处理
onnxsim 方式

方法二: onnx

import onnx 
from onnx import TensorProto, helper, numpy_helper

def run():
    print("run start....\n")

    reshape = helper.make_node(
        "Reshape",
        name="Reshape_0",
        inputs=["input", "shape"],
        outputs=["output"],
    )
    initializer = [ 
        helper.make_tensor("shape", TensorProto.INT64, [4], [5,31,1,42])
]
    graph = helper.make_graph(
        nodes=[reshape],
        name="test_graph",
        inputs=[helper.make_tensor_value_info(
            "input", TensorProto.FLOAT, [1,31,42,5]
        )],
        outputs=[helper.make_tensor_value_info(
            "output",TensorProto.FLOAT, [5,31,1,42]
        )],
        initializer=initializer,
    )

    op = onnx.OperatorSetIdProto()
    op.version = 11
    model = helper.make_model(graph, opset_imports=[op])
    print("run done....\n")
    return model

if __name__ == "__main__":
    model = run()
    onnx.save(model, "./test_reshape.onnx")

运行onnx

import onnx
import onnxruntime
import numpy as np


# 检查onnx计算图
def check_onnx(mdoel):
    onnx.checker.check_model(model)
    # print(onnx.helper.printable_graph(model.graph))

def run(model):
    print(f'run start....\n')
    session = onnxruntime.InferenceSession(model,providers=['CPUExecutionProvider'])
    input_name1 = session.get_inputs()[0].name  
    input_data1= np.random.randn(24,31,42,5).astype(np.float32)
    print(f'input_data1 shape:{input_data1.shape}\n')

    output_name1 = session.get_outputs()[0].name

    pred_onx = session.run(
    [output_name1], {input_name1: input_data1})[0]

    print(f'pred_onx shape:{pred_onx.shape} \n')

    print(f'run end....\n')


if __name__ == '__main__':
    path = "./reshape_dynamic_sim.onnx"
    model = onnx.load("./reshape_dynamic_sim.onnx")
    check_onnx(model)
    run(path)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

nsq_ai

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

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

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

打赏作者

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

抵扣说明:

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

余额充值