mirror of
https://github.com/luxfi/onnx-source-snapshot.git
synced 2026-10-30 15:23:38 +00:00
4.5 KiB
4.5 KiB
In [1]:
# NBVAL_SKIP
# Protobuf 4 and Protobuf 3 might output different order of protobuf fields
import onnx
from onnx import helper
from onnx import AttributeProto, TensorProto, GraphProto
# The protobuf definition can be found here:
# https://github.com/onnx/onnx/blob/main/onnx/onnx.proto
# Create one input (ValueInfoProto)
X = helper.make_tensor_value_info("X", TensorProto.FLOAT, [1, 2])
# Create second input (ValueInfoProto)
Pads = helper.make_tensor_value_info("Pads", TensorProto.INT64, [4])
# Create one output (ValueInfoProto)
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1, 4])
# Create a node (NodeProto)
node_def = helper.make_node(
"Pad", # node name
["X", "Pads"], # inputs
["Y"], # outputs
mode="constant", # Attributes
)
# Create the graph (GraphProto)
graph_def = helper.make_graph(
[node_def],
"test-model",
[X, Pads],
[Y],
[helper.make_tensor("Pads", TensorProto.INT64, [4,], [0, 0, 1, 1,])],
)
# Create the model (ModelProto)
model_def = helper.make_model(graph_def,
producer_name="onnx-example")
print("The producer_name in model: {}\n".format(model_def.producer_name))
print("The graph in model:\n{}".format(model_def.graph))
onnx.checker.check_model(model_def)
print("The model is checked!")The producer_name in model: onnx-example
The graph in model:
node {
input: "X"
input: "Pads"
output: "Y"
op_type: "Pad"
attribute {
name: "mode"
s: "constant"
type: STRING
}
}
name: "test-model"
initializer {
dims: 4
data_type: 7
int64_data: 0
int64_data: 0
int64_data: 1
int64_data: 1
name: "Pads"
}
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 2
}
}
}
}
}
input {
name: "Pads"
type {
tensor_type {
elem_type: 7
shape {
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 4
}
}
}
}
}
The model is checked!
In [ ]: