Skip to content

Commit

Permalink
'PyTorch에서 추론(inference)을 위해 모델 저장하기 & 불러오기' 번역 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
9bow committed Jun 9, 2020
1 parent 46728d1 commit 9d1a57d
Showing 1 changed file with 85 additions and 92 deletions.
177 changes: 85 additions & 92 deletions recipes_source/recipes/saving_and_loading_models_for_inference.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
"""
Saving and loading models for inference in PyTorch
==================================================
There are two approaches for saving and loading models for inference in
PyTorch. The first is saving and loading the ``state_dict``, and the
second is saving and loading the entire model.
PyTorch에서 추론(inference)을 위해 모델 저장하기 & 불러오기
================================================================
Introduction
PyTorch에서는 추론(inference)을 위해 모델을 저장하고 불러오는데 2가지 접근법이
있습니다. 첫번째는 ``state_dict`` 를 저장하고 불러오는 것이고, 두번째는 전체
모델을 저장하는 것입니다.
개요
------------
Saving the model’s ``state_dict`` with the ``torch.save()`` function
will give you the most flexibility for restoring the model later. This
is the recommended method for saving models, because it is only really
necessary to save the trained model’s learned parameters.
When saving and loading an entire model, you save the entire module
using Python’s
`pickle <https://docs.python.org/3/library/pickle.html>`__ module. Using
this approach yields the most intuitive syntax and involves the least
amount of code. The disadvantage of this approach is that the serialized
data is bound to the specific classes and the exact directory structure
used when the model is saved. The reason for this is because pickle does
not save the model class itself. Rather, it saves a path to the file
containing the class, which is used during load time. Because of this,
your code can break in various ways when used in other projects or after
refactors.
In this recipe, we will explore both ways on how to save and load models
for inference.
Setup
-----
Before we begin, we need to install ``torch`` if it isn’t already
available.
torch.save() 함수를 사용하여 모델의 ``state_dict`` 를 저장하면 이후에 모델을
불러올 때 유연함을 크게 살릴 수 있습니다. 학습된 모델의 매개변수(parameter)만을
저장하면되므로 모델 저장 시에 권장하는 방법입니다. 모델 전체를 저장하고 불러올
때에는 Python의 `pickle <https://docs.python.org/3/library/pickle.html>`__ 모듈을
사용하여 전체 모듈을 저장합니다. 이 방식은 직관적인 문법을 사용하며 코드의 양도
적습니다. 이 방식의 단점은 직렬화(serialized)된 데이터가 모델을 저장할 때 사용한
특정 클래스 및 디렉토리 구조에 종속(bind)된다는 것입니다. 그 이유는 pickle이
모델 클래스 자체를 저장하지 않기 때문입니다. 오히려 불러올 때 사용되는 클래스가
포함된 파일의 경로를 저장합니다. 이 때문에 작성한 코드가 다른 프로젝트에서
사용되거나 리팩토링을 거치는 등의 과정에서 동작하지 않을 수 있습니다. 이 레시피에서는
추론을 위해 모델을 저장하고 불러오는 두 가지 방법 모두를 살펴보겠습니다.
설정
----------
시작하기 전에 ``torch`` 가 없다면 설치해야 합니다.
::
Expand All @@ -40,34 +34,34 @@


######################################################################
# Steps
# -----
#
# 1. Import all necessary libraries for loading our data
# 2. Define and intialize the neural network
# 3. Initialize the optimizer
# 4. Save and load the model via ``state_dict``
# 5. Save and load the entire model
#
# 1. Import necessary libraries for loading our data
# 단계(Steps)
# ------------
#
# 1. 데이터 불러올 때 필요한 라이브러리들 불러오기
# 2. 신경망을 구성하고 초기화하기
# 3. 옵티마이저 초기화하기
# 4. ``state_dict`` 을 통해 모델을 저장하고 불러오기
# 5. 전체 모델을 저장하고 불러오기
#
# 1. 데이터 불러올 때 필요한 라이브러리들 불러오기
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# For this recipe, we will use ``torch`` and its subsidiaries ``torch.nn``
# and ``torch.optim``.
#
#
# 이 레시피에서는 ``torch`` 와 여기 포함된 ``torch.nn`` 와 ``torch.optim` 을
# 사용하겠습니다.
#

import torch
import torch.nn as nn
import torch.optim as optim


######################################################################
# 2. Define and intialize the neural network
# 2. 신경망을 구성하고 초기화하기
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# For sake of example, we will create a neural network for training
# images. To learn more see the Defining a Neural Network recipe.
#
#
# 예를 들어, 이미지를 학습하는 신경망을 만들어보겠습니다. 더 자세한 내용은
# 신경망 구성하기 레시피를 참고해주세요.
#

class Net(nn.Module):
def __init__(self):
Expand All @@ -93,76 +87,75 @@ def forward(self, x):


######################################################################
# 3. Initialize the optimizer
# 3. 옵티마이저 초기화하기
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# We will use SGD with momentum.
#
#
# 모멘텀(momentum)을 갖는 SGD를 사용하겠습니다.
#

optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)


######################################################################
# 4. Save and load the model via ``state_dict``
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Let’s save and load our model using just ``state_dict``.
#
# 4. ``state_dict`` 을 통해 모델을 저장하고 불러오기
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# 이제 ``state_dict`` 만 사용하여 모델을 저장하고 불러와보겠습니다.`
#

# Specify a path
# 경로 지정
PATH = "state_dict_model.pt"

# Save
# 저장하기
torch.save(net.state_dict(), PATH)

# Load
# 불러오기
model = Net()
model.load_state_dict(torch.load(PATH))
model.eval()


######################################################################
# A common PyTorch convention is to save models using either a ``.pt`` or
# ``.pth`` file extension.
#
# Notice that the ``load_state_dict()`` function takes a dictionary
# object, NOT a path to a saved object. This means that you must
# deserialize the saved state_dict before you pass it to the
# ``load_state_dict()`` function. For example, you CANNOT load using
# ``model.load_state_dict(PATH)``.
#
# Remember too, 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.
#
# 5. Save and load entire model
# PyTorch에서는 모델을 저장할 때 ``.pt`` 또는 ``.pth`` 확장자를 사용하는 것이
# 일반적인 규칙입니다.
#
# ``load_state_dict()`` 함수는 저장된 객체의 경로가 아닌, 사전 객체를 사용합니다.
# 즉, 저장된 state_dict를 ``load_state_dict()`` 함수에 전달하기 전에 반드시
# 역직렬화(deserialize)를 해야 합니다. 예를 들어, ``model.load_state_dict(PATH)``
# 와 같이 사용할 수 없습니다.
#
# 또한, 추론을 실행하기 전에 ``model.eval()`` 을 호출하여 드롭아웃(dropout)과
# 배치 정규화 층(batch normalization layer)을 평가(evaluation) 모드로 바꿔야한다는
# 것을 기억하세요. 이것을 빼먹으면 일관성 없는 추론 결과를 얻게 됩니다.
#
# 5. 전체 모델을 저장하고 불러오기
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Now let’s try the same thing with the entire model.
#
#
# 이제 전체 모델에 대해서 똑같이 해보겠습니다.
#

# Specify a path
# 경로 지정
PATH = "entire_model.pt"

# Save
# 저장하기
torch.save(net, PATH)

# Load
# 불러오기
model = torch.load(PATH)
model.eval()


######################################################################
# Again here, remember that you must call model.eval() to set dropout and
# batch normalization layers to evaluation mode before running inference.
#
# Congratulations! You have successfully saved and load models for
# inference in PyTorch.
#
# Learn More
# ----------
#
# Take a look at these other recipes to continue your learning:
#
# - `Saving and loading a general checkpoint in PyTorch <https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_a_general_checkpoint.html>`__
# - `Saving and loading multiple models in one file using PyTorch <https://pytorch.org/tutorials/recipes/recipes/saving_multiple_models_in_one_file.html>`__
# 여기서도 또한 model.eval()을 실행하여 드롭아웃(dropout)과 배치 정규화 층
# (batch normalization layer)을 평가(evaluation) 모드로 바꿔야한다는
# 것을 기억하세요.
#
# 축하합니다! 지금까지 PyTorch에서 추론을위한 모델을 성공적으로 저장하고 불러왔습니다.
#
# 더 알아보기
# -------------
#
# 다른 레시피를 둘러보고 계속 배워보세요:
#
# - :doc:`/recipes/recipes/saving_and_loading_a_general_checkpoint`
# - :doc:`/recipes/recipes/saving_multiple_models_in_one_file`

0 comments on commit 9d1a57d

Please sign in to comment.