THOP
https://github.com/Lyken17/pytorch-OpCounter
pip install thop(推荐用这个) 或者 pip install —upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git(这个方法需要同时安装pytorch)
from torchvision.models import resnet50
from thop import profile
model = resnet50()
input = torch.randn(1, 3, 224, 224)
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
import torchvision.models as models
import torch
from ptflops import get_model_complexity_info
with torch.cuda.device(0):
net = models.densenet161()
macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True,
print_per_layer_stat=True, verbose=True)
print('{:<30} {:<8}'.format('Computational complexity: ', macs))
print('{:<30} {:<8}'.format('Number of parameters: ', params))