日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

onnx模型部署(一) ONNXRuntime

發(fā)布時(shí)間:2025/4/5 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 onnx模型部署(一) ONNXRuntime 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????通常我們?cè)谟?xùn)練模型時(shí)可以使用很多不同的框架,比如有的同學(xué)喜歡用 Pytorch,有的同學(xué)喜歡使用 TensorFLow,也有的喜歡 MXNet,以及深度學(xué)習(xí)最開始流行的 Caffe等等,這樣不同的訓(xùn)練框架就導(dǎo)致了產(chǎn)生不同的模型結(jié)果包,在模型進(jìn)行部署推理時(shí)就需要不同的依賴庫,而且同一個(gè)框架比如tensorflow 不同的版本之間的差異較大, 為了解決這個(gè)混亂問題, LF AI 這個(gè)組織聯(lián)合 Facebook, MicroSoft等公司制定了機(jī)器學(xué)習(xí)模型的標(biāo)準(zhǔn),這個(gè)標(biāo)準(zhǔn)叫做ONNX, Open Neural Network Exchage,所有其他框架產(chǎn)生的模型包 (.pth, .pb) 都可以轉(zhuǎn)換成這個(gè)標(biāo)準(zhǔn)格式,轉(zhuǎn)換成這個(gè)標(biāo)準(zhǔn)格式后,就可以使用統(tǒng)一的 ONNX Runtime等工具進(jìn)行統(tǒng)一部署。

????這其實(shí)可以和 JVM 對(duì)比,
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,同時(shí)還有其他的語言比如 Scala等也是建立在 JVM上運(yùn)行的,因此不同的語言只要都最后將程序轉(zhuǎn)換成 JVM可以統(tǒng)一識(shí)別的格式,就可以在統(tǒng)一的跨平臺(tái) JVM JAVA 虛擬機(jī)上運(yùn)行。這里JVM使用的 包是二進(jìn)制包,因此里面的內(nèi)容是不可知的,人類難以直觀理解的。

這里 ONNX 標(biāo)準(zhǔn)采取了谷歌開發(fā) protocal buffers 作為格式標(biāo)準(zhǔn),這個(gè)格式是在 XML, json的基礎(chǔ)上發(fā)展的,是一個(gè)人類易理解的格式。ONNX 官網(wǎng)對(duì)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支持的模型來源,基本上囊括了我們?nèi)粘J褂玫乃锌蚣?#xff1a;


ONNX的文件格式,采用的是谷歌的 protocal buffers,和 caffe采用的一致。


ONNX定義的數(shù)據(jù)類包括了我們常用的數(shù)據(jù)類型,用來定義模型中的輸出輸出格式

ONNX中定義了很多我們常用的節(jié)點(diǎn),比如 Conv,ReLU,BN, maxpool等等約124種,同時(shí)也在不停地更新中,當(dāng)遇到自帶節(jié)點(diǎn)庫中沒有的節(jié)點(diǎn)時(shí),我們也可以自己寫一個(gè)節(jié)點(diǎn)


有了輸入輸出,以及計(jì)算節(jié)點(diǎn),就可以根據(jù) pytorch框架中的 forward 記錄一張模型從輸入圖片到輸出的計(jì)算圖,ONNX 就是將這張計(jì)算圖用標(biāo)準(zhǔn)的格式存儲(chǔ)下來了,可以通過一個(gè)工具 Netron對(duì) ONNX 進(jìn)行可視化,如第一張圖右側(cè)所示;
保存成統(tǒng)一的 ONNX 格式后,就可以使用統(tǒng)一的運(yùn)行平臺(tái)來進(jìn)行 inference。

pytorch原生支持 ONNX 格式轉(zhuǎn)碼,下面是實(shí)例:

1. 將pytorch模型轉(zhuǎn)換為onnx格式,直接傻瓜式調(diào)用 torch.onnx.export(model, input, output_name)

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

2. 對(duì)生成的 onnx 進(jìn)行查看

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))
  • ONNX Runtime
    支持ONNX的runtime就是類似于JVM將統(tǒng)一的ONNX格式的模型包運(yùn)行起來,包括對(duì)ONNX 模型進(jìn)行解讀,優(yōu)化(融合conv-bn等操作),運(yùn)行。

  • 推理

    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].namepred_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 modelsnet = 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].namepred_onx = sess.run([label_name], {input_name:data.astype(np.float32)})[0] print(pred_onx) print(np.argmax(pred_onx))

    完整代碼

    總結(jié)

    以上是生活随笔為你收集整理的onnx模型部署(一) ONNXRuntime的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。