构建onnx方式通常有两种:
1、通过代码转换成onnx结构,比如pytorch —> onnx
2、通过onnx 自定义结点,图,生成onnx结构
本文主要是简单学习和使用两种不同onnx结构,
下面以 Conv
结点进行分析
暂缓,主要研究方式二
import onnx
from onnx import helper
from onnx import AttributeProto, TensorProto, GraphProto
import numpy as np
def run():
print("run start....\n")
# 定义卷积层节点
conv = helper.make_node('Conv',
inputs=['input', 'weights', 'bias'],
outputs=['output'],
kernel_shape=[3, 3],
strides=[1, 1],
pads=[1, 1, 1, 1])
# 定义输入节点
input = helper.make_tensor_value_info('input', TensorProto.FLOAT, [1, 3, 224, 224])
# 定义权重节点
weights = helper.make_tensor('weights', TensorProto.FLOAT, [64, 3, 3, 3], ([0.1]*1728))
# 定义偏置节点
bias = helper.make_tensor('bias', TensorProto.FLOAT, [64], ([0.2]*64))
# 定义输出节点
output = helper.make_tensor_value_info('output', TensorProto.FLOAT, [1, 64, 224, 224])
graph = helper.make_graph(
nodes=[conv],
name="test_graph",
inputs=[input],
outputs=[output],
initializer=[weights, bias],
)
op = onnx.OperatorSetIdProto()
op.version = 11
model = helper.make_model(graph, opset_imports=[op])
print("run done....\n")
return model
if __name__ == "__main__":
model = run()
onnx.save(model, "./test_conv2d.onnx")```