0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

基于ONNX结构分析

FPGA创新中心 来源:FPGA创新中心 作者:FPGA创新中心 2022-10-13 11:29 次阅读

Q

什么是ONNX?

ONNX(Open Neural Network Exchange)- 开放神经网络交换格式,作为框架共用的一种模型交换格式,使用protobuf 二进制格式来序列化模型,可以提供更好的传输性能我们可能会在某一任务中Pytorch或者TensorFlow模型转化为ONNX模型(ONNX模型一般用于中间部署阶段),然后再拿转化后的ONNX模型进而转化为我们使用不同框架部署需要的类型,ONNX相当于一个翻译的作用。

Q

为什么要用ONNX?

深度学习算法大多通过计算数据流图来完成神经网络的深度学习过程。一些框架(例如CNTK,Caffe2,Theano和TensorFlow)使用静态图形,而其他框架(例如PyTorch和Chainer)使用动态图形。但是这些框架都提供了接口,使开发人员可以轻松构建计算图和运行时,以优化的方式处理图。这些图用作中间表示(IR),捕获开发人员源代码的特定意图,有助于优化和转换在特定设备(CPUGPUFPGA等)上运行。假设一个场景:现在某组织因为主要开发用TensorFlow为基础的框架,现在有一个深度算法,需要将其部署在移动设备上,以观测变现。传统地我们需要用Caffe2重新将模型写好,然后再训练参数;试想下这将是一个多么耗时耗力的过程。此时,ONNX便应运而生,Caffe2,PyTorch,Microsoft Cognitive Toolkit,Apache MXNet等主流框架都对ONNX有着不同程度的支持。这就便于我们的算法及模型在不同框架之间的迁移。

ONNX结构分析

ONNX将每一个网络的每一层或者说是每一个算子当作节点Node,再由这些Node去构建一个Graph,相当于是一个网络。最后将Graph和这个ONNX模型的其他信息结合在一起,生成一个Model,也就是最终的.onnx的模型。构建一个简单的ONNX模型,实质上,只要构建好每一个node,然后将它们和输入输出超参数一起塞到Graph,最后转成Model就可以了。

graph{
    node{
        input: "1"
        input: "2"
        output: "12"
        op_type: "Conv"
    }
    attribute{
        name: "strides"
        ints: 1
        ints: 1
    }
    attribute{
        name: "pads"
        ints: 2
        ints: 2
    }
    ...
}

我们查看ONNX网络结构和参数(查看网址:https://netron.app/)

fe08343a-4a1a-11ed-a3b6-dac502259ad0.png

ONNX安装、使用

安装ONNX环境,在终端中执行以下命令,环境中需要提前准本 python3.6. 以下流程以ubunt 20.04 为例。

模型转换流程

超分辨率是一种提高图像、视频分辨率的算法,广泛用于图像处理或视频编辑。首先,让我们在PyTorch中创建一个SuperResolution 模型。该模型使用描述的高效子像素卷积层将图像的分辨率提高了一个放大因子。该模型将图像的YCbCr的Y分量作为输入,并以超分辨率输出放大的Y分量。

# Some standard imports
import io
import numpy as np


from torch import nn
import torch.utils.model_zoo as model_zoo
import torch.onnx
# Super Resolution model definition in PyTorch
import torch.nn as nn
import torch.nn.init as init




class SuperResolutionNet(nn.Module):
    def __init__(self, upscale_factor, inplace=False):
        super(SuperResolutionNet, self).__init__()


        self.relu = nn.ReLU(inplace=inplace)
        self.conv1 = nn.Conv2d(1, 64, (5, 5), (1, 1), (2, 2))
        self.conv2 = nn.Conv2d(64, 64, (3, 3), (1, 1), (1, 1))
        self.conv3 = nn.Conv2d(64, 32, (3, 3), (1, 1), (1, 1))
        self.conv4 = nn.Conv2d(32, upscale_factor ** 2, (3, 3), (1, 1), (1, 1))
        self.pixel_shuffle = nn.PixelShuffle(upscale_factor)


        self._initialize_weights()


    def forward(self, x):
        x = self.relu(self.conv1(x))
        x = self.relu(self.conv2(x))
        x = self.relu(self.conv3(x))
        x = self.pixel_shuffle(self.conv4(x))
        return x


    def _initialize_weights(self):
        init.orthogonal_(self.conv1.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv2.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv3.weight, init.calculate_gain('relu'))
        init.orthogonal_(self.conv4.weight)


# Create the super-resolution model by using the above model definition.
torch_model = SuperResolutionNet(upscale_factor=3)

1

模型下载

由于本教程以演示为目的,因此采用下载预先训练好的权重。在导出模型之前调用torch_model.eval()或torch_model.train(False)将模型转换为推理模式很重要。因为dropout或batchnorm等运算符在推理和训练模式下的行为不同。

# Load pretrained model weights
model_url = 'https://s3.amazonaws.com/pytorch/test_data/export/superres_epoch100-44c6958e.pth'
batch_size = 1    # just a random number


# Initialize model with the pretrained weights
map_location = lambda storage, loc: storage
if torch.cuda.is_available():
    map_location = None
torch_model.load_state_dict(model_zoo.load_url(model_url, map_location=map_location))


# set the model to inference mode
torch_model.eval()

2

模型导出

要导出模型,我们调用该torch.onnx.export()函数。这将执行模型,记录用于计算输出的运算符。因为export运行模型,我们需要提供一个输入张量x。只要它是正确的类型和大小,其中的值可以是随机的。请注意,除非指定为动态轴,否则所有输入维度的导出ONNX图中的输入大小将是固定的。在此示例中,我们使用batch_size 1的输入导出模型,但随后在dynamic_axes参数中将第一个维度指定为动态 torch.onnx.export() . 因此,导出的模型将接受大小为[batch_size, 1, 224, 224]的输入,其中batch_size可以是可变的。

# Input to the model
x = torch.randn(batch_size, 1, 224, 224, requires_grad=True)
torch_out = torch_model(x)


# Export the model
torch.onnx.export(torch_model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "super_resolution.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable length axes
                                'output' : {0 : 'batch_size'}})

3

导出模型测试

在使用ONNX Runtime验证模型的输出之前,我们将使用ONNX的 API检查ONNX 模型。首先,onnx.load("super_resolution.onnx")将加载保存的模型并输出 onnx.ModelProto结构(用于捆绑 ML 模型的顶级文件/容器格式)。然后,onnx.checker.check_model(onnx_model)将验证模型的结构并确认模型具有有效的架构。ONNX 图的有效性通过检查模型的版本、图的结构以及节点及其输入和输出来验证。

import onnx


onnx_model = onnx.load("super_resolution.onnx")
onnx.checker.check_model(onnx_model)


import onnxruntime


ort_session = onnxruntime.InferenceSession("super_resolution.onnx")


def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()


# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)


# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05)


print("Exported model has been tested with ONNXRuntime, and the result looks good!")

1.加载处理前图片,使用标准PIL python库对其进行预处理。 2.调整图像大小以适应模型输入的大小 (224x224)。

审核编辑:彭静
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 神经网络
    +关注

    关注

    42

    文章

    4575

    浏览量

    98790
  • 模型
    +关注

    关注

    1

    文章

    2707

    浏览量

    47707
  • Graph
    +关注

    关注

    0

    文章

    36

    浏览量

    8834

原文标题:【技术基础】使用ONNX使模型通用化

文章出处:【微信号:FPGA创新中心,微信公众号:FPGA创新中心】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    将yolov5s的模型转成.onnx模型,进行cube-ai分析时报错的原因?

    报错显示张量不能大于四维的,想请教解决一下,我再此之后通过onnx-simplifier对.onnx进行简化之后再通过cube-ai进行分析还是出现上述报错,恳求指导,谢谢您!
    发表于 03-15 06:54

    cubemx ai导入onnx模型后压缩失败了怎么解决?

    cubemx ai导入onnx模型后压缩失败。请问我怎么解决
    发表于 03-19 07:58

    深度探索ONNX模型部署 精选资料分享

    这篇文章从多个角度探索了ONNX,从ONNX的导出到ONNX和Caffe的对比,以及使用ONNX遭遇的困难以及一些解决办法,另...
    发表于 07-20 07:41

    ONNX的相关资料分享

    ONNX简述ONNX是一种AI神经网络模型的通用中间文件保存方法(可以理解成AI世界的XML),各种AI框架,Inference引擎,甚至OpenCV里面的dnn onnx相关的模块都可以解析
    发表于 11-05 06:45

    如何使用Paddle2ONNX模型转换工具将飞桨模型转换为ONNX模型?

    如何使用Paddle2ONNX模型转换工具将飞桨模型转换为ONNX模型?
    发表于 12-29 07:42

    yolov5-face的pt模型是怎样转为onnx模型的

    yolov5-face的pt模型是怎样转为onnx模型的?有哪些转换步骤?
    发表于 02-21 07:19

    怎么解决rknntoolkit1.7.1转onnx报错的问题呢?

    -> Config model --> Loading onnx model I Start importing onnx... W Detect Input node:1325
    发表于 03-13 11:46

    EIQ onnx模型转换为tf-lite失败怎么解决?

    我们正在尝试将 tflite 框架与 npu 一起使用来进行机器学习。这是我们的步骤:1)用pytorch训练一个模型2) 以onnx格式导出模型3) eiq工具的covnert模型 我们遇到了这个
    发表于 03-31 08:03

    yolov7 onnx模型在NPU上太慢了怎么解决?

    我将 yolov7tiny.pt(yolov7-tiny 模型)转换为具有 uint8 权重的 yolov7tiny.onnx,然后在 i.MX 8M Plus NPU 上运行
    发表于 04-04 06:13

    ONNX模型转换为中间表示(IR)后,精度下降了怎么解决?

    ONNX 模型转换为 IR。 与使用 PyTorch 运行 ONNX 模型相比,Ran IR 采用 基准 C++ 工具,其性能准确率降低了 20%。 无法确定如何对图像进行预处理以获得更好的准确性。
    发表于 08-15 08:28

    将TensorFlow Lite模型转换为ONNX

    由 Facebook 和 Microsoft 创建的开放格式神经网络交换格式 ONNX,是一种用于表示机器学习模型。
    的头像 发表于 12-08 23:19 1091次阅读

    手工优化ncnn模型结构

    本文模型结构使用 netron visualizer 截图展示,支持 onnx 和 ncnn 模型的可视化
    发表于 01-26 18:48 0次下载
    手工优化ncnn模型<b class='flag-5'>结构</b>

    YOLOX模型ONNX格式说明

    我记得大概是在去年七月份的时候我写过一篇文章是介绍YOLOX+OpenVINO推理的,下载YOLOX的ONNX格式模型(github上可以下载)
    的头像 发表于 04-13 08:35 5002次阅读

    Pytorch转化ONNX过程代码实操

    一般来说转ONNX只是一个手段,在之后得到ONNX模型后还需要再将它做转换,比如转换到TensorRT上完成部署,或者有的人多加一步,从ONNX先转换到caffe,再从caffe到tensorRT。
    发表于 01-02 07:32 527次阅读

    AI推理框架软件ONNX Runtime正式支持龙架构

    近日,知名AI推理框架开源社区ONNX Runtime正式发布支持龙架构的版本1.17.0。
    的头像 发表于 03-12 12:23 267次阅读
    AI推理框架软件<b class='flag-5'>ONNX</b> Runtime正式支持龙架构