😘ONNX IR(Intermediate Representation)
ONNX部分
ONNX是一种神经网络的格式,采用Protobuf二进制形式进行序列化模型。 Protobuf会根据用于定义的数据结构来进行序列化存储 同理,我们可以根据官方提供的数据结构信息,去修改或者创建onnx。

onnx的各类proto的定义需要看官方文档(https://github.com/onnx/onnx/tree/main) 。这里面的onnx/onnx.in.proto定义了所有onnx的Proto。有关onnx的IR(intermediate representation)信息,看这里(https://github.com/onnx/onnx/blob/main/docs/IR.md)
理解onnx中的组织结构
ModelProto(描述的是整个模型的信息)GraphProto(描述的是整个网络的信息)NodeProto(描述的是各个计算节点,比如conv,linear)TensorProto(描述的是tensor的信息,主要包括权重)ValueInfoProto(描述的是input/output信息)AttributeProto(描述的是node节点的各种属性信息)
onnx中的ValueInfoProto
一般用来定义网络的input/output ,会根据input/output的type来附加属性


onnx中的TensorProto



一般用来定义一个权重,比如conv的w和b ,dims是repeated类型,意味着是数组,raw_data是bytes类型。
onnx中的NodeProto

一般用来定义一个计算节点,比如conv, linear ,input是repeated类型,意味着是数组,output是repeated类型,意味着是数组,attribute有一个自己的Proto,op_type需要严格根据onnx所提供的Operators写。
onnx中的AttributeProto


一般用来定义一个node的属性。比如说kernel size,比较常见的方式就是把(key, value)传入Proto,之后 name = key ,i = value 。
onnx中的GraphProto

一般用来定义一个网络。包括 :input/output ,initializer,initializer: 在onnx中一般表示权重信息。 node是repeated,所以是数组,initializer是repeated,所以是数组,input/output是repeated,所以是数组。
onnx中的ModelProto

一般用来定义模型的全局信息,比如opset ,graph并不是repeated,所以一个model对应一个graph
🥰onnx提供了一些很方便的api来创建onnx :onnx.helper.make_tensor onnx.helper.make_tensor_value_info ,onnx.helper.make_attribute onnx.helper.make_node ,onnx.helper.make_graph ,onnx.helper.make_model
虽然onnx官方提供了而一些python API来修改onnx,但是推荐大家使用TensorRT下的onnxsurgeon,更加方便快捷。
Last updated
Was this helpful?