q
什么是onnx?
onnx(open neural network exchange)- 开放神经网络交换格式,作为框架共用的一种模型交换格式,使用protobuf 二进制格式来序列化模型,可以提供更好的传输性能我们可能会在某一任务中pytorch或者tensorflow模型转化为onnx模型(onnx模型一般用于中间部署阶段),然后再拿转化后的onnx模型进而转化为我们使用不同框架部署需要的类型,onnx相当于一个翻译的作用。
q
为什么要用onnx?
深度学习算法大多通过计算数据流图来完成神经网络的深度学习过程。一些框架(例如cntk,caffe2,theano和tensorflow)使用静态图形,而其他框架(例如pytorch和chainer)使用动态图形。但是这些框架都提供了接口,使开发人员可以轻松构建计算图和运行时,以优化的方式处理图。这些图用作中间表示(ir),捕获开发人员源代码的特定意图,有助于优化和转换在特定设备(cpu,gpu,fpga等)上运行。假设一个场景:现在某组织因为主要开发用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/)
onnx安装、使用
安装onnx环境,在终端中执行以下命令,环境中需要提前准本 python3.6. 以下流程以ubunt 20.04 为例。
模型转换流程
超分辨率是一种提高图像、视频分辨率的算法,广泛用于图像处理或视频编辑。首先,让我们在pytorch中创建一个superresolution 模型。该模型使用描述的高效子像素卷积层将图像的分辨率提高了一个放大因子。该模型将图像的ycbcr的y分量作为输入,并以超分辨率输出放大的y分量。
# some standard importsimport ioimport numpy as npfrom torch import nnimport torch.utils.model_zoo as model_zooimport torch.onnx# super resolution model definition in pytorchimport torch.nn as nnimport torch.nn.init as initclass 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 weightsmodel_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 weightsmap_location = lambda storage, loc: storageif torch.cuda.is_available(): map_location = nonetorch_model.load_state_dict(model_zoo.load_url(model_url, map_location=map_location))# set the model to inference modetorch_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 modelx = torch.randn(batch_size, 1, 224, 224, requires_grad=true)torch_out = torch_model(x)# export the modeltorch.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 onnxonnx_model = onnx.load(super_resolution.onnx)onnx.checker.check_model(onnx_model)import onnxruntimeort_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 predictionort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}ort_outs = ort_session.run(none, ort_inputs)# compare onnx runtime and pytorch resultsnp.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)。
MMS在中国市场的首秀,独有的智能汽车摄像头开发套件
ZH-YLS-12A型 鼠尾测痛仪-功能说明
盘点那些采用了黑科技的高端平板
战略合作 | IAR全面支持云途车规级MCU
无线通信标准大PK
基于ONNX结构分析
计划总投资3亿元!瑞声科技马来西亚智能制造基地投产
数字IC验证之基本的TLM通信
光纤温度传感器分类_光纤温度传感器发展前景
9个优秀的开源工单系统,你知道几个
井贤栋:蚂蚁金服要做智慧城市新引擎
基于高光谱成像技术的双孢蘑菇病害早期诊
苹果Homekit来了,您准备好了吗?
iphone8什么时候上市?iphone8概念图流出:后双摄光学变焦+卡尔察司,颜值升级堪称新一代怪兽
XREAL与宝马集团展示智能AR辅助的未来驾驶体验
如何构建一个低成本的Arduino MiniCNC绘图仪
动态补偿装置的元件构成与注意事项有哪些?
C语言函数的回调函数
PLC逻辑梯形图编程的特点
中国联通面向交通重磅发布了5G+车联网与智慧交通系列产品