ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

onnx模型部署(一) ONNXRuntime

2021-12-27 14:59:21  阅读:358  来源: 互联网

标签:name ONNXRuntime onnx 模型 JVM ONNX np model


    通常我们在训练模型时可以使用很多不同的框架,比如有的同学喜欢用 Pytorch,有的同学喜欢使用 TensorFLow,也有的喜欢 MXNet,以及深度学习最开始流行的 Caffe等等,这样不同的训练框架就导致了产生不同的模型结果包,在模型进行部署推理时就需要不同的依赖库,而且同一个框架比如tensorflow 不同的版本之间的差异较大, 为了解决这个混乱问题, LF AI 这个组织联合 Facebook, MicroSoft等公司制定了机器学习模型的标准,这个标准叫做ONNX, Open Neural Network Exchage,所有其他框架产生的模型包 (.pth, .pb) 都可以转换成这个标准格式,转换成这个标准格式后,就可以使用统一的 ONNX Runtime等工具进行统一部署。

    这其实可以和 JVM 对比,
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation. Having a specification ensures interoperability of Java programs across different implementations so that program authors using the Java Development Kit (JDK) need not worry about idiosyncrasies of the underlying hardware platform.

JAVA中有 JAVA 语言 + .jar 包 + JVM,同时还有其他的语言比如 Scala等也是建立在 JVM上运行的,因此不同的语言只要都最后将程序转换成 JVM可以统一识别的格式,就可以在统一的跨平台 JVM JAVA 虚拟机上运行。这里JVM使用的 包是二进制包,因此里面的内容是不可知的,人类难以直观理解的。

这里 ONNX 标准采取了谷歌开发 protocal buffers 作为格式标准,这个格式是在 XML, json的基础上发展的,是一个人类易理解的格式。ONNX 官网对ONNX的介绍如下:
ONNX defines a common set of operators - the building blocks of machine learning and deep learning models - and a common file format to enable AI developers to use models with a variety of frameworks, tools, runtimes, and compilers.
ONNX支持的模型来源,基本上囊括了我们日常使用的所有框架:

在这里插入图片描述
ONNX的文件格式,采用的是谷歌的 protocal buffers,和 caffe采用的一致。

在这里插入图片描述
ONNX定义的数据类包括了我们常用的数据类型,用来定义模型中的输出输出格式
在这里插入图片描述
ONNX中定义了很多我们常用的节点,比如 Conv,ReLU,BN, maxpool等等约124种,同时也在不停地更新中,当遇到自带节点库中没有的节点时,我们也可以自己写一个节点

在这里插入图片描述
有了输入输出,以及计算节点,就可以根据 pytorch框架中的 forward 记录一张模型从输入图片到输出的计算图,ONNX 就是将这张计算图用标准的格式存储下来了,可以通过一个工具 Netron对 ONNX 进行可视化,如第一张图右侧所示;
保存成统一的 ONNX 格式后,就可以使用统一的运行平台来进行 inference。

pytorch原生支持 ONNX 格式转码,下面是实例:

1. 将pytorch模型转换为onnx格式,直接傻瓜式调用 torch.onnx.export(model, input, output_name)

import torch
from torchvision import models

net = models.resnet.resnet18(pretrained=True)
dummpy_input = torch.randn(1,3,224,224)
torch.onnx.export(net, dummpy_input, 'resnet18.onnx')

2. 对生成的 onnx 进行查看

import onnx

# Load the ONNX model
model = onnx.load("resnet18.onnx")

# Check that the IR is well formed
onnx.checker.check_model(model)

# Print a human readable representation of the graph
print(onnx.helper.printable_graph(model.graph))
  1. ONNX Runtime
    支持ONNX的runtime就是类似于JVM将统一的ONNX格式的模型包运行起来,包括对ONNX 模型进行解读,优化(融合conv-bn等操作),运行。

在这里插入图片描述
推理

import onnxruntime as rt
import numpy as  np
data = np.array(np.random.randn(1,3,224,224))
sess = rt.InferenceSession('resnet18.onnx')
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

pred_onx = sess.run([label_name], {input_name:data.astype(np.float32)})[0]
print(pred_onx)
print(np.argmax(pred_onx)

完整代码


import torch
from torchvision import models

net = models.resnet.resnet18(pretrained=True)
dummpy_input = torch.randn(1,3,224,224)
torch.onnx.export(net, dummpy_input, 'resnet18.onnx')

import onnx

# Load the ONNX model
model = onnx.load("resnet18.onnx")

# Check that the IR is well formed
onnx.checker.check_model(model)

# Print a human readable representation of the graph
print(onnx.helper.printable_graph(model.graph))


import onnxruntime as rt
import numpy as  np
data = np.array(np.random.randn(1,3,224,224))
sess = rt.InferenceSession('resnet18.onnx')
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

pred_onx = sess.run([label_name], {input_name:data.astype(np.float32)})[0]
print(pred_onx)
print(np.argmax(pred_onx))

完整代码

标签:name,ONNXRuntime,onnx,模型,JVM,ONNX,np,model
来源: https://blog.csdn.net/luoganttcc/article/details/122171291

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有