๐Ÿ Python & library/PyTorch

[PyTorch] ๋ชจ๋ธ ์‹œ๊ฐํ™” ํˆด ์„ธ๊ฐ€์ง€ - Torchviz, HiddenLayer, Netron (Model visualization)

๋ณต๋งŒ 2022. 1. 13. 15:41

PyTorch ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ํˆด ์„ธ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. 

 

์ถœ์ฒ˜: https://stackoverflow.com/questions/52468956/how-do-i-visualize-a-net-in-pytorch

 

How do I visualize a net in Pytorch?

import torch import torch.nn as nn import torch.optim as optim import torch.utils.data as data import torchvision.models as models import torchvision.datasets as dset import torchvision.transforms as

stackoverflow.com

 

 

1. Torchviz

https://github.com/szagoruyko/pytorchviz

 

backward pass๋ฅผ ์ด์šฉํ•ด ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•œ๋‹ค. ์ค‘๊ฐ„์— ์–ด๋–ป๊ฒŒ gradient๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  backward ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๋Š”์ง€ ๋ชจ๋‘ ๋ณด์—ฌ์ค€๋‹ค.

 

Install:

pip install torchviz

 

Usage:

from torchviz import make_dot
make_dot(y_pred, params=dict(model.named_parameters())

 

Example:

 

 

 

2. HiddenLayer

https://github.com/waleedka/hiddenlayer

 

Install:

pip install hiddenlayer

 

Usage:

import hiddenlayer as hl
hl.build_graph(model, input)

 

Example:

 

๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™์€๋ฐ ๊ทธ๋ž˜ํ”„ ์ƒ‰๊น”์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜, ์—ฌ๋Ÿฌ node๋“ค์„ ํ•ฉ์ณ ํ•˜๋‚˜์˜ block์œผ๋กœ ๋งŒ๋“ ๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” customizing option๋“ค์ด ์žˆ๋‹ค. 

๊ทธ๋Ÿฐ๋ฐ ์—ฐ์‚ฐ๊ทธ๋ž˜ํ”„์— ๋„ˆ๋ฌด ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์ด ๋งŽ๊ธด ํ•˜๋‹ค. BN์„ constant-cast-add-div ์˜ ๊ณผ์ •์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๋˜์ง€..

Hiddenlayers๋Š” ๋ชจ๋ธ ์‹œ๊ฐํ™” ์ด์™ธ์—๋„ training ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์‹œ๊ฐํ™” ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

 

 

3. Netron

https://github.com/lutzroeder/netron

 

desktop application์œผ๋กœ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ , browser version์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

PyTorch ๋ชจ๋ธ๋„ experimental support๋ฅผ ์ง„ํ–‰ ์ค‘์ด๋ผ๊ณ  ํ•ด์„œ torch.save๋กœ ์ €์žฅํ•œ ๋ชจ๋ธ๊ณผ state_dict๋ฅผ ๊ฐ๊ฐ ๋„ฃ์–ด๋ดค๋Š”๋ฐ ๋‘˜๋‹ค ์ข€.. ์œ ์˜๋ฏธํ•œ ์ •๋ณด๊ฐ€ ์•„๋‹ˆ๋‹ค.

 

model.state_dict๋ฅผ ๋ถˆ๋Ÿฌ์™”์„ ๋•Œ
model์„ ํ†ต์งธ๋กœ ๋ถˆ๋Ÿฌ์™”์„ ๋•Œ

 

ONNX format์œผ๋กœ ๋ชจ๋ธ์„ ์ €์žฅํ•œ ํ›„ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์ž˜ ๋ณด์ธ๋‹ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋‚ด ๋ชจ๋ธ์€ complex number์„ input์œผ๋กœ ๋ฐ›๋Š”๋ฐ ONNX format์ด complex format์„ ์ง€์›์„ ์•ˆํ•œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ƒฅ ์•ˆํ•ด๋ดค์Œ.

๊ทธ๋Ÿฌ๋‚˜ ์ผ๋‹จ browser๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์žˆ๊ณ , zoom์ด๋‚˜ ์ด๋™ ๋“ฑ UI๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ์–ด ์‚ฌ์šฉํ•˜๊ธฐ ๋งค์šฐ ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 


 

๋‚ด๊ฐ€ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ์€ complex๋ฅผ input์œผ๋กœ ๋ฐ›๋Š” ๋ชจ๋ธ์ธ๋ฐ, ONNX format์ด complex๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„์„œ Netron์€ .pth ํŒŒ์ผ๋กœ๋งŒ ํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๊ณ , hiddenlayer ์—ญ์‹œ ์ค‘๊ฐ„์— ONNX๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ์„œ ์‚ฌ์šฉ์„ ๋ชปํ•ด๋ดค๋‹ค.

๊ฒฐ๋ก ์€ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ์€ Torchviz, customizing์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๊ธฐ๋Šฅ์ด ๋งŽ์€ ๊ฒƒ์€ HiddenLayer, browser-base๋ผ ํŽธ๋ฆฌํ•œ ๊ฒƒ์€ Netron์ธ๋“ฏ โ—

๋ฐ˜์‘ํ˜•