[pytorch] model ์— ์ ‘๊ทผํ•˜๊ธฐ, ํŠน์ • layer ๋ณ€๊ฒฝํ•˜๊ธฐ

2022. 1. 5. 02:35ยท๐Ÿ’ป Programming/AI & ML
๋ฐ˜์‘ํ˜•

pytorch ๋ชจ๋ธ์— ์ ‘๊ทผํ•˜๊ณ  ํŠน์ • layer ๋˜๋Š” layer ๋‚ด๋ถ€์˜ ๋ชจ๋“ˆ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ๋‹ค.

- ์˜ˆ์‹œ ๋ชจ๋ธ : resnet50

 

import torch.nn as nn
import torchvision.models as models

model = models.resnet50(pretrained=True)

1. self.named_parameters() 

for name, param in model.layer1.named_parameters():
    print(name,param.shape,sep="   ")

 

2. self.named_children()

for name,ch in model.layer1.named_children():
    print("name :",name)
    print("child :", ch)
    print('===========================')

 

๋ฐ˜์‘ํ˜•

 

3. Model layer ๋ณ€๊ฒฝ

๋ชจ๋ธ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ return ํ•ด์ฃผ๋Š” named_children()์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•˜๋Š” layer๋ฅผ ์ฐพ๊ณ , ํด๋ž˜์Šค์˜ ๋ชจ๋“ˆ๋“ค์„ return ํ•ด์ฃผ๋Š” _modules๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผ.

print('model.layer1._modules[\'0\'] ๋ณ€๊ฒฝ ์ „ :', model.layer1._modules['0'],sep ='\n')
model.layer1._modules['0']._modules['conv1'] = nn.Conv2d(77,77,7)
print('model.layer1._modules[\'0\'] ๋ณ€๊ฒฝ ํ›„ :', model.layer1._modules['0'],sep ='\n')
print('model.layer1._modules[\'0\']._modules[\'conv1\'] ๋ณ€๊ฒฝ ํ›„ :',model.layer1._modules['0']._modules['conv1'],sep ='\n')

 

* ์ ‘๊ทผ ์˜ˆ์‹œ

Resnet50 ๋ชจ๋ธ์—์„œ layer1์˜ 0๋ฒˆ์งธ bottleneck์˜ downsample์˜ Conv2d ์—์„œ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด -

model.layer1._modules['0']._modules['downsample']._modules['0']
๋ฐ˜์‘ํ˜•

'๐Ÿ’ป Programming > AI & ML' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[pytorch] pytorch ๋ชจ๋ธ ๋กœ๋“œ ์ค‘ Missing key(s) in state_dict ์—๋Ÿฌ  (0) 2022.12.15
[pytorch] COCO Data Format ์ „์šฉ Custom Dataset ์ƒ์„ฑ  (1) 2022.06.04
[pytorch] Custom dataset, dataloader ๋งŒ๋“ค๊ธฐ  (0) 2022.01.02
[pytorch] DataParallel ๋กœ ํ•™์Šตํ•œ ๋ชจ๋ธ load  (0) 2021.02.17
[pytorch] ๋ชจ๋ธ์˜ ์ผ๋ถ€ ๋ ˆ์ด์–ด ์›จ์ดํŠธ ์—…๋ฐ์ดํŠธ ๋ง‰๊ธฐ | model freezing (๋ชจ๋ธ ํ”„๋ฆฌ์ง•)  (0) 2021.02.17
'๐Ÿ’ป Programming/AI & ML' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [pytorch] pytorch ๋ชจ๋ธ ๋กœ๋“œ ์ค‘ Missing key(s) in state_dict ์—๋Ÿฌ
  • [pytorch] COCO Data Format ์ „์šฉ Custom Dataset ์ƒ์„ฑ
  • [pytorch] Custom dataset, dataloader ๋งŒ๋“ค๊ธฐ
  • [pytorch] DataParallel ๋กœ ํ•™์Šตํ•œ ๋ชจ๋ธ load
๋ญ…์ฆค
๋ญ…์ฆค
AI ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ
    ๋ฐ˜์‘ํ˜•
  • ๋ญ…์ฆค
    moovzi’s Doodle
    ๋ญ…์ฆค
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • โœจ About Me
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (213)
      • ๐Ÿ“– Fundamentals (34)
        • Computer Vision (9)
        • 3D vision & Graphics (6)
        • AI & ML (16)
        • NLP (2)
        • etc. (1)
      • ๐Ÿ› Research (75)
        • Deep Learning (7)
        • Perception (19)
        • OCR (7)
        • Multi-modal (5)
        • Image•Video Generation (18)
        • 3D Vision (4)
        • Material • Texture Recognit.. (8)
        • Large-scale Model (7)
        • etc. (0)
      • ๐Ÿ› ๏ธ Engineering (8)
        • Distributed Training & Infe.. (5)
        • AI & ML ์ธ์‚ฌ์ดํŠธ (3)
      • ๐Ÿ’ป Programming (92)
        • Python (18)
        • Computer Vision (12)
        • LLM (4)
        • AI & ML (18)
        • Database (3)
        • Distributed Computing (6)
        • Apache Airflow (6)
        • Docker & Kubernetes (14)
        • ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ (4)
        • C++ (1)
        • etc. (6)
      • ๐Ÿ’ฌ ETC (4)
        • ์ฑ… ๋ฆฌ๋ทฐ (4)
  • ๋งํฌ

    • ๋ฆฌํ‹€๋ฆฌ ํ”„๋กœํ•„ (๋ฉ˜ํ† ๋ง, ๋ฉด์ ‘์ฑ…,...)
    • ใ€Ž๋‚˜๋Š” AI ์—”์ง€๋‹ˆ์–ด์ž…๋‹ˆ๋‹คใ€
    • Instagram
    • Brunch
    • Github
  • ์ธ๊ธฐ ๊ธ€

  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
๋ญ…์ฆค
[pytorch] model ์— ์ ‘๊ทผํ•˜๊ธฐ, ํŠน์ • layer ๋ณ€๊ฒฝํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”