반응형

다음 편 :

[AI/Self-Study] - PyTorch 모델 구조 summary & 그래프화 2

 

PyTorch 모델 구조 summary & 그래프화 2

이전 글: [AI/Self-Study] - PyTorch 모델 구조 summary & 그래프화 1 PyTorch 모델 구조 summary & 그래프화 1 TensorFlow에서는 model.summary() 메서드 호출을 통해 모델을 요약해서 layer마다 shape와 같은 정..

lynnshin.tistory.com

 

 

TensorFlow에서는 model.summary() 메서드 호출을 통해 모델을 요약해서 layer마다 shape와 같은 정보들을 볼 수 있다.

PyTorch에서 model.summary() 처럼 사용할 수 있는 메서드와 모델 구조를 그래프화 하는 방법을 알아보자!

 

+) 결론 : pytorch_model_summary 또는 torchsummaryX 라이브러리를 자주 이용할 것 같다.

 

1. 모델 summary 확인

1-1) PyTorch 에서는 print 함수로 간단하게 모델을 출력할 수 있다.

 

 EfficientNet print(model) 출력 일부 

 

1-2) torchsummary 라이브러리를 이용한 방법

  • pip install torchsummary
  • summary(your_model, input_size=(channels, H, W))
  • input_size를 넣어서 output shape와 파라미터 수를 확인할 수 있다.

 EfficientNet torchsummary 출력 일부 

 

1-3) pytorch_model_summary 라이브러리를 이용한 방법

  • pip install pytorch-model-summary
  • summary(model, *inputs, batch_size=-1, show_input=False, show_hierarchical=False, print_summary=False, max_depth=1, show_parent_layers=False)
    • input shape : (N x C x H x W) -> batch_size, channel, height, width  (입력 크기로 토치텐서를 주고 입력 크기가 출력됨)
    • show_input = True면 input shape가 나오고 False면 output shape가 나온다. (default: False)
    • print_summary = True 면 print() 함수를 안써도 된다. (default 가 False이므로 print()를 써줘야 이쁘게? 정리된 버전을 볼 수 있다)
    • max_depth = None 와 show_parent_layers = True를 설정하면 모든 레이어의 정보와 레이어의 parent layers path도 볼 수 있다. max_depth의 default는 1이며 세부 레이어를 보고 싶은 경우에는 값을 조정해주면 된다. max_depth=None이면 모든 세부 레이어를 볼 수 있다.
    • show_hierarchical = True를 설정하면 summary table 외에도 모델의 hierarchical view를 볼 수 있다.
    • batch_size를 설정해주면 마지막 summary table에 같이 출력된다.
      • Total params: 28,346,931 Trainable params: 28,346,931 Non-trainable params: 0 Batch size: 32

 

show_input = True

 

max_depth=None & show_parent_layers=True로 설정해 모든 세부 layers와 parent layers를 확인 가능

 

show_hierarchical = True

 

1-4) torchinfo 라이브러리를 이용한 방법

  • pip install torchinfo
  • (batch, channel, height, width)와 같은 형태로 데이터를 입력

EfficientNet torchinfo 출력 일부

 

 

1-5) torchsummaryX 라이브러리를 이용한 방법

  • pip install torchsummaryX
    • from torchsummaryX import summary
  • (batch, channel, height, width)와 같은 형태로 데이터를 입력
    • summary(your_model, torch.zeros((1, 3, 224, 224)))

 

CNN torchsummaryX 출력

 

input size 와 output size 둘다 확인할 수 있고, depth도 설정할 수 있는 여러 옵션들이 많은 pytorch_model_summary 라이브러리를 자주 이용할 것 같다.

 

+) torchsummaryX 도 최근 논문 GitHub를 살펴보았을 때, 많이 사용하는 것 같다. 깔끔하고 보기 쉬운 것 같다.

 

colab 코드 링크 : https://colab.research.google.com/drive/1BqIcQMNYsfzmH9ju25KVDxszW9_6qOLY?usp=sharing

반응형