最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch模型轉(zhuǎn)換為onnx可視化(使用netron)

 更新時(shí)間:2023年05月18日 09:44:53   作者:michaelchengjl  
netron 是一個(gè)非常好用的網(wǎng)絡(luò)結(jié)構(gòu)可視化工具,但是netron對(duì)pytorch模型的支持還不成熟,這篇文章主要介紹了pytorch模型轉(zhuǎn)換為onnx,并使用netron可視化,需要的朋友可以參考下

pytorch模型轉(zhuǎn)換為onnx,并使用netron可視化

netron 是一個(gè)非常好用的網(wǎng)絡(luò)結(jié)構(gòu)可視化工具。

但是netron對(duì)pytorch模型的支持還不成熟。自己試的效果是生成的模型圖沒(méi)有連線。

目前支持的框架 根據(jù)netron的github

目前netron支持:

ONNX (.onnx, .pb, .pbtxt)
Keras (.h5, .keras)
Core ML (.mlmodel)
Caffe (.caffemodel, .prototxt)
Caffe2 (predict_net.pb, predict_net.pbtxt)
Darknet (.cfg)
MXNet (.model, -symbol.json)
ncnn (.param) 
TensorFlow Lite (.tflite)
PaddlePaddle (.zip, model)
TensorFlow.js
CNTK (.model, .cntk)

并且實(shí)驗(yàn)性支持:

TorchScript (.pt, .pth)
PyTorch (.pt, .pth)
Torch (.t7)
Arm NN (.armnn)
BigDL (.bigdl, .model) 
Chainer (.npz, .h5)
Deeplearning4j (.zip)
MediaPipe (.pbtxt)
ML.NET (.zip), MNN (.mnn)
OpenVINO (.xml)
scikit-learn (.pkl)
TensorFlow (.pb, .meta, .pbtxt, .ckpt, .index)

Netron supports ONNX, TensorFlow Lite, Caffe, Keras, Darknet, PaddlePaddle, ncnn, MNN, Core ML, RKNN, MXNet, MindSpore Lite, TNN, Barracuda, Tengine, CNTK, TensorFlow.js, Caffe2 and UFF.

Netron has experimental support for PyTorch, TensorFlow, TorchScript, OpenVINO, Torch, Vitis AI, kmodel, Arm NN, BigDL, Chainer, Deeplearning4j, MediaPipe, ML.NET and scikit-learn.

這里就有一個(gè)把 .pth 模型轉(zhuǎn)化為 .onnx 模型。

Pytorch模型轉(zhuǎn)onnx

model = resnet18(pretrained=True)
# print(model)
# old_net_path = "resnet18.pth"
new_net_path = "./resnet18.onnx"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 導(dǎo)入模型
net = model.to(device)
# net.load_state_dict(torch.load(old_net_path, map_location=device))
net.eval()
input = torch.randn(1, 3, 224, 224).to(device)  # BCHW  其中Batch必須為1,因?yàn)闇y(cè)試時(shí)一般為1,尺寸HW必須和訓(xùn)練時(shí)的尺寸一致
torch.onnx.export(net, input, new_net_path, verbose=False)

torch.onnx.export(model, args, f, export_params=True, verbose=False, training=False, input_names=None, output_names=None)

參數(shù):

model(torch.nn.Module)-要被導(dǎo)出的模型
args(參數(shù)的集合)-模型的輸入,例如,這種model(*args)方式是對(duì)模型的有效調(diào)用。任何非Variable參數(shù)都將硬編碼到導(dǎo)出的模型中;任何Variable參數(shù)都將成為導(dǎo)出的模型的輸入,并按照他們?cè)赼rgs中出現(xiàn)的順序輸入。如果args是一個(gè)Variable,這等價(jià)于用包含這個(gè)Variable的1-ary元組調(diào)用它。(注意:現(xiàn)在不支持向模型傳遞關(guān)鍵字參數(shù)。)
f-一個(gè)類文件的對(duì)象(必須實(shí)現(xiàn)文件描述符的返回)或一個(gè)包含文件名字符串。一個(gè)二進(jìn)制Protobuf將會(huì)寫入這個(gè)文件中。
export_params(bool,default True)-如果指定,所有參數(shù)都會(huì)被導(dǎo)出。如果你只想導(dǎo)出一個(gè)未訓(xùn)練的模型,就將此參數(shù)設(shè)置為False。在這種情況下,導(dǎo)出的模型將首先把所有parameters作為參arguments,順序由model.state_dict().values()指定。
verbose(bool,default False)-如果指定,將會(huì)輸出被導(dǎo)出的軌跡的調(diào)試描述。
training(bool,default False)-導(dǎo)出訓(xùn)練模型下的模型。目前,ONNX只面向推斷模型的導(dǎo)出,所以一般不需要將該項(xiàng)設(shè)置為True。
input_names(list of strings, default empty list)-按順序分配名稱到圖中的輸入節(jié)點(diǎn)。
output_names(list of strings, default empty list)-按順序分配名稱到圖中的輸出節(jié)點(diǎn)。

文件中保存模型結(jié)構(gòu)和權(quán)重參數(shù)

import torch
torch_model = torch.load("save.pt") # pytorch模型加載
batch_size = 1  #批處理大小
input_shape = (3,244,244)   #輸入數(shù)據(jù)
# set the model to inference mode
torch_model.eval()
x = torch.randn(batch_size,*input_shape)		# 生成張量
export_onnx_file = "test.onnx"					# 目的ONNX文件名
torch.onnx.export(torch_model,
                    x,
                    export_onnx_file,
                    opset_version=10,
                    do_constant_folding=True,	# 是否執(zhí)行常量折疊優(yōu)化
                    input_names=["input"],		# 輸入名
                    output_names=["output"],	# 輸出名
                    dynamic_axes={"input":{0:"batch_size"},		# 批處理變量
                    "output":{0:"batch_size"}})

dynamic_axes字段用于批處理.若不想支持批處理或固定批處理大小,移除dynamic_axes字段即可.

文件中只保留模型權(quán)重

import torch
torch_model = selfmodel()  					# 由研究員提供python.py文件
batch_size = 1 								# 批處理大小
input_shape = (3, 244, 244) 				# 輸入數(shù)據(jù)
# set the model to inference mode
torch_model.eval()
x = torch.randn(batch_size,*input_shape) 	# 生成張量
export_onnx_file = "test.onnx" 				# 目的ONNX文件名
torch.onnx.export(torch_model,
                    x,
                    export_onnx_file,
                    opset_version=10,
                    do_constant_folding=True,	# 是否執(zhí)行常量折疊優(yōu)化
                    input_names=["input"],		# 輸入名
                    output_names=["output"],	# 輸出名
                    dynamic_axes={"input":{0:"batch_size"},	# 批處理變量
                    "output":{0:"batch_size"}})

到此這篇關(guān)于pytorch模型轉(zhuǎn)換為onnx可視化(使用netron)的文章就介紹到這了,更多相關(guān)pytorch模型轉(zhuǎn)onnx可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

胶南市| 石楼县| 桂平市| 乌鲁木齐市| 德惠市| 将乐县| 揭西县| 赣州市| 新干县| 屏边| 建阳市| 建昌县| 独山县| 修文县| 沙雅县| 泰宁县| 肥城市| 湟中县| 固阳县| 海盐县| 莲花县| 五指山市| 永昌县| 嵊泗县| 翼城县| 东丽区| 崇礼县| 陵水| 凉山| 通江县| 正安县| 伊宁县| 西青区| 长宁县| 吉林市| 航空| 屏边| 绍兴市| 翁源县| 舟曲县| 漳浦县|