Suggestion: add number of parameters in torchvision models comparison

In https://pytorch.org/docs/stable/torchvision/models.html ,
When comparing the models in the tables by error rate \ IoU, I think it can be beneficial to also add the number of learned parameters.
It will assist in choosing the right model given the performance\runtime trade-off.

I second this. Would be very handy

1 Like

I wrote a snippet for it.

import torchvision.models as models
from types import FunctionType
def calculate_num_of_learned_params(model):
    cnt = 0
    for param in model.parameters():
        if param.requires_grad:
            cnt += param.numel()
    return cnt

for model_name in dir(models):
    if model_name[0].islower():
        attr = getattr(models, model_name)
        if isinstance(attr, FunctionType):
            print(model_name, '\t\t\t', calculate_num_of_learned_params(attr()))

Results:

alexnet 			 61100840
densenet121 			 7978856
densenet161 			 28681000
densenet169 			 14149480
densenet201 			 20013928
googlenet 			 13004888
inception_v3 			 27161264
mobilenet_v2 			 3504872
resnet101 			 44549160
resnet152 			 60192808
resnet18 			 11689512
resnet34 			 21797672
resnet50 			 25557032
resnext101_32x8d 			 88791336
resnext50_32x4d 			 25028904
shufflenet_v2_x0_5 			 1366792
shufflenet_v2_x1_0 			 2278604
shufflenet_v2_x1_5 			 3503624
shufflenet_v2_x2_0 			 7393996
squeezenet1_0 			 1248424
squeezenet1_1 			 1235496
vgg11 			 132863336
vgg11_bn 			 132868840
vgg13 			 133047848
vgg13_bn 			 133053736
vgg16 			 138357544
vgg16_bn 			 138365992
vgg19 			 143667240
vgg19_bn 			 143678248

1 Like

Great :slight_smile: Was hard to count the zeros so I made a human readable tweak

import torchvision.models as models
from types import FunctionType

def calculate_num_of_learned_params(model):
    cnt = 0
    for param in model.parameters():
        if param.requires_grad:
            cnt += param.numel()
    return cnt

def human_readable(n_params):
    if n_params >= 1e6:
        return '{:.2f} million'.format(n_params/1e6)
    if n_params >= 1e3:
        return '{:.2f} thousands'.format(n_params/1e3)
    else:
        return n_params

for model_name in dir(models):
    if model_name[0].islower():
        attr = getattr(models, model_name)
        if isinstance(attr, FunctionType):
            n_params =  calculate_num_of_learned_params(attr())
            print(model_name, '\t\t\t', human_readable(n_params))

Edit: torchvision 0.3.0

alexnet                61.10 million
densenet121            7.98 million
densenet161            28.68 million
densenet169            14.15 million
densenet201            20.01 million
googlenet              13.00 million
inception_v3           27.16 million
mobilenet_v2           3.50 million
resnet101              44.55 million
resnet152              60.19 million
resnet18               11.69 million
resnet34               21.80 million
resnet50               25.56 million
resnext101_32x8d       88.79 million
resnext50_32x4d        25.03 million
shufflenet_v2_x0_5     1.37 million
shufflenet_v2_x1_0     2.28 million
shufflenet_v2_x1_5     3.50 million
shufflenet_v2_x2_0     7.39 million
squeezenet1_0          1.25 million
squeezenet1_1          1.24 million
vgg11                  132.86 million
vgg11_bn               132.87 million
vgg13                  133.05 million
vgg13_bn               133.05 million
vgg16                  138.36 million
vgg16_bn               138.37 million
vgg19                  143.67 million
vgg19_bn               143.68 million

Great! I noticed you have some missing models, probably due to an old torchvision version.

Anyway I shortened one of the functions:

def calculate_num_of_learned_params(model):
    return sum([param.numel() for param in model.parameters() if param.requires_grad])
1 Like