记录分为
环境:
pytorch 1.4.0
onnx 1.6.0 (转换)
onnxruntime 1.3.0 (测试)
onnx-simplifier 0.2.9 (模型量化,不执行后续报错了,我测试是这样的)
转换代码:
import onnx
import torch
from test_net import TestModel
import numpy as np
import cv2
if 1:
torch_model = TestModel("model.pt")
torch_model.eval()
batch_size = 1 #批处理大小
input_shape = (3,384,384) #输入数据
# set the model to inference mode
# torch_model.eval()
x = torch.randn(batch_size,*input_shape) # 生成张量
export_onnx_file = "./model.onnx" # 目的ONNX文件名
torch.onnx.export(torch_model,
x,
export_onnx_file,
export_params=True,
opset_version=11,
do_constant_folding=True, # wether 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 lenght axes
'output' : {0 : 'batch_size'}}
)
print ('get onnx ok!')
利用 onnxruntime 测试转换的模型:
import onnxruntime
import imageio
import time
(width, height) = (384,384)
cap = cv2.VideoCapture(0)
while 1:
ret,img = cap.read()
time_start = time.time()
if img is None:
print('no image input!')
break
if img.ndim == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
in_height ,in_width ,_ = img.shape
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
img_resized = cv2.resize(img, (width, height), interpolation=cv2.INTER_AREA)
img_resized = (
torch.from_numpy(np.transpose(img_resized, (2, 0, 1))).contiguous().float()
)
value = img_resized.unsqueeze(0)
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
ort_session = onnxruntime.InferenceSession("model.onnx")
ort_inputs = {ort_session.get_inputs()[0].name: (to_numpy(value)).astype(np.float32)}
#Actual: (N11onnxruntime17PrimitiveDataTypeIdEE) , expected: (N11onnxruntime17PrimitiveDataTypeIfEE)
#传入数据类型不对
ort_outs = ort_session.run(None, ort_inputs)
result = ort_outs[0][0, 0, :, :]
result = np.array(result)
print (result.shape)
reslut_resized = cv2.resize(
result, (in_width, in_height), interpolation=cv2.INTER_AREA
)
print('cost : %.3f (s)'%(time.time() - time_start))
cv2.namedWindow('re',2)
cv2.imshow('re',reslut_resized)
if cv2.waitKey(1) ==27:
break
cap.release()
cv2.destroyAllWindows()
模型简化操作:
python -m onnxsim model10.onnx model_sim.onnx --input-shape 1,3,384,384
python onnx2tnn.py model/model_sim.onnx -version=algo_version -optimize=1
0.----onnx version:1.6.0
结果为:
algo_optimize 1
onnx_net_opt_path /home/jiang/TNN-master/tools/onnx2tnn/onnx-converter/model/model_sim.opt.onnx
1.----onnx_optimizer: /home/jiang/TNN-master/tools/onnx2tnn/onnx-converter/model/model_sim.onnx
/home/jiang/TNN-master/tools/onnx2tnn/onnx-converter
----load onnx model: /home/jiang/TNN-master/tools/onnx2tnn/onnx-converter/model/model_sim.onnx
----onnxsim.simplify error: You'd better check the result with Netron
----onnxsim.simplify error: <class 'RuntimeError'>
----export optimized onnx model: /home/jiang/TNN-master/tools/onnx2tnn/onnx-converter/model/model_sim.opt.onnx
----export optimized onnx model done
2.----onnx2tnn: /home/jiang/TNN-master/tools/onnx2tnn/onnx-converter/model/model_sim.opt.onnx
get_node_attr_ai [Line 116] name :546
get_node_attr_ai [Line 116] name :585
get_node_attr_ai [Line 116] name :624
get_node_attr_ai [Line 116] name :663
get_node_attr_ai [Line 116] name :693
TNNLayerParam [Line 61] resize: coordinate_transformation_mode(pytorch_half_pixel) is not supported, result may be different.
3.----onnx2tnn status: 0
出现了错误----onnxsim.simplify error: You’d better check the result with Netron
----onnxsim.simplify error: <class ‘RuntimeError’>。
参数 algo_optimize=0即不进行优化就不保存转换成功。
先要安装Android环境;
本文地址:https://blog.csdn.net/qq_32768679/article/details/107106390
如对本文有疑问, 点击进行留言回复!!
Python | 用Python爬取LOL所有的英雄信息以及英雄皮肤
荐 Python之数据分析(坐标刻度定位器、散点图、柱状图、颜色区域填充)
剑指offer JZ31 整数中1出现的次数 Python 解
荐 opencv进阶学习笔记3:像素运算和图像亮度对比度调节
荐 Python全栈(八)Flask项目实战之5.CMS后台权限验证
在Python中使用moviepy进行音视频剪辑混音合成时输出文件无声音问题
Python基础核心经典教程(029)——面向对象特征之多态
网友评论