|
13 | 13 |
|
14 | 14 | 1) `torch.save <https://pytorch.org/docs/stable/torch.html?highlight=save#torch.save>`__: |
15 | 15 | ์ง๋ ฌํ๋ ๊ฐ์ฒด๋ฅผ ๋์คํฌ์ ์ ์ฅํฉ๋๋ค. ์ด ํจ์๋ Python์ |
16 | | - `pickle <https://docs.python.org/3/library/pickle.html>`__ ์ ์ฌ์ฉํ์ฌ ์ง๋ ฌํํฉ๋๋ค |
| 16 | + `pickle <https://docs.python.org/3/library/pickle.html>`__ ์ ์ฌ์ฉํ์ฌ ์ง๋ ฌํํฉ๋๋ค. |
17 | 17 | ์ด ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์ข
๋ฅ์ ๊ฐ์ฒด์ ๋ชจ๋ธ, Tensor ๋ฐ ์ฌ์ ์ ์ ์ฅํ ์ ์์ต๋๋ค. |
18 | 18 |
|
19 | 19 | 2) `torch.load <https://pytorch.org/docs/stable/torch.html?highlight=torch%20load#torch.load>`__: |
20 | 20 | `pickle <https://docs.python.org/3/library/pickle.html>`__\ ์ ์ฌ์ฉํ์ฌ |
21 | | - ์ ์ฅ๋ ๊ฐ์ฒด ํ์ผ๋ค์ ์ญ์ง๋ ฌํํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฝ๋๋ค. ์ด ํจ์๋ ๋ฐ์ดํฐ๋ฅผ ์ฅ์น์ |
22 | | - ๋ถ๋ฌ์ฌ ๋๋ ์ฌ์ฉํฉ๋๋ค. |
23 | | - (`์ฅ์น๊ฐ ๋ชจ๋ธ ์ ์ฅํ๊ธฐ & ๋ถ๋ฌ์ค๊ธฐ <#device>`__ ์ฐธ๊ณ ) |
| 21 | + ์ ์ฅ๋ ๊ฐ์ฒด ํ์ผ๋ค์ ์ญ์ง๋ ฌํํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฝ๋๋ค. ์ด ํจ์๋ ๋ฐ์ดํฐ๋ฅผ ์ฅ์น์ ๋ถ๋ฌ์ฌ |
| 22 | + ๋์๋ ์ฌ์ฉ๋ฉ๋๋ค. |
| 23 | + (`์ฅ์น ๊ฐ ๋ชจ๋ธ ์ ์ฅํ๊ธฐ & ๋ถ๋ฌ์ค๊ธฐ <#device>`__ ์ฐธ๊ณ ) |
24 | 24 |
|
25 | 25 | 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>`__: |
26 | 26 | ์ญ์ง๋ ฌํ๋ *state_dict* ๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋งค๊ฐ๋ณ์๋ค์ ๋ถ๋ฌ์ต๋๋ค. |
|
145 | 145 | # model.eval() |
146 | 146 | # |
147 | 147 | # .. note:: |
148 | | -# PyTorch ๋ฒ์ 1.6์์๋ ์๋ก์ด Zipํ์ผ-๊ธฐ๋ฐ์ ํ์ผ ํฌ๋งท์ ์ฌ์ฉํ๋๋ก |
149 | | -# ``torch.save`` ๊ฐ ๋ณ๊ฒฝ๋์์ต๋๋ค. ``torch.load`` ๋ ์์ ๋ฐฉ์์ ํ์ผ๋ค์ |
| 148 | +# PyTorch ๋ฒ์ 1.6์์๋ ``torch.save`` ๊ฐ ์๋ก์ด Zipํ์ผ-๊ธฐ๋ฐ์ ํ์ผ |
| 149 | +# ํฌ๋งท์ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค. ``torch.load`` ๋ ์์ ๋ฐฉ์์ ํ์ผ๋ค์ |
150 | 150 | # ์ฝ์ด์ฌ ์ ์๋๋ก ํ๊ณ ์์ต๋๋ค. ์ด๋ค ์ด์ ์์๋ ``torch.save`` ๊ฐ ์์ |
151 | 151 | # ๋ฐฉ์์ ์ฌ์ฉํ๋๋ก ํ๊ณ ์ถ๋ค๋ฉด, ``_use_new_zipfile_serialization=False`` ์ |
152 | 152 | # kwarg๋ก ์ ๋ฌํ์ธ์. |
153 | 153 | # |
154 | | -# ์ถ๋ก ์ ์ํด ๋ชจ๋ธ์ ์ ์ฅํ ๋๋ ํ์ต๋ ๋ชจ๋ธ์ ํ์ต๋ ๋งค๊ฐ๋ณ์๋ง ์ ์ฅํ๋ฉด ๋ฉ๋๋ค. |
| 154 | +# ์ถ๋ก ์ ์ํด ๋ชจ๋ธ์ ์ ์ฅํ ๋๋ ๊ทธ ๋ชจ๋ธ์ ํ์ต๋ ๋งค๊ฐ๋ณ์๋ง ์ ์ฅํ๋ฉด ๋ฉ๋๋ค. |
155 | 155 | # ``torch.save()`` ๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ *state_dict* ๋ฅผ ์ ์ฅํ๋ ๊ฒ์ด ๋์ค์ ๋ชจ๋ธ์ |
156 | 156 | # ์ฌ์ฉํ ๋ ๊ฐ์ฅ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์๋, ๋ชจ๋ธ ์ ์ฅ ์ ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ์
๋๋ค. |
157 | 157 | # |
158 | 158 | # PyTorch์์๋ ๋ชจ๋ธ์ ์ ์ฅํ ๋ ``.pt`` ๋๋ ``.pth`` ํ์ฅ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด |
159 | 159 | # ์ผ๋ฐ์ ์ธ ๊ท์น์
๋๋ค. |
160 | 160 | # |
161 | | -# ์ถ๋ก ์ ์คํํ๊ธฐ ์ ์๋ ๋ฐ๋์ ``model.eval()`` ์ ํธ์ถํ์ฌ ๋๋กญ์์ ๋ฐ ๋ฐฐ์น |
162 | | -# ์ ๊ทํ๋ฅผ ํ๊ฐ ๋ชจ๋๋ก ์ค์ ํ์ฌ์ผ ํฉ๋๋ค. ์ด๊ฒ์ ํ์ง ์์ผ๋ฉด ์ถ๋ก ๊ฒฐ๊ณผ๊ฐ ์ผ๊ด์ฑ |
163 | | -# ์๊ฒ ์ถ๋ ฅ๋ฉ๋๋ค. |
| 161 | +# ์ถ๋ก ์ ์คํํ๊ธฐ ์ ์ ๋ฐ๋์ ``model.eval()`` ์ ํธ์ถํ์ฌ ๋๋กญ์์ ๋ฐ ๋ฐฐ์น |
| 162 | +# ์ ๊ทํ๋ฅผ ํ๊ฐ ๋ชจ๋๋ก ์ค์ ํ์ฌ์ผ ํฉ๋๋ค. ์ด ๊ณผ์ ์ ๊ฑฐ์น์ง ์์ผ๋ฉด ์ผ๊ด์ฑ ์๋ |
| 163 | +# ์ถ๋ก ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค. |
164 | 164 | # |
165 | 165 | # .. Note :: |
166 | 166 | # |
167 | 167 | # ``load_state_dict()`` ํจ์์๋ ์ ์ฅ๋ ๊ฐ์ฒด์ ๊ฒฝ๋ก๊ฐ ์๋, ์ฌ์ ๊ฐ์ฒด๋ฅผ |
168 | 168 | # ์ ๋ฌํด์ผ ํ๋ ๊ฒ์ ์ ์ํ์ธ์. ๋ฐ๋ผ์ ์ ์ฅ๋ *state_dict* ๋ฅผ ``load_state_dict()`` |
169 | 169 | # ํจ์์ ์ ๋ฌํ๊ธฐ ์ ์ ๋ฐ๋์ ์ญ์ง๋ ฌํ๋ฅผ ํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, |
170 | | -# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ์ ์์ผ๋ก๋ ์ฌ์ฉํ๋ฉด ์๋ฉ๋๋ค. |
| 170 | +# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ์ ์์ผ๋ก ์ฌ์ฉํ๋ฉด ์๋ฉ๋๋ค. |
171 | 171 | # |
172 | 172 | # .. Note :: |
173 | 173 | # |
|
192 | 192 | # |
193 | 193 | # .. code:: python |
194 | 194 | # |
195 | | -# # ๋ชจ๋ธ ํด๋์ค๋ ์ด๋๊ฐ์ ๋ฐ๋์ ์ ์ธ๋์ด ์์ด์ผ ํฉ๋๋ค |
| 195 | +# # ๋ชจ๋ธ ํด๋์ค๋ ์ด๋๊ฐ์ ๋ฐ๋์ ์ ์ธ๋์ด ์์ด์ผ ํฉ๋๋ค. |
196 | 196 | # model = torch.load(PATH) |
197 | 197 | # model.eval() |
198 | 198 | # |
|
213 | 213 | # ์ ๊ทํ๋ฅผ ํ๊ฐ ๋ชจ๋๋ก ์ค์ ํ์ฌ์ผ ํฉ๋๋ค. ์ด๊ฒ์ ํ์ง ์์ผ๋ฉด ์ถ๋ก ๊ฒฐ๊ณผ๊ฐ ์ผ๊ด์ฑ |
214 | 214 | # ์๊ฒ ์ถ๋ ฅ๋ฉ๋๋ค. |
215 | 215 | # |
216 | | -# Export/Load Model in TorchScript Format |
| 216 | +# TorchScript ํฌ๋งท์ผ๋ก ๋ชจ๋ธ ๋ด๋ณด๋ด๊ธฐ/๊ฐ์ ธ์ค๊ธฐ |
217 | 217 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
218 | 218 | # |
219 | | -# One common way to do inference with a trained model is to use |
220 | | -# `TorchScript <https://pytorch.org/docs/stable/jit.html>`__, an intermediate |
221 | | -# representation of a PyTorch model that can be run in Python as well as in a |
222 | | -# high performance environment like C++. TorchScript is actually the recommended model format |
223 | | -# for scaled inference and deployment. |
| 219 | +# ํ๋ จ๋ ๋ชจ๋ธ๋ก ์ถ๋ก ์ ์ํํ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ ์ค ํ๋๋ `TorchScript <https://pytorch.org/docs/stable/jit.html>`__ ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. |
| 220 | +# TorchScript๋ ํ์ด์ฌ ํ๊ฒฝ์ด๋ C++์ ๊ฐ์ ๊ณ ์ฑ๋ฅ ํ๊ฒฝ์์ ์คํํ ์ ์๋ |
| 221 | +# ํ์ดํ ์น ๋ชจ๋ธ์ ์ค๊ฐ ํํ(IR; Intermediate Representation)์
๋๋ค. |
| 222 | +# TorchScript๋ ํ์ฅ๋ ์ถ๋ก ๋ฐ ๋ฐฐํฌ์ ๊ถ์ฅ๋๋ ๋ชจ๋ธ ํ์์ด๊ธฐ๋ ํฉ๋๋ค. |
224 | 223 | # |
225 | 224 | # .. note:: |
226 | | -# Using the TorchScript format, you will be able to load the exported model and |
227 | | -# run inference without defining the model class. |
| 225 | +# TorchScript ํ์์ ์ฌ์ฉํ๋ฉด ๋ชจ๋ธ ํด๋์ค๋ฅผ ์ ์ํ์ง ์๊ณ ๋ ๋ด๋ณด๋ธ ๋ชจ๋ธ์ ์ฝ์ด ์ค๊ฑฐ๋ ์ถ๋ก ์ ์คํํ ์ ์์ต๋๋ค. |
228 | 226 | # |
229 | 227 | # **Export:** |
230 | 228 | # |
231 | 229 | # .. code:: python |
232 | 230 | # |
233 | | -# model_scripted = torch.jit.script(model) # Export to TorchScript |
234 | | -# model_scripted.save('model_scripted.pt') # Save |
| 231 | +# model_scripted = torch.jit.script(model) # TorchScript ํ์์ผ๋ก ๋ด๋ณด๋ด๊ธฐ |
| 232 | +# model_scripted.save('model_scripted.pt') # ์ ์ฅํ๊ธฐ |
235 | 233 | # |
236 | 234 | # **Load:** |
237 | 235 | # |
|
240 | 238 | # model = torch.jit.load('model_scripted.pt') |
241 | 239 | # model.eval() |
242 | 240 | # |
243 | | -# Remember that you must call ``model.eval()`` to set dropout and batch |
244 | | -# normalization layers to evaluation mode before running inference. |
245 | | -# Failing to do this will yield inconsistent inference results. |
| 241 | +# ์ถ๋ก ์คํ ์ , ๋๋กญ์์ ๋ฐ ๋ฐฐ์น(batch) ์ ๊ทํ ๋ ์ด์ด๋ฅผ ํ๊ฐ ๋ชจ๋๋ก ์ค์ ํ๊ธฐ ์ํด ``model.eval()`` ์ ํธ์ถํด์ผ |
| 242 | +# ํฉ๋๋ค. ์ด ํธ์ถ ๊ณผ์ ์ด ์์ผ๋ฉด ์ผ๊ด์ฑ ์๋ ์ถ๋ก ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ฉ๋๋ค. |
246 | 243 | # |
247 | | -# For more information on TorchScript, feel free to visit the dedicated |
248 | | -# `tutorials <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__. |
249 | | -# You will get familiar with the tracing conversion and learn how to |
250 | | -# run a TorchScript module in a `C++ environment <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__. |
| 244 | +# TorchScript์ ๋ํ ์ถ๊ฐ ์ ๋ณด๋ ์ ์ฉ |
| 245 | +# `์์ต์ <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__ ์์ ์ฐพ์ ์ ์์ต๋๋ค. |
| 246 | +# `C++ ํ๊ฒฝ <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ํธ๋ ์ด์ฑ(Tracing) ๋ณํ์ |
| 247 | +# ์ํํ๋ ๋ฐฉ๋ฒ๊ณผ C++ ํ๊ฒฝ์์ TorchScript ๋ชจ๋์ ์คํํ๋ ๋ฐฉ๋ฒ์ ์ตํ ์ ์์ต๋๋ค. |
| 248 | + |
251 | 249 |
|
252 | 250 |
|
253 | 251 | ###################################################################### |
|
0 commit comments