构建onnx方式通常有两种:
1、通过代码转换成onnx结构,比如pytorch —> onnx
2、通过onnx 自定义结点,图,生成onnx结构
本文主要是简单学习和使用两种不同onnx结构,
下面以 Unfold
结点进行分析
暂缓,主要研究方式二
import onnx
from onnx import helper, numpy_helper
from onnx import TensorProto
# 创建一个unfold onnx node
node = helper.make_node(
'Unfold',
inputs=['input', 'kernel_shape', 'dilations', 'pads', 'strides'],
outputs=['output'],
)
# 创建一个简单的onnx模型
graph = helper.make_graph(
[node],
'unfold_model',
inputs=[
helper.make_tensor_value_info('input', TensorProto.FLOAT, [1, 3, 224, 224]),
helper.make_tensor_value_info('kernel_shape', TensorProto.INT64, [2]),
helper.make_tensor_value_info('dilations', TensorProto.INT64, [2]),
helper.make_tensor_value_info('pads', TensorProto.INT64, [4]),
helper.make_tensor_value_info('strides', TensorProto.INT64, [2]),
],
outputs=[
helper.make_tensor_value_info('output', TensorProto.FLOAT, [1, 3, 224, 224]),
],
)
model = helper.make_model(graph)
onnx.save(model, 'unfold_model.onnx')