PyTorch: torchsummary for loaded model

I have the following code to print the model summary of a previously saved model:

import torch                                                                                                                                              

model = torch.load('results/model_best.pth.tar')
print(model) # works

from torchsummary import summary

summary(model, (3, 224, 224)) # error

The print method works okay, but the summary method has the following error:

Traceback (most recent call last):
  File "ModelSummary.py", line XX, in <module>
    summary(model, (3, 224, 224))
  File "anaconda3/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 68, in summary
    model.apply(register_hook)
AttributeError: 'dict' object has no attribute 'apply'

How to rectify this? (I tried the summary method on a model from torchvision database. It worked okay).

Thank you.

Maybe you should instantiate it when you use summary function to show it.
Try like this

model_dict = torch.load('results/model_best.pth.tar')
model = YourModel() #Your Model Class Here
model.load_state_dict(model_dict)
summary(model, (3, 224, 224)) 
1 Like