Skip to content

Commit 1cacda1

Browse files
authored
Python API Overview: minor improvements for clarity (onnx#3446)
Also use repo-relative links so that they'll work as expected locally or in a branch. Signed-off-by: Gary Miguel <garymiguel@microsoft.com>
1 parent ad96f22 commit 1cacda1

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

docs/PythonAPIOverview.md

+27-20
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
```python
77
import onnx
88

9+
# onnx_model is an in-memory ModelProto
910
onnx_model = onnx.load('path/to/the/model.onnx')
10-
# `onnx_model` is a ModelProto struct
1111
```
1212
Runnable IPython notebooks:
13-
- [load_model.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/load_model.ipynb)
13+
- [load_model.ipynb](/onnx/examples/load_model.ipynb)
1414

1515
## Loading an ONNX Model with External Data
1616

@@ -37,7 +37,8 @@ load_external_data_for_model(onnx_model, 'data/directory/path/')
3737
```python
3838
from onnx.external_data_helper import convert_model_to_external_data
3939

40-
onnx_model = ... # Your model in memory as ModelProto
40+
# onnx_model is an in-memory ModelProto
41+
onnx_model = ...
4142
convert_model_to_external_data(onnx_model, all_tensors_to_one_file=True, location='filename', size_threshold=1024, convert_attribute=False)
4243
# Then the onnx_model has converted raw data as external data
4344
# Must be followed by save
@@ -47,20 +48,22 @@ convert_model_to_external_data(onnx_model, all_tensors_to_one_file=True, locatio
4748
```python
4849
import onnx
4950

50-
onnx_model = ... # Your model in memory as ModelProto
51+
# onnx_model is an in-memory ModelProto
52+
onnx_model = ...
5153

5254
# Save the ONNX model
5355
onnx.save(onnx_model, 'path/to/the/model.onnx')
5456
```
5557
Runnable IPython notebooks:
56-
- [save_model.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/save_model.ipynb)
58+
- [save_model.ipynb](/onnx/examples/save_model.ipynb)
5759

5860

5961
## Converting and Saving an ONNX Model to External Data
6062
```python
6163
import onnx
6264

63-
onnx_model = ... # Your model in memory as ModelProto
65+
# onnx_model is an in-memory ModelProto
66+
onnx_model = ...
6467
onnx.save_model(onnx_model, 'path/to/save/the/model.onnx', save_as_external_data=True, all_tensors_to_one_file=True, location='filename', size_threshold=1024, convert_attribute=False)
6568
# Then the onnx_model has converted raw data as external data and saved to specific directory
6669
```
@@ -95,7 +98,7 @@ with open('tensor.pb', 'rb') as f:
9598
print('After saving and loading, new TensorProto:\n{}'.format(new_tensor))
9699
```
97100
Runnable IPython notebooks:
98-
- [np_array_tensorproto.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/np_array_tensorproto.ipynb)
101+
- [np_array_tensorproto.ipynb](/onnx/examples/np_array_tensorproto.ipynb)
99102

100103
## Creating an ONNX Model Using Helper Functions
101104
```python
@@ -120,18 +123,18 @@ Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [3, 4])
120123

121124
# Create a node (NodeProto) - This is based on Pad-11
122125
node_def = helper.make_node(
123-
'Pad', # node name
126+
'Pad', # name
124127
['X', 'pads', 'value'], # inputs
125-
['Y'], # outputs
126-
mode='constant', # attributes
128+
['Y'], # outputs
129+
mode='constant', # attributes
127130
)
128131

129132
# Create the graph (GraphProto)
130133
graph_def = helper.make_graph(
131-
[node_def],
132-
'test-model',
133-
[X, pads, value],
134-
[Y],
134+
[node_def], # nodes
135+
'test-model', # name
136+
[X, pads, value], # inputs
137+
[Y], # outputs
135138
)
136139

137140
# Create the model (ModelProto)
@@ -142,8 +145,8 @@ onnx.checker.check_model(model_def)
142145
print('The model is checked!')
143146
```
144147
Runnable IPython notebooks:
145-
- [make_model.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/make_model.ipynb)
146-
- [Protobufs.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/Protobufs.ipynb)
148+
- [make_model.ipynb](/onnx/examples/make_model.ipynb)
149+
- [Protobufs.ipynb](/onnx/examples/Protobufs.ipynb)
147150

148151
## Checking an ONNX Model
149152
```python
@@ -156,11 +159,15 @@ onnx_model = onnx.load(model_path)
156159
print('The model is:\n{}'.format(onnx_model))
157160

158161
# Check the model
159-
onnx.checker.check_model(onnx_model)
160-
print('The model is checked!')
162+
try:
163+
onnx.checker.check_model(onnx_model)
164+
except onnx.checker.ValidationError as e:
165+
print('The model is invalid: %s' % e)
166+
else:
167+
print('The model is valid!')
161168
```
162169
Runnable IPython notebooks:
163-
- [check_model.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/check_model.ipynb)
170+
- [check_model.ipynb](/onnx/examples/check_model.ipynb)
164171

165172
### Checking a Large ONNX Model >2GB
166173
Current checker supports checking models with external data, but for those models larger than 2GB, please use the model path for onnx.checker and the external data needs to be under the same directory.
@@ -204,7 +211,7 @@ onnx.checker.check_model(inferred_model)
204211
print('After shape inference, the shape info of Y is:\n{}'.format(inferred_model.graph.value_info))
205212
```
206213
Runnable IPython notebooks:
207-
- [shape_inference.ipynb](https://github.com/onnx/onnx/tree/master/onnx/examples/shape_inference.ipynb)
214+
- [shape_inference.ipynb](/onnx/examples/shape_inference.ipynb)
208215

209216
### Shape inference a Large ONNX Model >2GB
210217
Current shape_inference supports models with external data, but for those models larger than 2GB, please use the model path for onnx.shape_inference.infer_shapes_path and the external data needs to be under the same directory. You can specify the output path for saving the inferred model; otherwise, the default output path is same as the original model path.

onnx/examples/check_model.ipynb

+7-3
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@
8181
"name": "stdout",
8282
"output_type": "stream",
8383
"text": [
84-
"The model is checked!\n"
84+
"The model is valid!\n"
8585
]
8686
}
8787
],
8888
"source": [
8989
"# Check the model\n",
90-
"onnx.checker.check_model(onnx_model)\n",
91-
"print('The model is checked!')"
90+
"try:\n",
91+
" onnx.checker.check_model(onnx_model)\n",
92+
"except onnx.checker.ValidationError as e:\n",
93+
" print('The model is invalid: %s' % e)\n",
94+
"else:\n",
95+
" print('The model is valid!')"
9296
]
9397
}
9498
],

0 commit comments

Comments
 (0)