THOP

https://github.com/Lyken17/pytorch-OpCounter
pip install thop(推荐用这个) 或者 pip install —upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git(这个方法需要同时安装pytorch)

  1. from torchvision.models import resnet50
  2. from thop import profile
  3. model = resnet50()
  4. input = torch.randn(1, 3, 224, 224)
  5. macs, params = profile(model, inputs=(input, ))

ptflops

pip install ptflops 或者 pip install —upgrade git+https://github.com/sovrasov/flops-counter.pytorch.git
https://github.com/sovrasov/flops-counter.pytorch

  1. import torchvision.models as models
  2. import torch
  3. from ptflops import get_model_complexity_info
  4. with torch.cuda.device(0):
  5. net = models.densenet161()
  6. macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True,
  7. print_per_layer_stat=True, verbose=True)
  8. print('{:<30} {:<8}'.format('Computational complexity: ', macs))
  9. print('{:<30} {:<8}'.format('Number of parameters: ', params))