Check the number of parameters from state_dict in pytorch
SO has an answer about how to check the total # of params from the model:
pytorch_total_params = sum(p.numel() for p in model.parameters())

However, how does one check the total # of params from
the state_dict?

state_dict = torch.load(model_path, map_location='cpu')?

You can count the number of saved entries in the state_dict:

sum(p.numel() for p in state_dict.values())


However, there's a snag here: a state_dict stores both parameters and persistent buffers (e.g., BatchNorm's running mean and var). There's no way (AFAIK) to tell them apart from the state_dict itself, you'll need to load them into the model and use sum(p.numel() for p in model.parameters() to count only the parameters.

For instance, if you checkout resnet50

from torchvision.models import resnet50
model = resnet50(pretrained=True)
state_dict = torch.load('~/.torch/models/resnet50-19c8e357.pth')
num_parameters = sum(p.numel() for p in model.parameters())
num_state_dict = sum(p.numel() for p in state_dict.values())
print('num parameters = {}, stored in state_dict = {}, diff = {}'.format(num_parameters, num_state_dict, num_state_dict - num_parameters))


Resulting with


num parameters = 25557032, stored in state_dict = 25610152, diff = 53120



As you can see there can be quite a gap between the two values.
