Skip to content

Latest commit

ย 

History

History
93 lines (63 loc) ยท 4.97 KB

CONTRIBUTING_MODELS.md

File metadata and controls

93 lines (63 loc) ยท 4.97 KB

PyTorch Korea ์— ๋ชจ๋ธ ๊ฒŒ์‹œํ•˜๊ธฐ

๋ณธ ํŽ˜์ด์ง€๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ hub๋ฅผ ํ†ตํ•ด ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฐฐํฌ๋œ ๋ชจ๋ธ์„ Pytorch Korea์˜ ๋ชจ๋ธ ์•ˆ๋‚ด ํŽ˜์ด์ง€์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค.

1. torch.hub ๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ ๋ฐฐํฌํ•˜๊ธฐ

torch.hub๋Š” ์—ฐ๊ตฌ ์žฌํ˜„ ๋ฐ ์‚ฌ์šฉ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค.

1.1. ๋ชจ๋ธ ํผ๋ธ”๋ฆฌ์‹ฑ

Pytorch Hub๋Š” hubconf.py๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ(๋ชจ๋ธ ์ •์˜ ๋ฐ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜)์„ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ์— ๊ฒŒ์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

hubconf.py๋Š” ์—ฌ๋ ค ๊ฐœ์˜ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ(entry point)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ๋“ค์€ ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๊ฐ€ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ)

def entrypoint_name(*args, **kwargs):
    # args & kwargs are optional, for models which take positional/keyword arguments.
    ...

1.2. ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋‹ค์Œ์€ pytorch/vision/hubconf.py์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•œ resnet18 ๋ชจ๋ธ์˜ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ๋ฅผ ์ง€์ •ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ hubconf.py์˜ ๊ตฌํ˜„๋œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ์˜ค๋ฉด ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ํ™•์žฅ ๋ฒ„์ „์„ ์˜ˆ์‹œ๋กœ ์ž‘๋™ ๋ฐฉ์‹์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. pytorch/vision repo ์—ฌ๊ธฐ์„œ ๋ชจ๋“  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

dependencies = ['torch']
from torchvision.models.resnet import resnet18 as _resnet18


# resnet18์€ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
def resnet18(pretrained=False, **kwargs):
""" # ์ด ๋ฌธ์„œ ๋ฌธ์ž์—ด์€  hub.help() Resnet18 model ์•ˆ์—์„œ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค
	์‚ฌ์ „ ๊ต์œก (bool) : kwwargs, load pretrained weights๋ฅผ ๋ชจ๋ธ์— ์ ์šฉํ•ฉ๋‹ˆ๋‹ค
"""

    # ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‚ฌ์ „ ํ•™์Šต๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๋กœ๋“œ
    model = _resnet18(pretrained=pretrained, **kwargs)
    return model

dependencies ๋ณ€์ˆ˜๋Š” ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ๋‹ด์€ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ ํ•™์Šต์— ํ•„์š”ํ•œ ์ข…์†์„ฑ๊ณผ๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ์„ ์ฃผ์˜ํ•˜์„ธ์š”. args์™€ kwargs๋Š” ์‹ค์ œ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ Docstring์€ ๋„์›€ ๋ฉ”์‹œ์ง€์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๊ธฐ๋Šฅ๊ณผ ํ—ˆ์šฉ๋œ ์œ„์น˜/ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ(nn.module)์„ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜ ์ž‘์—… ํ๋ฆ„์„ ๋ณด๋‹ค ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ณด์กฐ ๋„๊ตฌ(์˜ˆ: tokenizer)๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ‘์ค„์ด ์•ž์— ๋ถ™์€ callables๋Š” torch.hub.list()์— ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š” ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋Š” ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ์— ๋กœ์ปฌ๋กœ ์ €์žฅ๋˜๊ฑฐ๋‚˜ torch.hub.load_state_dict_from_url()์„ ์‚ฌ์šฉํ•ด ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ 2GB ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ project release์— ์ฒจ๋ถ€ํ•˜๊ณ  ๋ฆด๋ฆฌ์Šค์˜ URL์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” torchvision.models.resnet.resnet18์ด pretrained๋ฅผ ๋‹ค๋ฃจ์ง€๋งŒ, ํ•ด๋‹น ๋กœ์ง์„ ์—”ํŠธ๋ฆฌ ํฌ์ธํŠธ ์ •์˜์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

if pretrained:
	#์ฒดํฌ ํฌ์ธํŠธ๋กœ ๋กœ์ปฌ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ, ์˜ˆ๋ฅผ ๋“ค๋ฉด <RELATIVE_PATH_TO_CHECKPOINT>=weights/save.pth์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค
    dirname = os.path.dirname(__file__)
    checkpoint = os.path.join(dirname, <RELATIVE_PATH_TO_CHECKPOINT>)
    state_dict = torch.load(checkpoint)
    model.load_state_dict(state_dict)

   # ์ฒดํฌ ํฌ์ธํŠธ๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์— ์ €์žฅ๋œ ๊ฒฝ์šฐ
    checkpoint = 'https://download.pytorch.org/models/resnet18-5c106cde.pth'
    model.load_state_dict(torch.hub.load_state_dict_from_url(checkpoint, progress=False))

์ฃผ์˜ ์‚ฌํ•ญ ๋ฐฐํฌ๋œ ๋ชจ๋ธ๋“ค์€ ์ ์–ด๋„ branch/tag์— ์†ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋žœ๋ค ์ปค๋ฐ‹์ด ๋˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

2. Pytorch Korea ์— ๋ชจ๋ธ ์•ˆ๋‚ด ํŽ˜์ด์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

- `torch.hub` ๋ฅผ ๋ฐฐํฌํ•œ ์ดํ›„ ํ•ด๋‹น ๋ชจ๋ธ์— ๋Œ€ํ•œ ์•ˆ๋‚ด ํŽ˜์ด์ง€๋ฅผ pytorch.kr ์— ๋ชจ๋ธ ์•ˆ๋‚ด ํŽ˜์ด์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

2.1. ํŽ˜์ด์ง€ ์ž‘์„ฑํ•˜๊ธฐ

2.1.1. ๋ณธ ์ €์žฅ์†Œ ๋ฃจํŠธ์— ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

touch ${project_root}/sample.md

2.1.2. template.md ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํŒŒ์ผ ์ž‘์„ฑํ•˜๊ธฐ

2.2. ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ

2.2.1. ๋นŒ๋“œํ•˜๊ธฐ ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํ™ˆํŽ˜์ด์ง€๋ฅผ ๋นŒ๋“œ

2.2.2. ์ดํ›„์— https://127.0.0.1:4000/hub/ ์—์„œ ์ถ”๊ฐ€๋œ ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ํŽ˜์ด์ง€ ์•ˆ๋‚ด

2.3. ๊ธฐ์—ฌํ•˜๊ธฐ

์ถ”๊ฐ€ํ•œ ํŽ˜์ด์ง€๋Š” ๊ธฐ์—ฌํ•˜๊ธฐ ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋ณธ ๋ ˆํฌ์ง€ํ„ฐ๋ฆฌ์— ๊ธฐ์—ฌํ•ด์ฃผ์„ธ์š”!