How to get a torch summary of a pre trained object detection model in pytorch?

Checked out

sksq96/pytorch-summary

Tried

import torch
from torchvision import models
from torchsummary import summary

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
loaded_model = model().to(device)

summary(loaded_model, (3, 224, 224))

I get his error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-39b645f0f2fc> in <module>()
      5 model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False)
      6 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
----> 7 loaded_model = model().to(device)
      8 
      9 summary(loaded_model, (3, 224, 224))

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

TypeError: forward() missing 1 required positional argument: 'images'

What is going wrong? How do I correct it?

Should be:

model.to(device)

Then just run:

summary(loaded_model)

Output:
=================================================================
Layer (type:depth-idx)                   Param #
=================================================================
├─GeneralizedRCNNTransform: 1-1          --
├─BackboneWithFPN: 1-2                   --
|    └─IntermediateLayerGetter: 2-1      --
|    |    └─Conv2d: 3-1                  (9,408)
|    |    └─FrozenBatchNorm2d: 3-2       --
|    |    └─ReLU: 3-3                    --
|    |    └─MaxPool2d: 3-4               --
|    |    └─Sequential: 3-5              (212,992)
|    |    └─Sequential: 3-6              1,212,416
|    |    └─Sequential: 3-7              7,077,888
|    |    └─Sequential: 3-8              14,942,208
|    └─FeaturePyramidNetwork: 2-2        --
|    |    └─ModuleList: 3-9              984,064
|    |    └─ModuleList: 3-10             2,360,320
|    |    └─LastLevelMaxPool: 3-11       --
├─RegionProposalNetwork: 1-3             --
|    └─AnchorGenerator: 2-3              --
|    └─RPNHead: 2-4                      --
|    |    └─Conv2d: 3-12                 590,080
|    |    └─Conv2d: 3-13                 771
|    |    └─Conv2d: 3-14                 3,084
├─RoIHeads: 1-4                          --
|    └─MultiScaleRoIAlign: 2-5           --
|    └─TwoMLPHead: 2-6                   --
|    |    └─Linear: 3-15                 12,846,080
|    |    └─Linear: 3-16                 1,049,600
|    └─FastRCNNPredictor: 2-7            --
|    |    └─Linear: 3-17                 93,275
|    |    └─Linear: 3-18                 373,100
=================================================================
Total params: 41,755,286
Trainable params: 41,532,886
Non-trainable params: 222,400
=================================================================
import torch
from torchsummary import summary

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
loaded_model = model.to(device)

summary(loaded_model)

error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-61-dc22bb1d5de8> in <module>()
      6 loaded_model = model.to(device)
      7 
----> 8 summary(loaded_model)

TypeError: summary() missing 1 required positional argument: 'input_size'

What version of torchsummary are you using? EDIT: In most conventional setups, you can check it by running, in a terminal: pip list and looking at the number next to torch-summary.

I did
!pip show torchsummary

I got

Name: torchsummary
Version: 1.5.1
Summary: Model summary in PyTorch similar to `model.summary()` in Keras
Home-page: https://github.com/sksq96/pytorch-summary
Author: Shubham Chandel @sksq96
Author-email: shubham.zeez@gmail.com
License: UNKNOWN
Location: /usr/local/lib/python3.7/dist-packages
Requires: 
Required-by: 

PS: Using google colab

Hmm, it looks like you might be using torchsummary (one word) rather than torch-summary (two words). The one you’re using looks like it was last updated in 2018, the other one was updated in 2020. Looking at the repo, it looks like they’ve now moved over to torchinfo.

The readme for torchinfo presents this example use:

from torchinfo import summary

model = ConvNet()
batch_size = 16
summary(model, input_size=(batch_size, 1, 28, 28))

So perhaps you try installing torchinfo and using it like so:

from torchinfo import summary
summary(loaded_model, input_size=(1, 3, 224, 224))
4 Likes