Serialization

Load a model

onnx.load(f: Union[IO[bytes], str], format: Optional[Any] = None, load_external_data: bool = True) onnx.onnx_ml_pb2.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

@params f can be a file-like object (has “read” function) or a string containing a file name format is for future use

@return 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())