需要使用官方库THOP: PyTorch-OpCounter
GitHub 地址:https://github.com/Lyken17/pytorch-OpCounter
1.安装THOP:
pip install thop # orpip install --upgrade git+https://github.com/Lyken17/pytorch-OpCounter.git2.使用方法:
from torchvision.models import resnet50from thop import profile# 增加可读性from thop import clever_format# 可替换为自己的模型及输入model = resnet50()input = torch.randn(1, 3, 224, 224)flops, params = profile(model, inputs=(input, ))flops, params = clever_format([flops, params], "%.3f")