ONNX、onnxruntime学习记录及相关错误整理

1、介绍的比较好的博客
https://www.cnblogs.com/vh-pg/p/11736174.html
2、onnx算子
1)conv

weight=np.arange(36).reshape([2,2,3,3])
X=onnx.helper.make_tensor_value_info('X1',TensorProto.FLOAT,[1,2,5,5])
W=onnx.helper.make_tensor('W1',TensorProto.FLOAT,[2,2,3,3],weight)
B=onnx.helper.make_tensor('B1',TensorProto.FLOAT,[2],[3.0,5.0])
Y=onnx.helper.make_tensor_value_info('Y1',TensorProto.FLOAT,[1,2,2,2])
conv1=onnx.helper.make_node(
    'Conv',
    ['X1','W1','B1'], #W1是必须加, B1不是必须加的
    ['Y1'],
    padding=[1,1,0,0], #第一个1决定在图像的上面加一行元素,元素的值为0,第二个1决定在图像左侧加一竖排元素,第三个0表示在图像下面加一行元素
    strides=[2,2] 
)
graph_def=onnx.helper.make_graph(
    [conv1],
    'test_conv',
    [X],
    [Y]
  initializer=[W,B] #W和B必须加,顺序无所谓
)

2)RELU只需要make_tensor_value_info,不需要initializer

import onnx 
import numpy as np
from onnx import TensorProto
import os

X=onnx.helper.make_tensor_value_info('X1',TensorProto.FLOAT,[2,2,3,3])
Y=onnx.helper.make_tensor_value_info('Y1',TensorProto.FLOAT,[2,2,3,3])
relu1=onnx.helper.make_node(
    'Relu',
    ['X1'],
    ['Y1']   
)

graph_def=onnx.helper.make_graph(
    [relu1],
    'test_conv',
    [X],
    [Y]  
)
mode_def=onnx.helper.make_model(graph_def,opset_imports=[onnx.helper.make_opsetid("",11)])
onnx.checker.check_model(mode_def)
onnx.save(mode_def,'./relu1.onnx')

3)Sub

X1=onnx.helper.make_tensor_value_info('X1',TensorProto.FLOAT,[1,5])

X2=onnx.helper.make_tensor_value_info('X2',TensorProto.FLOAT,[1,5])
Y1=onnx.helper.make_tensor_value_info('Y1',TensorProto.FLOAT,[1,5])

sub1=onnx.helper.make_node(
    'Sub',
    ['X2','X1'],  #结果是X2-X1
    ['Y1'], 
)
graph_def=onnx.helper.make_graph(
    [sub1],
    'test_conv',
    [X1,X2],
    [Y1]
)
mode_def=onnx.helper.make_model(graph_def,opset_imports=[onnx.helper.make_opsetid("",11)])
onnx.checker.check_model(mode_def)
onnx.save(mode_def,'./sub1.onnx')

二、使用错误处理
1、使用helper.make_model和onnx.save生成onnx模型 ,但onnxruntime.InferenceSession时无法运行报错:
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from linear_func2.onnx failed:/onnxruntime_src/onnxruntime/core/graph/model_load_utils.h:47 void onnxruntime::model_load_utils::ValidateOpsetForDomain(const std::unordered_map&, const onnxruntime::logging::Logger&, bool, const string&, int) ONNX Runtime only guarantees support for models stamped with official released onnx opset versions. Opset 17 is under development and support for this is limited. The operator schemas and or other functionality may change before next ONNX release and in this case ONNX Runtime will not guarantee backward compatibility. Current official support for domain ai.onnx is till opset 15.
原因:
helper.make_model如果不指定版本,可能生成的版本是17,而不是电脑支持的版本。
ONNX、onnxruntime学习记录及相关错误整理_第1张图片
修改方法:
helper.make_model(graph_def,opset_imports=[helper.make_opsetid(“”, 11)]),通过opset_imports指定版本imports的参数,就可以了。
2、AttributeError: module ‘onnxruntime‘ has no attribute ‘set_default_logger_severity‘

import onnx
import onnxruntime
sess=onnxruntime.InferenceSession("/home/cxhpc/Public/lhz/detection_sidev2/model/lane_loc/lane_localization.onnx")

只写这么一句也报错:
在这里插入图片描述
是onnxruntime库出问题了,可能是被卸载了,重新安装即可

pip3 uninstall onnxruntime
pip3 install onnxruntime

你可能感兴趣的:(神经网络,学习,算法)