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 resnet50from thop import profilemodel = 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 modelsimport torchfrom ptflops import get_model_complexity_infowith 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))
