Skip to content

Commit 167b383

Browse files
authored
beginner_source/onnx/export_simple_model_to_onnx_tutorial.py ๋ฒˆ์—ญ (#1050)
* export simpl model to onnx translation * line 28-29, 47 updated * tensor, source of truth fixed
1 parent b362eb4 commit 167b383

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

โ€Žbeginner_source/onnx/export_simple_model_to_onnx_tutorial.pyโ€Ž

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
# -*- coding: utf-8 -*-
22
"""
33
`Introduction to ONNX <intro_onnx.html>`_ ||
4-
**Exporting a PyTorch model to ONNX** ||
4+
**PyTorch ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ** ||
55
`Extending the ONNX exporter operator support <onnx_registry_tutorial.html>`_ ||
66
`Export a model with control flow to ONNX <export_control_flow_model_to_onnx_tutorial.html>`_
77
8-
Export a PyTorch model to ONNX
8+
PyTorch ๋ชจ๋ธ์„ ONNX๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ
99
==============================
1010
11-
**Author**: `Ti-Tai Wang <https://github.com/titaiwangms>`_, `Justin Chu <justinchu@microsoft.com>`_, `Thiago Crepaldi <https://github.com/thiagocrepaldi>`_.
11+
**์ €์ž**: `Ti-Tai Wang <https://github.com/titaiwangms>`_, `Justin Chu <justinchu@microsoft.com>`_, `Thiago Crepaldi <https://github.com/thiagocrepaldi>`_.
1212
13-
.. note::
14-
Starting with PyTorch 2.5, there are two ONNX Exporter options available.
15-
* ``torch.onnx.export(..., dynamo=True)`` is the recommended exporter that leverages ``torch.export`` and Torch FX for graph capture.
16-
* ``torch.onnx.export`` is the legacy approach that relies on the deprecated TorchScript and is no longer recommended for use.
13+
**๋ฒˆ์—ญ**: `์ด์ค€ํ˜ <https://github.com/titaiwangms>`_.
1714
15+
.. note::
16+
PyTorch 2.5๋ถ€ํ„ฐ ๋‘ ๊ฐ€์ง€ ONNX ์ต์Šคํฌํ„ฐ(Exporter) ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
17+
* ``torch.onnx.export(..., dynamo=True)`` ๋Š” ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ์— ``torch.export`` ์™€ Torch FX๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ถŒ์žฅ ์ต์Šคํฌํ„ฐ์ž…๋‹ˆ๋‹ค.
18+
* ``torch.onnx.export`` ๋Š” TorchScript์— ์˜์กดํ•˜๋Š” ๋ ˆ๊ฑฐ์‹œ ๋ฐฉ์‹์ด๋ฉฐ ๋” ์ด์ƒ ์‚ฌ์šฉ์ด ๊ถŒ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
19+
1820
"""
1921

2022
###############################################################################
21-
# In the `60 Minute Blitz <https://tutorials.pytorch.kr/beginner/deep_learning_60min_blitz.html>`_,
22-
# we had the opportunity to learn about PyTorch at a high level and train a small neural network to classify images.
23-
# In this tutorial, we are going to expand this to describe how to convert a model defined in PyTorch into the
24-
# ONNX format using the ``torch.onnx.export(..., dynamo=True)`` ONNX exporter.
23+
# `PyTorch๋กœ ๋”ฅ๋Ÿฌ๋‹ํ•˜๊ธฐ: 60๋ถ„๋งŒ์— ๋์žฅ๋‚ด๊ธฐ <https://tutorials.pytorch.kr/beginner/deep_learning_60min_blitz.html>`_ ์—์„œ๋Š”
24+
# PyTorch๋ฅผ ๊ณ ์ˆ˜์ค€์—์„œ ๋ฐฐ์šฐ๊ณ  ์ž‘์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œ์ผœ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
25+
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๊ทธ ๋‚ด์šฉ์˜ ํ™•์žฅ์œผ๋กœ ``torch.onnx.export(..., dynamo=True)`` ONNX ์ต์Šคํฌํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ
26+
# PyTorch์—์„œ ์ •์˜๋œ ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
2527
#
26-
# While PyTorch is great for iterating on the development of models, the model can be deployed to production
27-
# using different formats, including `ONNX <https://onnx.ai/>`_ (Open Neural Network Exchange)!
28+
# ๋ชจ๋ธ ๊ฐœ๋ฐœ๊ณผ ์‹คํ—˜์—๋Š” PyTorch๊ฐ€ ๋งค์šฐ ์œ ์šฉํ•˜๊ณ , PyTorch๋กœ ์™„์„ฑ๋œ ๋ชจ๋ธ์€ `ONNX <https://onnx.ai/>`_ (Open Neural Network Exchange)
29+
# ๋“ฑ ๋‹ค์–‘ํ•œ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์‹ค์ œ ์„œ๋น„์Šค ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
2830
#
29-
# ONNX is a flexible open standard format for representing machine learning models which standardized representations
30-
# of machine learning allow them to be executed across a gamut of hardware platforms and runtime environments
31-
# from large-scale cloud-based supercomputers to resource-constrained edge devices, such as your web browser and phone.
31+
# ONNX๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์œ ์—ฐํ•œ ๊ณต๊ฐœ ํ‘œ์ค€ ํ˜•์‹์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ‘œ์ค€ํ™”๋œ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์€
32+
# ๋Œ€๊ทœ๋ชจ ํด๋ผ์šฐ๋“œ ๊ธฐ๋ฐ˜ ์Šˆํผ์ปดํ“จํ„ฐ๋ถ€ํ„ฐ, ์›น ๋ธŒ๋ผ์šฐ์ €๋‚˜ ํœด๋Œ€ํฐ์ฒ˜๋Ÿผ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ œํ•œ๋œ ์—ฃ์ง€ ๋””๋ฐ”์ด์Šค๊นŒ์ง€
33+
# ๋‹ค์–‘ํ•œ ํ•˜๋“œ์›จ์–ด ํ”Œ๋žซํผ ๋ฐ ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
3234
#
33-
# In this tutorial, weโ€™ll learn how to:
35+
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋‹ค์Œ ํ•ญ๋ชฉ๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
3436
#
35-
# 1. Install the required dependencies.
36-
# 2. Author a simple image classifier model.
37-
# 3. Export the model to ONNX format.
38-
# 4. Save the ONNX model in a file.
39-
# 5. Visualize the ONNX model graph using `Netron <https://github.com/lutzroeder/netron>`_.
40-
# 6. Execute the ONNX model with `ONNX Runtime`
41-
# 7. Compare the PyTorch results with the ones from the ONNX Runtime.
37+
# 1. ํ•„์š”ํ•œ ์˜์กด์„ฑ ์„ค์น˜ํ•˜๊ธฐ.
38+
# 2. ๊ฐ„๋‹จํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ ์ž‘์„ฑํ•˜๊ธฐ.
39+
# 3. ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ.
40+
# 4. ONNX ๋ชจ๋ธ์„ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ธฐ.
41+
# 5. `Netron <https://github.com/lutzroeder/netron>`_ ์„ ์‚ฌ์šฉํ•ด ONNX ๋ชจ๋ธ ๊ทธ๋ž˜ํ”„ ์‹œ๊ฐํ™”ํ•˜๊ธฐ.
42+
# 6. `ONNX Runtime` ์œผ๋กœ ONNX ๋ชจ๋ธ ์‹คํ–‰ํ•˜๊ธฐ.
43+
# 7. PyTorch์˜ ๊ฒฐ๊ณผ์™€ ONNX Runtime์˜ ๊ฒฐ๊ณผ ๋น„๊ตํ•˜๊ธฐ.
4244
#
43-
# 1. Install the required dependencies
45+
# 1. ํ•„์š”ํ•œ ์˜์กด์„ฑ ์„ค์น˜ํ•˜๊ธฐ
4446
# ------------------------------------
45-
# Because the ONNX exporter uses ``onnx`` and ``onnxscript`` to translate PyTorch operators into ONNX operators,
46-
# we will need to install them.
47+
# ONNX ์ต์Šคํฌํ„ฐ๋Š” PyTorch ์—ฐ์‚ฐ์ž๋ฅผ ONNX ์—ฐ์‚ฐ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ``onnx`` ์™€ ``onnxscript`` ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ
48+
# ์ด์— ๋Œ€ํ•œ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
4749
#
4850
# .. code-block:: bash
4951
#
5052
# pip install --upgrade onnx onnxscript
5153
#
52-
# 2. Author a simple image classifier model
54+
# 2. ๊ฐ„๋‹จํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ ์ž‘์„ฑํ•˜๊ธฐ
5355
# -----------------------------------------
5456
#
55-
# Once your environment is set up, letโ€™s start modeling our image classifier with PyTorch,
56-
# exactly like we did in the `60 Minute Blitz <https://tutorials.pytorch.kr/beginner/deep_learning_60min_blitz.html>`_.
57+
# ํ™˜๊ฒฝ ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด `PyTorch๋กœ ๋”ฅ๋Ÿฌ๋‹ํ•˜๊ธฐ: 60๋ถ„๋งŒ์— ๋์žฅ๋‚ด๊ธฐ <https://tutorials.pytorch.kr/beginner/deep_learning_60min_blitz.html>`_ ์—์„œ
58+
# ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ PyTorch๋กœ ๊ฐ„๋‹จํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
5759
#
5860

5961
import torch
@@ -81,78 +83,78 @@ def forward(self, x: torch.Tensor):
8183

8284

8385
######################################################################
84-
# 3. Export the model to ONNX format
86+
# 3. ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ
8587
# ----------------------------------
8688
#
87-
# Now that we have our model defined, we need to instantiate it and create a random 32x32 input.
88-
# Next, we can export the model to ONNX format.
89+
# ๋ชจ๋ธ์ด ์ •์˜๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ด์ œ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์ž„์˜์˜ 32x32 ์ž…๋ ฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
90+
# ์ดํ›„ ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค.
8991

9092
torch_model = ImageClassifierModel()
91-
# Create example inputs for exporting the model. The inputs should be a tuple of tensors.
93+
# ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„ํ•œ ์˜ˆ์‹œ ์ž…๋ ฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ž…๋ ฅ์€ tensor์˜ ํŠœํ”Œ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
9294
example_inputs = (torch.randn(1, 1, 32, 32),)
9395
onnx_program = torch.onnx.export(torch_model, example_inputs, dynamo=True)
9496

9597
######################################################################
96-
# As we can see, we didn't need any code change to the model.
97-
# The resulting ONNX model is stored within ``torch.onnx.ONNXProgram`` as a binary protobuf file.
98+
# ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ๊ณผ์ •์—์„œ๋Š” ๋ชจ๋ธ์— ๋Œ€ํ•œ ์–ด๋– ํ•œ ์ฝ”๋“œ ๋ณ€๊ฒฝ๋„ ํ•„์š”ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
99+
# ๊ฒฐ๊ณผ๋กœ ๋„์ถœ๋œ ONNX ๋ชจ๋ธ์€ ``torch.onnx.ONNXProgram`` ๋‚ด์— ์ด์ง„ protobuf ํŒŒ์ผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
98100
#
99-
# 4. Save the ONNX model in a file
101+
# 4. ONNX ๋ชจ๋ธ์„ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ธฐ
100102
# --------------------------------
101103
#
102-
# Although having the exported model loaded in memory is useful in many applications,
103-
# we can save it to disk with the following code:
104+
# ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋‘๊ณ  ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ,
105+
# ๋‹ค์Œ ์ฝ”๋“œ๋กœ ๋ชจ๋ธ์„ ๋””์Šคํฌ์— ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
104106

105107
onnx_program.save("image_classifier_model.onnx")
106108

107109
######################################################################
108-
# You can load the ONNX file back into memory and check if it is well formed with the following code:
110+
# ๋‹ค์Œ ์ฝ”๋“œ๋กœ ONNX ํŒŒ์ผ์„ ๋‹ค์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๊ณ  ๊ทธ ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
109111

110112
import onnx
111113

112114
onnx_model = onnx.load("image_classifier_model.onnx")
113115
onnx.checker.check_model(onnx_model)
114116

115117
######################################################################
116-
# 5. Visualize the ONNX model graph using Netron
118+
# 5. Netron์„ ์‚ฌ์šฉํ•ด ONNX ๋ชจ๋ธ ๊ทธ๋ž˜ํ”„ ์‹œ๊ฐํ™”ํ•˜๊ธฐ
117119
# ----------------------------------------------
118120
#
119-
# Now that we have our model saved in a file, we can visualize it with `Netron <https://github.com/lutzroeder/netron>`_.
120-
# Netron can either be installed on macos, Linux or Windows computers, or run directly from the browser.
121-
# Let's try the web version by opening the following link: https://netron.app/.
121+
# ๋ชจ๋ธ์ด ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฉด `Netron <https://github.com/lutzroeder/netron>`_ ์œผ๋กœ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
122+
# Netron์€ macos, Linux ๋˜๋Š” Windows ์ปดํ“จํ„ฐ์— ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง์ ‘ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
123+
# ์ด `๋งํฌ <https://github.com/lutzroeder/netron>`_ ๋กœ ์›น ๋ธŒ๋ผ์šฐ์ € ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
122124
#
123125
# .. image:: ../../_static/img/onnx/netron_web_ui.png
124126
# :width: 70%
125127
# :align: center
126128
#
127129
#
128-
# Once Netron is open, we can drag and drop our ``image_classifier_model.onnx`` file into the browser or select it after
129-
# clicking the **Open model** button.
130+
# Netron์ด ์—ด๋ฆฌ๋ฉด ``image_classifier_model.onnx`` ํŒŒ์ผ์„ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋“œ๋ž˜๊ทธ ์•ค๋“œ ๋“œ๋กญ(drag and drop)ํ•˜๊ฑฐ๋‚˜
131+
# **Open model** ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ํ›„ ํŒŒ์ผ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
130132
#
131133
# .. image:: ../../_static/img/onnx/image_classifier_onnx_model_on_netron_web_ui.png
132134
# :width: 50%
133135
#
134136
#
135-
# And that is it! We have successfully exported our PyTorch model to ONNX format and visualized it with Netron.
137+
# ์ด์ œ ๋์Šต๋‹ˆ๋‹ค! ์„ฑ๊ณต์ ์œผ๋กœ PyTorch ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ณ  Netron์œผ๋กœ ์‹œ๊ฐํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค.
136138
#
137-
# 6. Execute the ONNX model with ONNX Runtime
139+
# 6. ONNX Runtime์œผ๋กœ ONNX ๋ชจ๋ธ ์‹คํ–‰ํ•˜๊ธฐ
138140
# -------------------------------------------
139141
#
140-
# The last step is executing the ONNX model with `ONNX Runtime`, but before we do that, let's install ONNX Runtime.
142+
# ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” `ONNX Runtime` ์œผ๋กœ ONNX ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ „์— ๋จผ์ € ONNX Runtime์„ ์„ค์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
141143
#
142144
# .. code-block:: bash
143145
#
144146
# pip install onnxruntime
145147
#
146-
# The ONNX standard does not support all the data structure and types that PyTorch does,
147-
# so we need to adapt PyTorch input's to ONNX format before feeding it to ONNX Runtime.
148-
# In our example, the input happens to be the same, but it might have more inputs
149-
# than the original PyTorch model in more complex models.
148+
# ONNX ํ‘œ์ค€์€ PyTorch๊ฐ€ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ํƒ€์ž…์„ ์ง€์›ํ•˜์ง€๋Š” ์•Š์œผ๋ฏ€๋กœ,
149+
# ONNX Runtime์— ๋„ฃ๊ธฐ ์ „์— ์šฐ์„  PyTorch ์ž…๋ ฅ์„ ONNX ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
150+
# ์ด ์˜ˆ์ œ์—์„œ๋Š” ์ž…๋ ฅ์ด ๋™์ผํ•˜์ง€๋งŒ, ๋” ๋ณต์žกํ•œ ๋ชจ๋ธ์—์„œ๋Š”
151+
# ๊ธฐ์กด PyTorch ๋ชจ๋ธ๋ณด๋‹ค ๋” ๋งŽ์€ ์ž…๋ ฅ์œผ๋กœ ๋‚˜๋ˆ„์–ด์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
150152
#
151-
# ONNX Runtime requires an additional step that involves converting all PyTorch tensors to Numpy (in CPU)
152-
# and wrap them on a dictionary with keys being a string with the input name as key and the numpy tensor as the value.
153+
# ONNX Runtime์€ ๋ชจ๋“  PyTorch tensor๋ฅผ (CPU์˜) Numpy tensor๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค,
154+
# ์ž…๋ ฅ ์ด๋ฆ„ ๋ฌธ์ž์—ด์„ ํ‚ค๋กœ, Numpy tensor๋ฅผ ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๊ฐ์‹ธ๋Š” ์ถ”๊ฐ€ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
153155
#
154-
# Now we can create an *ONNX Runtime Inference Session*, execute the ONNX model with the processed input
155-
# and get the output. In this tutorial, ONNX Runtime is executed on CPU, but it could be executed on GPU as well.
156+
# ์ด์ œ *ONNX Runtime ์ถ”๋ก  ์„ธ์…˜* ์„ ์ƒ์„ฑํ•˜๊ณ , ์ฒ˜๋ฆฌ๋œ ์ž…๋ ฅ์œผ๋กœ ONNX ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜์—ฌ ์ถœ๋ ฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
157+
# ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ ONNX Runtime์€ CPU์—์„œ ์‹คํ–‰๋˜์ง€๋งŒ, GPU์—์„œ๋„ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
156158

157159
import onnxruntime
158160

@@ -166,18 +168,18 @@ def forward(self, x: torch.Tensor):
166168

167169
onnxruntime_input = {input_arg.name: input_value for input_arg, input_value in zip(ort_session.get_inputs(), onnx_inputs)}
168170

169-
# ONNX Runtime returns a list of outputs
171+
# ONNX Runtime์€ ์ถœ๋ ฅ์— ๋Œ€ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
170172
onnxruntime_outputs = ort_session.run(None, onnxruntime_input)[0]
171173

172174
####################################################################
173-
# 7. Compare the PyTorch results with the ones from the ONNX Runtime
175+
# 7. PyTorch์˜ ๊ฒฐ๊ณผ์™€ ONNX Runtime์˜ ๊ฒฐ๊ณผ ๋น„๊ตํ•˜๊ธฐ
174176
# ------------------------------------------------------------------
175177
#
176-
# The best way to determine whether the exported model is looking good is through numerical evaluation
177-
# against PyTorch, which is our source of truth.
178+
# ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€
179+
# ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ค€(source of truth)์ด ๋˜๋Š” PyTorch์™€ ์ˆ˜์น˜์ ์œผ๋กœ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
178180
#
179-
# For that, we need to execute the PyTorch model with the same input and compare the results with ONNX Runtime's.
180-
# Before comparing the results, we need to convert the PyTorch's output to match ONNX's format.
181+
# ์ด๋ฅผ ์œ„ํ•ด ๋™์ผํ•œ ์ž…๋ ฅ์œผ๋กœ PyTorch ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ONNX Runtime์˜ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
182+
# ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์ „์— PyTorch์˜ ์ถœ๋ ฅ์„ ONNX์˜ ํ˜•์‹๊ณผ ์ผ์น˜ํ•˜๋„๋ก ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
181183

182184
torch_outputs = torch_model(*example_inputs)
183185

@@ -190,20 +192,20 @@ def forward(self, x: torch.Tensor):
190192
print(f"Sample output: {onnxruntime_outputs}")
191193

192194
######################################################################
193-
# Conclusion
195+
# ๊ฒฐ๋ก 
194196
# ----------
195197
#
196-
# That is about it! We have successfully exported our PyTorch model to ONNX format,
197-
# saved the model to disk, viewed it using Netron, executed it with ONNX Runtime
198-
# and finally compared its numerical results with PyTorch's.
198+
# ์ด๊ฒƒ์œผ๋กœ ํŠœํ† ๋ฆฌ์–ผ์„ ๋งˆ์นฉ๋‹ˆ๋‹ค! ์šฐ๋ฆฌ๋Š” ์„ฑ๊ณต์ ์œผ๋กœ PyTorch ๋ชจ๋ธ์„ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ณ ,
199+
# ๋ชจ๋ธ์„ ๋””์Šคํฌ์— ์ €์žฅํ•˜๊ณ , Netron์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐํ™”ํ•˜๊ณ , ONNX Runtime์œผ๋กœ ์‹คํ–‰ํ–ˆ์œผ๋ฉฐ,
200+
# ๋งˆ์ง€๋ง‰์—๋Š” ONNX Runtime์˜ ๊ฒฐ๊ณผ๋ฅผ PyTorch์˜ ๊ฒฐ๊ณผ์™€ ์ˆ˜์น˜์ ์œผ๋กœ ๋น„๊ตํ–ˆ์Šต๋‹ˆ๋‹ค.
199201
#
200-
# Further reading
202+
# ๋” ์ฝ์–ด๋ณด๊ธฐ
201203
# ---------------
202204
#
203-
# The list below refers to tutorials that ranges from basic examples to advanced scenarios,
204-
# not necessarily in the order they are listed.
205-
# Feel free to jump directly to specific topics of your interest or
206-
# sit tight and have fun going through all of them to learn all there is about the ONNX exporter.
205+
# ์•„๋ž˜ ๋ชฉ๋ก์€ ๊ธฐ๋ณธ ์˜ˆ์ œ๋ถ€ํ„ฐ ๊ณ ๊ธ‰ ์‹œ๋‚˜๋ฆฌ์˜ค๊นŒ์ง€ ์•„์šฐ๋ฅด๋Š” ํŠœํ† ๋ฆฌ์–ผ๋“ค๋กœ,
206+
# ๋ฐ˜๋“œ์‹œ ๋‚˜์—ด๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ณผ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.
207+
# ์ž์œ ๋กญ๊ฒŒ ๊ด€์‹ฌ ๊ฐ€๋Š” ์ฃผ์ œ๋กœ ๋ฐ”๋กœ ์ด๋™ํ•˜๊ฑฐ๋‚˜,
208+
# ํŽธ์•ˆํžˆ ์•‰์•„ ์ „์ฒด ํŠœํ† ๋ฆฌ์–ผ์„ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๋ฉฐ ONNX ์ต์Šคํฌํ„ฐ์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ฒƒ๋“ค์„ ๋ฐฐ์›Œ๋ณด์„ธ์š”.
207209
#
208210
# .. include:: /beginner_source/onnx/onnx_toc.txt
209211
#

0 commit comments

Comments
ย (0)