Skip to content

Commit 4c468ea

Browse files
KsenijaSbddppq
authored andcommitted
Add documentation for exporting model from PyTorch to different ONNX opset versions (#120)
* Add Doc For Export from PyTorch To ONNX * Add batchnorm img * Rename md file * Update README file * Update README file * Update README file
1 parent 579c12f commit 4c468ea

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
| [Menoh](https://github.com/pfnet-research/menoh) | [pfnet-research/menoh](https://github.com/pfnet-research/menoh) | n/a | [Importing](tutorials/OnnxMenohHaskellImport.ipynb) |
2222
| [ML.NET](https://github.com/dotnet/machinelearning/) | [built-in](https://www.nuget.org/packages/Microsoft.ML/) | [Exporting](https://github.com/dotnet/machinelearning/blob/master/test/Microsoft.ML.Tests/OnnxConversionTest.cs) | [Importing](https://github.com/dotnet/machinelearning/blob/master/test/Microsoft.ML.OnnxTransformerTest/OnnxTransformTests.cs) |
2323
| [Apache MXNet](http://mxnet.incubator.apache.org/) | part of mxnet package [docs](http://mxnet.incubator.apache.org/api/python/contrib/onnx.html) [github](https://github.com/apache/incubator-mxnet/tree/master/python/mxnet/contrib/onnx) | [Exporting](tutorials/MXNetONNXExport.ipynb) | [Importing](tutorials/OnnxMxnetImport.ipynb) |
24-
| [PyTorch](http://pytorch.org/) | [part of pytorch package](http://pytorch.org/docs/master/onnx.html) | [Exporting](tutorials/PytorchOnnxExport.ipynb), [Extending support](tutorials/PytorchAddExportSupport.md) | coming soon |
24+
| [PyTorch](http://pytorch.org/) | [part of pytorch package](http://pytorch.org/docs/master/onnx.html) | [Exporting](tutorials/PytorchOnnxExport.ipynb), [Exporting - Different ONNX Opsets](tutorials/ExportModelFromPyTorchToDifferentONNXOpsetVersions.md), [Extending support](tutorials/PytorchAddExportSupport.md) | coming soon |
2525
| [SciKit-Learn](http://scikit-learn.org/) | [onnx/sklearn-onnx](https://github.com/onnx/sklearn-onnx) | [Exporting](http://onnx.ai/sklearn-onnx/index.html) | n/a |
2626
| [TensorFlow](https://www.tensorflow.org/) | [onnx/onnx-tensorflow](https://github.com/onnx/onnx-tensorflow) and [onnx/tensorflow-onnx](https://github.com/onnx/tensorflow-onnx) | [Exporting - ONNX-Tensorflow](tutorials/OnnxTensorflowExport.ipynb)<br>[Exporting - Tensorflow-ONNX](https://github.com/onnx/tensorflow-onnx/blob/master/examples/call_coverter_via_python.py) | [Importing](tutorials/OnnxTensorflowImport.ipynb) [experimental] |
2727
| [TensorRT](https://developer.nvidia.com/tensorrt) | [onnx/onnx-tensorrt](https://github.com/onnx/onnx-tensorrt) | n/a | [Importing](https://github.com/onnx/onnx-tensorrt/blob/master/README.md) |
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
### Export model from PyTorch to ONNX:
2+
3+
Default version for ONNX model is 9 when it is exported from PyTorch.
4+
5+
```python
6+
import torch
7+
import torchvision
8+
9+
# No CUDA
10+
dummy_input = torch.randn(10, 3, 224, 224)
11+
model = torchvision.models.resnet18(pretrained=True)
12+
13+
# CUDA
14+
dummy_input = torch.randn(10, 3, 224, 224, device='cuda')
15+
model = torchvision.models.alexnet(pretrained=True).cuda()
16+
17+
input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
18+
output_names = [ "output1" ]
19+
20+
torch.onnx.export(model, dummy_input, "resnet18.onnx", verbose=True, input_names=input_names, output_names=output_names)
21+
```
22+
23+
### Version Conversion
24+
25+
Version Conversion for BatchNormalization from opset 8 to 9:
26+
27+
<img src="assets/batchnorm.png" />
28+
29+
30+
### Downgrade Version Conversion from 9 to 8:
31+
32+
```python
33+
import onnx
34+
35+
# Load the model
36+
model = onnx.load("path_to/resnet18.onnx")
37+
38+
# Check that the IR is well formed
39+
onnx.checker.check_model(model)
40+
41+
from onnx import version_converter
42+
43+
# Convert to version 8
44+
converted_model = version_converter.convert_version(onnx_model, 8)
45+
46+
# Save model
47+
onnx.save(converted_model, "path_to/resnet18_v8.onnx")
48+
```
49+
50+
### Upgrade Version Conversion from 8 to 9
51+
52+
```python
53+
# Convert to version 9
54+
converted_model9 = version_converter.convert_version(converted_model, 9)
55+
56+
# Save model
57+
onnx.save(converted_model9, "path_to/resnet18_v9.onnx")
58+
```
59+
60+
### Downgrade Version Conversion from 8 to 7
61+
62+
```python
63+
# Convert to version 7
64+
converted_model7 = version_converter.convert_version(converted_model, 7)
65+
66+
# Save model
67+
onnx.save(converted_model7, "path_to/resnet18_v7.onnx")
68+
```
69+
70+
### Upgrade Version Conversion from 7 to 9
71+
72+
```python
73+
# Convert to version 9
74+
converted_model79 = version_converter.convert_version(converted_model7, 9)
75+
76+
# Save model
77+
onnx.save(converted_model79, "path_to/resnet18_v79.onnx")
78+
```

tutorials/assets/batchnorm.png

73.4 KB
Loading

0 commit comments

Comments
 (0)