Machine Learning

[Pytorch] 모델 구조 확인, parameter확인

모델 구조(architecture) 확인하기

model = models.resnet50(pretrained = True)
print(model)

모델 파라미터(parameter) 확인하기 - parameters()

for name, param in model.named_parameters():
	count+=1
    if count==1:
    print(param.size())
    print(name)

torch.Size([64])
conv1.1.weight

parameters()을 이용해서  총 파라미터 수 counting

 

모델 파라미터(parameter) 확인하기 - children()

for child in model.children():
        count += 1
        if count == 2:
            print(child)

children()에서 name변수를 이용하면 현재 parameter(layer) 위치 확인이 가능하다.

count = 0
for name, layer in model.named_childeren():
	count += 1
    if count == 1:
    	print(name) # conv1출력

torchsummary 사용하기

또는 torchsummary를 사용하여 output shape와 parameter수를 같이 확인하는 방법도 있다.

!pip install torchsummary
from torchsummary import summary as summary

model = models.resnet50(pretrained=True)
summary(model, (3,224,224)) # (model, input_size)

 

 

위에서 parameters()를 확인하다가 실제로 알게 된 것

BatchNorm layer에는 실제 파라미터가 (weight*gamma, bias*beta) 2개가 존재

ReLU, MaxPool은 학습 가능한 파라미터가 없으므로 생략되고 바로 다음 parameter가 있는 conv layer의 파라미터를 출력한다.

다음 포스팅에서는 이 위의 내용들을 사용해서 pytorch에서 pre-trained model을 가지고 fine-tuning을 어떻게 하는지를 알아보려한다.