5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
120 lines
2.3 KiB
Markdown
120 lines
2.3 KiB
Markdown
(l-serialization)=
|
|
|
|
# Serialization
|
|
|
|
## Save a model and any Proto class
|
|
|
|
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.
|
|
|
|
```{eval-rst}
|
|
.. autoclass:: onnx.ModelProto
|
|
:members: SerializeToString
|
|
```
|
|
|
|
Every Proto class implements method `SerializeToString`.
|
|
Therefore the following code works with any class described
|
|
in page {ref}`l-onnx-classes`.
|
|
|
|
```
|
|
with open("proto.pb", "wb") as f:
|
|
f.write(proto.SerializeToString())
|
|
```
|
|
|
|
Next example shows how to save a {ref}`l-nodeproto`.
|
|
|
|
```{eval-rst}
|
|
.. exec_code::
|
|
|
|
from onnx import NodeProto
|
|
|
|
node = NodeProto()
|
|
node.name = "example-type-proto"
|
|
node.op_type = "Add"
|
|
node.input.extend(["X", "Y"])
|
|
node.output.extend(["Z"])
|
|
|
|
with open("node.pb", "wb") as f:
|
|
f.write(node.SerializeToString())
|
|
```
|
|
|
|
## Load a model
|
|
|
|
Following function only automates the loading of a class
|
|
{ref}`l-modelproto`. Next sections shows how to restore
|
|
any other proto class.
|
|
|
|
```{eval-rst}
|
|
.. autofunction:: onnx.load
|
|
```
|
|
|
|
```
|
|
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)
|
|
```
|
|
|
|
Next function does the same from a bytes array.
|
|
|
|
```{eval-rst}
|
|
.. autofunction:: onnx.load_model_from_string
|
|
|
|
```
|
|
|
|
(l-onnx-load-data)=
|
|
|
|
## Load a Proto
|
|
|
|
Proto means here any type containing data including a model, a tensor,
|
|
a sparse tensor, any class listed in page {ref}`l-onnx-classes`.
|
|
The user must know the type of the data he needs to restore
|
|
and then call method `ParseFromString`.
|
|
[protobuf](https://developers.google.com/protocol-buffers)
|
|
does not store any information about the class
|
|
of the saved data. Therefore, this class must be known before
|
|
restoring an object.
|
|
|
|
```{eval-rst}
|
|
.. autoclass:: onnx.ModelProto
|
|
:members: ParseFromString
|
|
```
|
|
|
|
Next example shows how to restore a {ref}`l-nodeproto`.
|
|
|
|
```{eval-rst}
|
|
.. exec_code::
|
|
|
|
from onnx import NodeProto
|
|
|
|
tp2 = NodeProto()
|
|
with open("node.pb", "rb") as f:
|
|
content = f.read()
|
|
|
|
tp2.ParseFromString(content)
|
|
|
|
print(tp2)
|
|
```
|
|
|
|
A shortcut exists for {ref}`l-tensorproto`:
|
|
|
|
```{eval-rst}
|
|
.. autofunction:: onnx.load_tensor_from_string
|
|
```
|