1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
(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
```
|