Proto and Serialization

Proto

AttributeProto

class onnx.AttributeProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

FunctionProto

class onnx.FunctionProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

GraphProto

class onnx.GraphProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

MapProto

class onnx.MapProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

ModelProto

class onnx.ModelProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

NodeProto

class onnx.NodeProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

OperatorProto

class onnx.OperatorProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

OperatorSetIdProto

class onnx.OperatorSetIdProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

OperatorSetProto

class onnx.OperatorSetProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

OptionalProto

class onnx.OptionalProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

SequenceProto

class onnx.SequenceProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

SparseTensorProto

class onnx.SparseTensorProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

StringStringEntryProto

TensorProto

class onnx.TensorProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Segment
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

TensorShapeProto

class onnx.TensorShapeProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Dimension
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

TrainingInfoProto

class onnx.TrainingInfoProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

TypeProto

class onnx.TypeProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Map
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Opaque
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Optional
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Sequence
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class SparseTensor
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

class Tensor
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

ValueInfoProto

class onnx.ValueInfoProto
DESCRIPTOR = <google._upb._message.Descriptor object>

The google.protobuf.descriptor.Descriptor for this message type.

Serialization

Load a model

onnx.load(f: Union[IO[bytes], str], format: Optional[Any] = None, load_external_data: bool = True) ModelProto

Loads a serialized ModelProto into memory load_external_data is true if the external data under the same directory of the model and load the external data If not, users need to call load_external_data_for_model with directory to load

Parameters:
  • f – can be a file-like object (has “read” function) or a string containing a file name

  • format – for future use

Returns:

Loaded in-memory ModelProto

from onnx import load

onnx_model = load("model.onnx")

Or:

from onnx import load

with open("model.onnx", "rb") as f:
    onnx_model = load(f)

Save a model

This ONNX graph needs to be serialized into one contiguous memory buffer. Method SerializeToString is available in every ONNX objects.

with open("model.onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())

This method has the following signature.

class onnx.ModelProto
SerializeToString()

Serializes the message to a string, only for initialized messages.

Load data

Data means here any type containing data including a model, a tensor, a sparse tensor…

onnx.load_model_from_string(s: bytes, format: Optional[Any] = None) ModelProto[source]

Loads a binary string (bytes) that contains serialized ModelProto

Parameters:
  • s – a string, which contains serialized ModelProto

  • format – for future use

Returns:

Loaded in-memory ModelProto

onnx.load_tensor_from_string(s: bytes, format: Optional[Any] = None) TensorProto[source]

Loads a binary string (bytes) that contains serialized TensorProto

Parameters:
  • s – a string, which contains serialized TensorProto

  • format – for future use

Returns:

Loaded in-memory TensorProto

protobuf does not store any information about the class of the saved data. Therefore, this class must be known before restoring an object.

Save data

Any Proto class includes a method called SerializeToString. It must be called to serialize any proto object into an array of bytes.

class onnx.TensorProto
SerializeToString()

Serializes the message to a string, only for initialized messages.