Skip to content

Commit

Permalink
Rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
9bow committed Nov 23, 2022
1 parent 425c3e9 commit bb04c46
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
1) `torch.save <https://pytorch.org/docs/stable/torch.html?highlight=save#torch.save>`__:
์ง๋ ฌํ™”๋œ ๊ฐ์ฒด๋ฅผ ๋””์Šคํฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” Python์˜
`pickle <https://docs.python.org/3/library/pickle.html>`__ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค
`pickle <https://docs.python.org/3/library/pickle.html>`__ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค.
์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๊ฐ์ฒด์˜ ๋ชจ๋ธ, Tensor ๋ฐ ์‚ฌ์ „์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
2) `torch.load <https://pytorch.org/docs/stable/torch.html?highlight=torch%20load#torch.load>`__:
`pickle <https://docs.python.org/3/library/pickle.html>`__\ ์„ ์‚ฌ์šฉํ•˜์—ฌ
์ €์žฅ๋œ ๊ฐ์ฒด ํŒŒ์ผ๋“ค์„ ์—ญ์ง๋ ฌํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์žฅ์น˜์—
๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
(`์žฅ์น˜๊ฐ„ ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ <#device>`__ ์ฐธ๊ณ )
์ €์žฅ๋œ ๊ฐ์ฒด ํŒŒ์ผ๋“ค์„ ์—ญ์ง๋ ฌํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์žฅ์น˜์— ๋ถˆ๋Ÿฌ์˜ฌ
๋•Œ์—๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
(`์žฅ์น˜ ๊ฐ„ ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ <#device>`__ ์ฐธ๊ณ )
3) `torch.nn.Module.load_state_dict <https://pytorch.org/docs/stable/generated/torch.nn.Module.html?highlight=load_state_dict#torch.nn.Module.load_state_dict>`__:
์—ญ์ง๋ ฌํ™”๋œ *state_dict* ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
Expand Down Expand Up @@ -145,29 +145,29 @@
# model.eval()
#
# .. note::
# PyTorch ๋ฒ„์ „ 1.6์—์„œ๋Š” ์ƒˆ๋กœ์šด ZipํŒŒ์ผ-๊ธฐ๋ฐ˜์˜ ํŒŒ์ผ ํฌ๋งท์„ ์‚ฌ์šฉํ•˜๋„๋ก
# ``torch.save`` ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ``torch.load`` ๋Š” ์˜ˆ์ „ ๋ฐฉ์‹์˜ ํŒŒ์ผ๋“ค์„
# PyTorch ๋ฒ„์ „ 1.6์—์„œ๋Š” ``torch.save`` ๊ฐ€ ์ƒˆ๋กœ์šด ZipํŒŒ์ผ-๊ธฐ๋ฐ˜์˜ ํŒŒ์ผ
# ํฌ๋งท์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ``torch.load`` ๋Š” ์˜ˆ์ „ ๋ฐฉ์‹์˜ ํŒŒ์ผ๋“ค์„
# ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ด์œ ์—์„œ๋“  ``torch.save`` ๊ฐ€ ์˜ˆ์ „
# ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ``_use_new_zipfile_serialization=False`` ์„
# kwarg๋กœ ์ „๋‹ฌํ•˜์„ธ์š”.
#
# ์ถ”๋ก ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ๋Š” ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ํ•™์Šต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
# ์ถ”๋ก ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ๋Š” ๊ทธ ๋ชจ๋ธ์˜ ํ•™์Šต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
# ``torch.save()`` ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ *state_dict* ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์ค‘์— ๋ชจ๋ธ์„
# ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€์žฅ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”, ๋ชจ๋ธ ์ €์žฅ ์‹œ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
#
# PyTorch์—์„œ๋Š” ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ ``.pt`` ๋˜๋Š” ``.pth`` ํ™•์žฅ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด
# ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์ž…๋‹ˆ๋‹ค.
#
# ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์—๋Š” ๋ฐ˜๋“œ์‹œ ``model.eval()`` ์„ ํ˜ธ์ถœํ•˜์—ฌ ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ผ๊ด€์„ฑ
# ์—†๊ฒŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
# ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ``model.eval()`` ์„ ํ˜ธ์ถœํ•˜์—ฌ ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์œผ๋ฉด ์ผ๊ด€์„ฑ ์—†๋Š”
# ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
#
# .. Note ::
#
# ``load_state_dict()`` ํ•จ์ˆ˜์—๋Š” ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹Œ, ์‚ฌ์ „ ๊ฐ์ฒด๋ฅผ
# ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์„ธ์š”. ๋”ฐ๋ผ์„œ ์ €์žฅ๋œ *state_dict* ๋ฅผ ``load_state_dict()``
# ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,
# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ๋Š” ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
#
# .. Note ::
#
Expand All @@ -192,7 +192,7 @@
#
# .. code:: python
#
# # ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” ์–ด๋”˜๊ฐ€์— ๋ฐ˜๋“œ์‹œ ์„ ์–ธ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
# # ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” ์–ด๋”˜๊ฐ€์— ๋ฐ˜๋“œ์‹œ ์„ ์–ธ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
# model = torch.load(PATH)
# model.eval()
#
Expand All @@ -213,25 +213,23 @@
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ผ๊ด€์„ฑ
# ์—†๊ฒŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
#
# Export/Load Model in TorchScript Format
# TorchScript ํฌ๋งท์œผ๋กœ ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ/๊ฐ€์ ธ์˜ค๊ธฐ
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# One common way to do inference with a trained model is to use
# `TorchScript <https://pytorch.org/docs/stable/jit.html>`__, an intermediate
# representation of a PyTorch model that can be run in Python as well as in a
# high performance environment like C++. TorchScript is actually the recommended model format
# for scaled inference and deployment.
# ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” `TorchScript <https://pytorch.org/docs/stable/jit.html>`__ ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
# TorchScript๋Š” ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์ด๋‚˜ C++์™€ ๊ฐ™์€ ๊ณ ์„ฑ๋Šฅ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”
# ํŒŒ์ดํ† ์น˜ ๋ชจ๋ธ์˜ ์ค‘๊ฐ„ ํ‘œํ˜„(IR; Intermediate Representation)์ž…๋‹ˆ๋‹ค.
# TorchScript๋Š” ํ™•์žฅ๋œ ์ถ”๋ก  ๋ฐ ๋ฐฐํฌ์— ๊ถŒ์žฅ๋˜๋Š” ๋ชจ๋ธ ํ˜•์‹์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.
#
# .. note::
# Using the TorchScript format, you will be able to load the exported model and
# run inference without defining the model class.
# TorchScript ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๊ณ ๋„ ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์„ ์ฝ์–ด ์˜ค๊ฑฐ๋‚˜ ์ถ”๋ก ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
#
# **Export:**
#
# .. code:: python
#
# model_scripted = torch.jit.script(model) # Export to TorchScript
# model_scripted.save('model_scripted.pt') # Save
# model_scripted = torch.jit.script(model) # TorchScript ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ
# model_scripted.save('model_scripted.pt') # ์ €์žฅํ•˜๊ธฐ
#
# **Load:**
#
Expand All @@ -240,14 +238,14 @@
# model = torch.jit.load('model_scripted.pt')
# model.eval()
#
# Remember that you must call ``model.eval()`` to set dropout and batch
# normalization layers to evaluation mode before running inference.
# Failing to do this will yield inconsistent inference results.
# ์ถ”๋ก  ์‹คํ–‰ ์ „, ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜(batch) ์ •๊ทœํ™” ๋ ˆ์ด์–ด๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ``model.eval()`` ์„ ํ˜ธ์ถœํ•ด์•ผ
# ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜ธ์ถœ ๊ณผ์ •์ด ์—†์œผ๋ฉด ์ผ๊ด€์„ฑ ์—†๋Š” ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
#
# For more information on TorchScript, feel free to visit the dedicated
# `tutorials <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__.
# You will get familiar with the tracing conversion and learn how to
# run a TorchScript module in a `C++ environment <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__.
# TorchScript์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด๋Š” ์ „์šฉ
# `์ž์Šต์„œ <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__ ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# `C++ ํ™˜๊ฒฝ <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํŠธ๋ ˆ์ด์‹ฑ(Tracing) ๋ณ€ํ™˜์„
# ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ C++ ํ™˜๊ฒฝ์—์„œ TorchScript ๋ชจ๋“ˆ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



######################################################################
Expand Down
Loading

0 comments on commit bb04c46

Please sign in to comment.