ptflops
raw JSON → 0.7.5 verified Mon Apr 27 auth: no python
A Flops counter for neural networks in PyTorch. It computes the number of floating point operations (FLOPs) and parameters of a model. The current version is 0.7.5, with a slow release cadence.
pip install ptflops Common errors
error from ptflops.flops_counter import get_model_complexity_info ModuleNotFoundError: No module named 'ptflops.flops_counter' ↓
cause Old import path no longer exists after ptflops 0.7.
fix
Use: from ptflops import get_model_complexity_info
error TypeError: get_model_complexity_info() got an unexpected keyword argument 'input_res' ↓
cause The function signature changed in version 0.7, parameter renamed to 'input_shape'.
fix
Replace 'input_res' with 'input_shape' as a tuple, e.g., input_shape=(3, 224, 224).
Warnings
deprecated Importing from ptflops.flops_counter directly is deprecated. Use from ptflops import get_model_complexity_info instead. ↓
fix Change import to 'from ptflops import get_model_complexity_info'.
gotcha The function returns MACs (multiply-accumulate operations), not FLOPs. MACs are roughly half of FLOPs. ↓
fix Multiply the returned MACs by 2 to get FLOPs if needed.
gotcha For models with batch normalization, the flops count may be inaccurate if the model is not in eval mode. ↓
fix Set model.eval() before calling get_model_complexity_info.
breaking The function signature changed in version 0.7. The parameter 'input_res' was removed and replaced with 'input_shape' (tuple). ↓
fix Pass input_shape=(3, 224, 224) instead of input_res=224.
Imports
- get_model_complexity_info wrong
from ptflops.flops_counter import get_model_complexity_infocorrectfrom ptflops import get_model_complexity_info
Quickstart
import torch
from ptflops import get_model_complexity_info
import torchvision.models as models
net = models.resnet18()
macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, print_per_layer_stat=True)
print('{:<30} {:<8}'.format('Computational complexity: ', macs))
print('{:<30} {:<8}'.format('Number of parameters: ', params))