用PyTorch导出ONNX,以及onnxsimplify的使用 - 学习记录

1、一个输入一个输出

import torch
import torch.nn as nn
import torch.onnx

class Model(torch.nn.Module):
    def __init__(self, in_features, out_features, weights, bias=False):
        super().__init__()
        self.linear = nn.Linear(in_features, out_features, bias)
        with torch.no_grad():
            self.linear.weight.copy_(weights)
    
    def forward(self, x):
        x = self.linear(x)
        return x

def infer():
    in_features = torch.tensor([1, 2, 3, 4], dtype=torch.float32)
    weights = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)
    
    model = Model(4, 3, weights)
    x = model(in_features)
    print("result is: ", x)

def export_onnx():
    input   = torch.zeros(1, 1, 1, 4)
    weights = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)
    model   = Model(4, 3, weights)
    model.eval() #添加eval防止权重继续更新

    # pytorch导出onnx的方式,参数有很多,也可以支持动态size
    torch.onnx.export(
        model         = model, 
        args          = (input,),
        f             = "../models/example.onnx",
        input_names   = ["input0"],
        output_names  = ["output0"],
        opset_version = 12)
    print("Finished onnx export")


if __name__ == "__main__":
    infer()
    export_onnx()

在这里插入图片描述
在这里插入图片描述

2、一个输入两个输出

import torch
import torch.nn as nn
import torch.onnx

class Model(torch.nn.Module):
    def __init__(self, in_features, out_features, weights1, weights2, bias=False):
        super().__init__()
        self.linear1 = nn.Linear(in_features, out_features, bias)
        self.linear2 = nn.Linear(in_features, out_features, bias)
        with torch.no_grad():
            self.linear1.weight.copy_(weights1)
            self.linear2.weight.copy_(weights2)

    
    def forward(self, x):
        x1 = self.linear1(x)
        x2 = self.linear2(x)
        return x1, x2

def infer():
    in_features = torch.tensor([1, 2, 3, 4], dtype=torch.float32)
    weights1 = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)
    weights2 = torch.tensor([
        [2, 3, 4, 5],
        [3, 4, 5, 6],
        [4, 5, 6, 7]
    ],dtype=torch.float32)
    
    model = Model(4, 3, weights1, weights2)
    x1, x2 = model(in_features)
    print("result is: \n")
    print(x1)
    print(x2)

def export_onnx():
    input    = torch.zeros(1, 1, 1, 4)
    weights1 = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)
    weights2 = torch.tensor([
        [2, 3, 4, 5],
        [3, 4, 5, 6],
        [4, 5, 6, 7]
    ],dtype=torch.float32)
    model   = Model(4, 3, weights1, weights2)
    model.eval() #添加eval防止权重继续更新

    # pytorch导出onnx的方式,参数有很多,也可以支持动态size
    torch.onnx.export(
        model         = model, 
        args          = (input,),
        f             = "../models/example_two_head.onnx",
        input_names   = ["input0"],
        output_names  = ["output0", "output1"],
        opset_version = 12)
    print("Finished onnx export")


if __name__ == "__main__":
    infer()
    export_onnx()

在这里插入图片描述
在这里插入图片描述

3、动态shape

import torch
import torch.nn as nn
import torch.onnx

class Model(torch.nn.Module):
    def __init__(self, in_features, out_features, weights, bias=False):
        super().__init__()
        self.linear = nn.Linear(in_features, out_features, bias)
        with torch.no_grad():
            self.linear.weight.copy_(weights)
    
    def forward(self, x):
        x = self.linear(x)
        return x

def infer():
    in_features = torch.tensor([1, 2, 3, 4], dtype=torch.float32)
    weights = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)

    model = Model(4, 3, weights)
    x = model(in_features)
    print("result of {1, 1, 1 ,4} is ", x.data)

def export_onnx():
    input   = torch.zeros(1, 1, 1, 4)
    weights = torch.tensor([
        [1, 2, 3, 4],
        [2, 3, 4, 5],
        [3, 4, 5, 6]
    ],dtype=torch.float32)
    model   = Model(4, 3, weights)
    model.eval() #添加eval防止权重继续更新

    # pytorch导出onnx的方式,参数有很多,也可以支持动态size
    torch.onnx.export(
        model         = model, 
        args          = (input,),
        f             = "../models/example_dynamic_shape.onnx",
        input_names   = ["input0"],
        output_names  = ["output0"],
        dynamic_axes  = {
            'input0':  {0: 'batch'},
            'output0': {0: 'batch'}
        },
        opset_version = 12)
    print("Finished onnx export")



if __name__ == "__main__":
    infer()
    export_onnx()

4、导出onnx时,一些节点被自动融合

import torch
import torch.nn as nn
import torch.onnx

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3)
        self.bn1   = nn.BatchNorm2d(num_features=16)
        self.act1  = nn.ReLU()
    
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.act1(x)
        return x

def export_norm_onnx():
    input   = torch.rand(1, 3, 5, 5)
    model   = Model()
    model.eval()

    # onnx导出的时候,其实有一些节点已经被融合了
    file    = "../models/sample-cbr.onnx"
    torch.onnx.export(
        model         = model, 
        args          = (input,),
        f             = file,
        input_names   = ["input0"],
        output_names  = ["output0"],
        opset_version = 15)
    print("Finished normal onnx export")

if __name__ == "__main__":
    export_norm_onnx()

batchNorm 不见了
在这里插入图片描述

5、onnx-simplifier

import torch
import torch.nn as nn
import torch.onnx
import onnxsim
import onnx

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1   = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1)
        self.bn1     = nn.BatchNorm2d(num_features=16)
        self.act1    = nn.ReLU()
        self.conv2   = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=5, padding=2)
        self.bn2     = nn.BatchNorm2d(num_features=64)
        self.act2    = nn.ReLU()
        self.avgpool = nn.AdaptiveAvgPool1d(1)
        self.head    = nn.Linear(in_features=64, out_features=10)
    
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.act1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.act2(x) 
        x = torch.flatten(x, 2, 3)  # B, C, H, W -> B, C, L (这一个过程产生了shape->slice->concat->reshape这一系列计算节点, 思考为什么)


        # b, c, w, h = x.shape
        # x = x.reshape(b, c, w * h)
        # x = x.view(b, c, -1)

        x = self.avgpool(x)         # B, C, L    -> B, C, 1
        x = torch.flatten(x, 1)     # B, C, 1    -> B, C
        x = self.head(x)            # B, L       -> B, 10
        return x

def export_norm_onnx():
    input   = torch.rand(1, 3, 64, 64)
    model   = Model()
    file    = "../models/sample-reshape.onnx"
    torch.onnx.export(
        model         = model, 
        args          = (input,),
        f             = file,
        input_names   = ["input0"],
        output_names  = ["output0"],
        opset_version = 15)
    print("Finished normal onnx export")

    model_onnx = onnx.load(file)

    # 检查导入的onnx model
    onnx.checker.check_model(model_onnx)


    # 使用onnx-simplifier来进行onnx的简化。
    # 可以试试把这个简化给注释掉,看看flatten操作在简化前后的区别
    # onnx中其实会有一些constant value,以及不需要计算图跟踪的节点

    # print(f"Simplifying with onnx-simplifier {onnxsim.__version__}...")
    # model_onnx, check = onnxsim.simplify(model_onnx)
    # assert check, "assert check failed"
    onnx.save(model_onnx, file)

if __name__ == "__main__":
    export_norm_onnx()

在这里插入图片描述
onnx-simplifier 后
(把上面的三行注释取消后运行)
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PyTorch 中的 while 语句在导出 ONNX 时需要使用特殊的函数进行处理,这个函数叫 torch.ops.script_ops.while_loop。该函数接受三个参数:循环条件、循环体和循环初始值。 示例: ```python import torch import torch.onnx def my_loop(counter, threshold): while counter < threshold: counter += 1 return counter counter = torch.tensor(0, dtype=torch.float32) threshold = torch.tensor(5, dtype=torch.float32) output = torch.ops.script_ops.while_loop( lambda counter, threshold: counter < threshold, my_loop, (counter, threshold) ) ``` 在上面的代码中,我们定义了一个 while 循环,并使用 torch.ops.script_ops.while_loop 函数将其转换为 ONNX 可以使用的格式。 注意:这里的while语句内部只能使用torch支持的算子,否则会报错 ### 回答2: 在PyTorch中,通过将模型导出ONNX格式时,while循环语句的处理略有不同。由于ONNX对于循环结构的支持相对有限,因此需要将while循环转换为等效的数学表达式或通过迭代的方式来实现。 一种常见的处理方法是使用递归函数来替代while循环。首先,在导出时将while循环替换为递归函数的调用。然后,在递归函数中,编写循环迭代的逻辑以模拟原有的while循环。最后,当达到退出条件时,递归函数将返回结果。 另一种处理方法是转换为数学表达式。通过分析while循环的逻辑,将其转换为数学表达式来实现。然后,将该表达式嵌入到导出ONNX模型中。 需要注意的是,由于ONNX的限制,无法直接将while循环作为原子操作导出。因此,需要根据具体的循环逻辑进行相应的转换处理。此外,ONNX还需要明确指定循环的最大迭代次数或合适的宽限范围,以确保转换后的模型在运行时的效果与原模型保持一致。 总之,在导出PyTorch模型至ONNX时,需要根据循环的具体逻辑,将while循环转换为递归函数或数学表达式,以保持模型的功能和性能。 ### 回答3: 在PyTorch中,导出ONNX时要处理while语句,可以使用`torch.onnx.TracedModule`和`torch.onnx.export`函数来实现。 首先,需要使用`torch.onnx.TracedModule`将带有while语句的模型转换为可跟踪的模型。这可以通过在模型前面添加`torch.jit.trace_module`来完成。例如,假设我们有一个带有while循环的模型`model`: ```python import torch import torch.nn as nn class MyModel(nn.Module): def __init__(self): super(MyModel, self).__init__() def forward(self, x): i = 0 while i < 10: x = x + i i += 1 return x model = MyModel() ``` 然后,我们可以使用`torch.onnx.TracedModule`和`torch.jit.trace_module`来将带有while语句的模型转换为可跟踪的模型: ```python traced_model = torch.onnx.TracedModule(torch.jit.trace_module(model)) ``` 接下来,可以使用`torch.onnx.export`函数将可跟踪的模型导出ONNX格式: ```python torch.onnx.export(traced_model, (input,), "model.onnx") ``` 在导出ONNX文件中,while循环会变成对应的ONNX运算符,并以递归方式执行。例如,在上面的示例中,导出ONNX图中将包含循环的等价操作和一个条件节点。 总结起来,将PyTorch模型导出ONNX时处理while语句的步骤如下: 1. 使用`torch.onnx.TracedModule`将包含while循环的模型转换为可跟踪的模型。 2. 使用`torch.onnx.export`将可跟踪的模型导出ONNX格式。 这样,导出ONNX文件中的while语句将通过等价的ONNX运算符来表示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值