[모델 계산량] FLOPs 계산하는 방법 (How to calculate model FLOPs?)
Computer Vision

[모델 계산량] FLOPs 계산하는 방법 (How to calculate model FLOPs?)

반응형

 

 

임베디드 환경에 딥러닝 모델을 올리는 것을 " 포팅(Porting) " 이라고 한다.

포팅할 때 가장 중요한 것은, 해당 모델 FLOPs를 고려해야한다는 것이다.

 

 

예를 들어

Ambarella사의 CV22에 딥러닝 모델을 포팅하기 위해서는 최대 3~4 GFLOPs가 허용치이다.

 

 

FLOPs가 포팅에 절대적인 기준은 아니지만, 엄청 중요한 기준이 된다는 것은 확실하다.

그러므로 우리는 FLOPs를 자체적으로 계산할 줄 알아야한다.

 

 

 

그래서!!!

아래 내용을 한 문장으로 먼저 요약하자면...

  • pthflops를 사용 하면 된다!

 

 

 

 

 

 

" pthflops " install 하기


https://github.com/1adrianb/pytorch-estimate-flops

 

GitHub - 1adrianb/pytorch-estimate-flops: Estimate/count FLOPS for a given neural network using pytorch

Estimate/count FLOPS for a given neural network using pytorch - GitHub - 1adrianb/pytorch-estimate-flops: Estimate/count FLOPS for a given neural network using pytorch

github.com

위 링크가 pthflops github repository다.

궁금한 사람은 한번 들어가서 구경하길 바라고.

 

 

일단 설치 먼저 해보자.

!pip install pthflops

난 jupyter 환경에서 진행하기 때문에 " ! " 를 붙였다.터미널로 하는 사람들은 느낌표를 때고 입력하면 설치된다.

 

 

 

 

 

 

" pthflops " 활용 code


import torch
from torchvision.models import resnet18

from pthflops import count_ops

# Create a network and a corresponding input
device = 'cuda:0'
model = resnet18().to(device)
inp = torch.rand(1,3,224,224).to(device)

# Count the number of FLOPs
count_ops(model, inp)

torchvision에서 제공하는 resnet18 기본 sample code 이다.

 

 

입력하면, 여러분도 layer별로 FLOPs가 쭉 나오고 마지막에 아래 그림처럼 ouput이 나올 것이다.

1장씩 3Channel짜리 224x224 사이즈 이미지가 들어갔을 때, 1.83GFLOPs 라는 뜻.

 

 

 

만약, batch size가 1이 아니라, 64정도 된다면?

116.91 GFLOPs로, 약 64배정도 늘어난걸 확인할 수 있다.

 

 

 

 

 

 

 

 

 

의문점


자체 실험 결과, 커스터마이징한 모델도 FLOPs를 잘 뽑아내는 것 같음 

custom resnet50 = 3.89 GFLOPs
torchvision resnet50 = 4.14 GFLOPs

 

그러나, 궁금한 점은

  1. 더 복잡하며 최근에 나온 모델들도 정확히 지원이 되는지 모름
  2. 현업에서 flops 계산 어떻게 하는지 궁금

 

 

 

 

 

 

 

위 2가지 알게 되면 포스팅 수정 하겠습니다.

그리고, 아시는 분은 댓글 달아주시면

너무너무 감사하겠습니다. : )

 

 

 

반응형