mirror of
https://github.com/luxfi/onnx-source-snapshot.git
synced 2026-10-30 15:23:38 +00:00
16 KiB
16 KiB
In [1]:
from onnx import *In [2]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# Int Attibute
arg = helper.make_attribute("this_is_an_int", 1701)
print("\nInt attribute:\n")
print(arg)Int attribute: name: "this_is_an_int" i: 1701 type: INT
In [3]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# Float Attribute
arg = helper.make_attribute("this_is_a_float", 3.14)
print("\nFloat attribute:\n")
print(arg)Float attribute: name: "this_is_a_float" f: 3.140000104904175 type: FLOAT
In [4]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# String Attribute
arg = helper.make_attribute("this_is_a_string", "string_content")
print("\nString attribute:\n")
print(arg)String attribute: name: "this_is_a_string" s: "string_content" type: STRING
In [5]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# Repeated Attribute
arg = helper.make_attribute("this_is_a_repeated_int", [1, 2, 3, 4])
print("\nRepeated int attribute:\n")
print(arg)Repeated int attribute: name: "this_is_a_repeated_int" ints: 1 ints: 2 ints: 3 ints: 4 type: INTS
In [6]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# node
node_proto = helper.make_node("Relu", ["X"], ["Y"])
print("\nNodeProto:\n")
print(node_proto)NodeProto: input: "X" output: "Y" op_type: "Relu"
In [7]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# node with args
node_proto = helper.make_node(
"Conv", ["X", "W", "B"], ["Y"],
kernel=3, stride=1, pad=1)
# This is just for making the attributes to be printed in order
node_proto.attribute.sort(key=lambda attr: attr.name)
print("\nNodeProto:\n")
print(node_proto)
print("\nMore Readable NodeProto (no args yet):\n")
print(helper.printable_node(node_proto))NodeProto:
input: "X"
input: "W"
input: "B"
output: "Y"
op_type: "Conv"
attribute {
name: "kernel"
i: 3
type: INT
}
attribute {
name: "pad"
i: 1
type: INT
}
attribute {
name: "stride"
i: 1
type: INT
}
More Readable NodeProto (no args yet):
%Y = Conv[kernel = 3, pad = 1, stride = 1](%X, %W, %B)
In [8]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# graph
graph_proto = helper.make_graph(
[
helper.make_node("FC", ["X", "W1", "B1"], ["H1"]),
helper.make_node("Relu", ["H1"], ["R1"]),
helper.make_node("FC", ["R1", "W2", "B2"], ["Y"]),
],
"MLP",
[
helper.make_tensor_value_info("X" , TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("W1", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("B1", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("W2", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("B2", TensorProto.FLOAT, [1]),
],
[
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1]),
]
)
print("\ngraph proto:\n")
print(graph_proto)
print("\nMore Readable GraphProto:\n")
print(helper.printable_graph(graph_proto))graph proto:
node {
input: "X"
input: "W1"
input: "B1"
output: "H1"
op_type: "FC"
}
node {
input: "H1"
output: "R1"
op_type: "Relu"
}
node {
input: "R1"
input: "W2"
input: "B2"
output: "Y"
op_type: "FC"
}
name: "MLP"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "W1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "B1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "W2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "B2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
More Readable GraphProto:
graph MLP (
%X[FLOAT, 1]
%W1[FLOAT, 1]
%B1[FLOAT, 1]
%W2[FLOAT, 1]
%B2[FLOAT, 1]
) {
%H1 = FC(%X, %W1, %B1)
%R1 = Relu(%H1)
%Y = FC(%R1, %W2, %B2)
return %Y
}
In [9]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
# An node that is also a graph
graph_proto = helper.make_graph(
[
helper.make_node("FC", ["X", "W1", "B1"], ["H1"]),
helper.make_node("Relu", ["H1"], ["R1"]),
helper.make_node("FC", ["R1", "W2", "B2"], ["Y"]),
],
"MLP",
[
helper.make_tensor_value_info("X" , TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("W1", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("B1", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("W2", TensorProto.FLOAT, [1]),
helper.make_tensor_value_info("B2", TensorProto.FLOAT, [1]),
],
[
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1]),
]
)
# output = ThisSpecificgraph([input, w1, b1, w2, b2])
node_proto = helper.make_node(
"graph",
["Input", "W1", "B1", "W2", "B2"],
["Output"],
graph=[graph_proto],
)
print("\nNodeProto that contains a graph:\n")
print(node_proto)NodeProto that contains a graph:
input: "Input"
input: "W1"
input: "B1"
input: "W2"
input: "B2"
output: "Output"
op_type: "graph"
attribute {
name: "graph"
graphs {
node {
input: "X"
input: "W1"
input: "B1"
output: "H1"
op_type: "FC"
}
node {
input: "H1"
output: "R1"
op_type: "Relu"
}
node {
input: "R1"
input: "W2"
input: "B2"
output: "Y"
op_type: "FC"
}
name: "MLP"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "W1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "B1"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "W2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
input {
name: "B2"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
}
type: GRAPHS
}
In [ ]: