Skip to content

Commit 4ae523d

Browse files
authored
Add page for exporting PyTorch for WinML and modify pytorch version conversion page to be generic version conversion (#144)
* separate opset converter tutorial * Update and rename OpsetConverter.md to VersionConversion.md * Delete ExportModelFromPyTorchToDifferentONNXOpsetVersions.md * Update README.md * Create ExportModelFromPyTorchForWinML.md * Update ExportModelFromPyTorchForWinML.md
1 parent 15222af commit 4ae523d

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ These images are available for convenience to get started with ONNX and tutorial
2828
| [MATLAB](https://www.mathworks.com/) | [Deep Learning Toolbox](https://www.mathworks.com/matlabcentral/fileexchange/67296) | [Example](https://www.mathworks.com/help/deeplearning/ref/exportonnxnetwork.html) |
2929
| [ML.NET](https://github.com/dotnet/machinelearning/) | [built-in](https://www.nuget.org/packages/Microsoft.ML/) | [Example](https://github.com/dotnet/machinelearning/blob/master/test/Microsoft.ML.Tests/OnnxConversionTest.cs) |
3030
| [MXNet (Apache)](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) | [Example](tutorials/MXNetONNXExport.ipynb) |
31-
| [PyTorch](http://pytorch.org/) | [part of pytorch package](http://pytorch.org/docs/master/onnx.html) | [Example1](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html), [Example2](tutorials/PytorchOnnxExport.ipynb), [exporting different ONNX opsets](https://github.com/onnx/tutorials/blob/master/tutorials/ExportModelFromPyTorchToDifferentONNXOpsetVersions.md), [Extending support](tutorials/PytorchAddExportSupport.md) |
31+
| [PyTorch](http://pytorch.org/) | [part of pytorch package](http://pytorch.org/docs/master/onnx.html) | [Example1](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html), [Example2](tutorials/PytorchOnnxExport.ipynb), [export for Windows ML](tutorials/ExportModelFromPyTorchForWinML.md), [Extending support](tutorials/PytorchAddExportSupport.md) |
3232
| [SciKit-Learn](http://scikit-learn.org/) | [onnx/sklearn-onnx](https://github.com/onnx/sklearn-onnx) | [Example](http://onnx.ai/sklearn-onnx/index.html) | n/a |
3333
| [SINGA (Apache)](http://singa.apache.org/) - [Github](https://github.com/apache/incubator-singa/blob/master/python/singa/sonnx.py) (experimental) | [built-in](https://github.com/apache/incubator-singa/blob/master/doc/en/docs/installation.md) | [Example](https://github.com/apache/incubator-singa/tree/master/examples/onnx) |
3434
| [TensorFlow](https://www.tensorflow.org/) | [onnx/tensorflow-onnx](https://github.com/onnx/tensorflow-onnx) | [Examples](https://github.com/onnx/tutorials/blob/master/tutorials/TensorflowToOnnx-1.ipynb) |
@@ -85,6 +85,7 @@ Once you have an ONNX model, it can be scored with a variety of tools.
8585
* [Netron: a viewer for ONNX models](https://github.com/lutzroeder/Netron)
8686
* [Example of operating on ONNX protobuf](https://github.com/onnx/onnx/blob/master/onnx/examples/Protobufs.ipynb)
8787
* [Float16 <-> Float32 converter](https://github.com/onnx/onnx-docker/blob/master/onnx-ecosystem/converter_scripts/float32_float16_onnx.ipynb)
88+
* [Version conversion](tutorials/VersionConversion.md)
8889

8990
## Contributing
9091

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Export PyTorch models for Windows ML
2+
3+
[Windows Machine Learning](https://docs.microsoft.com/windows/ai/windows-ml/) makes it easy to integrate AI into your Windows applications using ONNX models.
4+
5+
## Step 1: Determine the ONNX version your model needs to be in
6+
This depends on which releases of Windows you are targeting. Newer releases of Windows support newer versions of ONNX. This [page](https://docs.microsoft.com/windows/ai/windows-ml/onnx-versions) lists the opset versions supported by different releases of Windows. ONNX 1.2 (opset 7) is the lowest one supported and will work on all versions of Windows ML. Newer versions of ONNX support more types of models.
7+
8+
## Step 2: Export your PyTorch model to that ONNX version
9+
PyTorch's ONNX export support is documented [here](https://pytorch.org/docs/stable/onnx.html). As of PyTorch 1.2, the `torch.onnx.export` function takes a parameter that lets you specify the ONNX opset version.
10+
11+
```python
12+
import torch
13+
import torchvision
14+
15+
dummy_in = torch.randn(10, 3, 224, 224)
16+
model = torchvision.models.resnet18(pretrained=True)
17+
18+
in_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
19+
out_names = [ "output1" ]
20+
21+
torch.onnx.export(model, dummy_in, "resnet18.onnx", input_names=in_names, output_names=out_names, opset_version=7, verbose=True)
22+
```
23+
24+
## Step 3: Integrate the ONNX model into your Windows app
25+
Follow the [tutorials and documentation](https://docs.microsoft.com/windows/ai/windows-ml/) to start using the model in your application. You can code directly against the [Windows ML APIs](https://docs.microsoft.com/windows/ai/windows-ml/integrate-model) or use the [mlgen tool](https://docs.microsoft.com/windows/ai/windows-ml/mlgen) to automatically generate wrapper classes for you.

tutorials/ExportModelFromPyTorchToDifferentONNXOpsetVersions.md renamed to tutorials/VersionConversion.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
### Export model from PyTorch to ONNX:
1+
## Version Conversion
22

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
3+
The ONNX [Version Converter](https://github.com/onnx/onnx/blob/master/docs/VersionConverter.md) helps convert ONNX models to the version needed by the runtime you are using.
244

255
Version Conversion for BatchNormalization from opset 8 to 9:
266

@@ -75,4 +55,4 @@ converted_model79 = version_converter.convert_version(converted_model7, 9)
7555

7656
# Save model
7757
onnx.save(converted_model79, "path_to/resnet18_v79.onnx")
78-
```
58+
```

0 commit comments

Comments
 (0)