1
1
"""
2
- Exporting a Model from PyTorch to ONNX and Running it using ONNXRuntime
3
- =======================================================================
2
+ Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime
3
+ ========================================================================
4
4
5
5
In this tutorial, we describe how to convert a model defined
6
- in PyTorch into the ONNX format and then run it with ONNXRuntime.
6
+ in PyTorch into the ONNX format and then run it with ONNX Runtime.
7
7
8
- ONNXRuntime is a performance-focused engine for ONNX models,
8
+ ONNX Runtime is a performance-focused engine for ONNX models,
9
9
which inferences efficiently across multiple platforms and hardware
10
10
(Windows, Linux, and Mac and on both CPUs and GPUs).
11
- ONNXRuntime has proved to considerably increase performance over
11
+ ONNX Runtime has proved to considerably increase performance over
12
12
multiple models as explained `here
13
13
<https://cloudblogs.microsoft.com/opensource/2019/05/22/onnx-runtime-machine-learning-inferencing-0-4-release>`__
14
14
15
- For this tutorial, you will need to install `onnx <https://github.com/onnx/onnx>`__
16
- and `onnxruntime <https://github.com/microsoft/onnxruntime>`__.
17
- You can get binary builds of onnx and onnxrunimte with
15
+ For this tutorial, you will need to install `ONNX <https://github.com/onnx/onnx>`__
16
+ and `ONNX Runtime <https://github.com/microsoft/onnxruntime>`__.
17
+ You can get binary builds of ONNX and ONNX Runtime with
18
18
``pip install onnx onnxruntime``.
19
- Note that ONNXRuntime is compatible with Python versions 3.5 to 3.7.
19
+ Note that ONNX Runtime is compatible with Python versions 3.5 to 3.7.
20
20
21
21
``NOTE``: This tutorial needs PyTorch master branch which can be installed by following
22
22
the instructions `here <https://github.com/pytorch/pytorch#from-source>`__
@@ -141,7 +141,7 @@ def _initialize_weights(self):
141
141
x , # model input (or a tuple for multiple inputs)
142
142
"super_resolution.onnx" , # where to save the model (can be a file or file-like object)
143
143
export_params = True , # store the trained parameter weights inside the model file
144
- opset_version = 10 , # the onnx version to export the model to
144
+ opset_version = 10 , # the ONNX version to export the model to
145
145
do_constant_folding = True , # wether to execute constant folding for optimization
146
146
input_names = ['input' ], # the model's input names
147
147
output_names = ['output' ], # the model's output names
@@ -151,10 +151,10 @@ def _initialize_weights(self):
151
151
######################################################################
152
152
# We also computed ``torch_out``, the output after of the model,
153
153
# which we will use to verify that the model we exported computes
154
- # the same values when run in onnxruntime .
154
+ # the same values when run in ONNX Runtime .
155
155
#
156
- # But before verifying the model's output with onnxruntime , we will check
157
- # the onnx model with onnx 's API.
156
+ # But before verifying the model's output with ONNX Runtime , we will check
157
+ # the ONNX model with ONNX 's API.
158
158
# First, ``onnx.load("super_resolution.onnx")`` will load the saved model and
159
159
# will output a onnx.ModelProto structure (a top-level file/container format for bundling a ML model.
160
160
# For more information `onnx.proto documentation <https://github.com/onnx/onnx/blob/master/onnx/onnx.proto>`__.).
@@ -172,18 +172,18 @@ def _initialize_weights(self):
172
172
173
173
174
174
######################################################################
175
- # Now let's compute the output using ONNXRuntime 's Python APIs.
175
+ # Now let's compute the output using ONNX Runtime 's Python APIs.
176
176
# This part can normally be done in a separate process or on another
177
177
# machine, but we will continue in the same process so that we can
178
- # verify that onnxruntime and PyTorch are computing the same value
178
+ # verify that ONNX Runtime and PyTorch are computing the same value
179
179
# for the network.
180
180
#
181
- # In order to run the model with ONNXRuntime , we need to create an
181
+ # In order to run the model with ONNX Runtime , we need to create an
182
182
# inference session for the model with the chosen configuration
183
183
# parameters (here we use the default config).
184
184
# Once the session is created, we evaluate the model using the run() api.
185
185
# The output of this call is a list containing the outputs of the model
186
- # computed by ONNXRuntime.
186
+ # computed by ONNX Runtime.
187
187
#
188
188
189
189
import onnxruntime
@@ -193,33 +193,33 @@ def _initialize_weights(self):
193
193
def to_numpy (tensor ):
194
194
return tensor .detach ().cpu ().numpy () if tensor .requires_grad else tensor .cpu ().numpy ()
195
195
196
- # compute onnxruntime output prediction
196
+ # compute ONNX Runtime output prediction
197
197
ort_inputs = {ort_session .get_inputs ()[0 ].name : to_numpy (x )}
198
198
ort_outs = ort_session .run (None , ort_inputs )
199
199
200
- # compare onnxruntime and PyTorch results
200
+ # compare ONNX Runtime and PyTorch results
201
201
np .testing .assert_allclose (to_numpy (torch_out ), ort_outs [0 ], rtol = 1e-03 , atol = 1e-05 )
202
202
203
203
print ("Exported model has been tested with ONNXRuntime, and the result looks good!" )
204
204
205
205
206
206
######################################################################
207
- # We should see that the output of PyTorch and onnxruntime runs match
207
+ # We should see that the output of PyTorch and ONNX Runtime runs match
208
208
# numerically with the given precision (rtol=1e-03 and atol=1e-05).
209
209
# As a side-note, if they do not match then there is an issue in the
210
- # onnx exporter, so please contact us in that case.
210
+ # ONNX exporter, so please contact us in that case.
211
211
#
212
212
213
213
214
214
######################################################################
215
- # Running the model on an image using ONNXRuntime
216
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215
+ # Running the model on an image using ONNX Runtime
216
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217
217
#
218
218
219
219
220
220
######################################################################
221
221
# So far we have exported a model from PyTorch and shown how to load it
222
- # and run it in onnxruntime with a dummy tensor as an input.
222
+ # and run it in ONNX Runtime with a dummy tensor as an input.
223
223
224
224
######################################################################
225
225
# For this tutorial, we will use a famous cat image used widely which
@@ -263,7 +263,7 @@ def to_numpy(tensor):
263
263
######################################################################
264
264
# Now, as a next step, let's take the tensor representing the
265
265
# greyscale resized cat image and run the super-resolution model in
266
- # ONNXRuntime as explained previously.
266
+ # ONNX Runtime as explained previously.
267
267
#
268
268
269
269
ort_inputs = {ort_session .get_inputs ()[0 ].name : to_numpy (img_y )}
@@ -299,14 +299,14 @@ def to_numpy(tensor):
299
299
# :alt: output\_cat
300
300
#
301
301
#
302
- # ONNXRuntime being a cross platform engine, you can run it across
302
+ # ONNX Runtime being a cross platform engine, you can run it across
303
303
# multiple platforms and on both CPUs and GPUs.
304
304
#
305
- # ONNXRuntime can also be deployed to the cloud for model inferencing
305
+ # ONNX Runtime can also be deployed to the cloud for model inferencing
306
306
# using Azure Machine Learning Services. More information `here <https://docs.microsoft.com/en-us/azure/machine-learning/service/concept-onnx>`__.
307
307
#
308
- # More information about ONNXRuntime 's performance `here <https://github.com/microsoft/onnxruntime#high-performance>`__.
308
+ # More information about ONNX Runtime 's performance `here <https://github.com/microsoft/onnxruntime#high-performance>`__.
309
309
#
310
310
#
311
- # For more information about ONNXRuntime `here <https://github.com/microsoft/onnxruntime>`__.
311
+ # For more information about ONNX Runtime `here <https://github.com/microsoft/onnxruntime>`__.
312
312
#
0 commit comments