Pytorch summary of deeplabv3 model

I want to print the model summary of Deeplabv3 model. I have tried it like this, but getting an error

from torchsummary import summary
summary(model,(3,128,128))

I got the following error

AttributeError                            Traceback (most recent call last)

<ipython-input-22-db78be0fb6c0> in <cell line: 1>()
----> 1 summary(model,(3,128,128))

6 frames

/usr/local/lib/python3.10/dist-packages/torchsummary/torchsummary.py in hook(module, input, output)
     24                 ]
     25             else:
---> 26                 summary[m_key]["output_shape"] = list(output.size())
     27                 summary[m_key]["output_shape"][0] = batch_size
     28 

AttributeError: 'collections.OrderedDict' object has no attribute 'size'

Please help.
Thankyou

Pls reply, if anyone knows.

Thanks

torchsummary is not maintained anymore, so use torchinfo:

from torchinfo import summary

model = torchvision.models.segmentation.deeplabv3_resnet50()
summary(model)
# ===========================================================================
# Layer (type:depth-idx)                             Param #
# ===========================================================================
# DeepLabV3                                          --
# ├─IntermediateLayerGetter: 1-1                     --
# │    └─Conv2d: 2-1                                 9,408
# │    └─BatchNorm2d: 2-2                            128
# │    └─ReLU: 2-3                                   --
# │    └─MaxPool2d: 2-4                              --
# │    └─Sequential: 2-5                             --
# │    │    └─Bottleneck: 3-1                        75,008
# │    │    └─Bottleneck: 3-2                        70,400
# │    │    └─Bottleneck: 3-3                        70,400
# │    └─Sequential: 2-6                             --
# │    │    └─Bottleneck: 3-4                        379,392
# │    │    └─Bottleneck: 3-5                        280,064
# │    │    └─Bottleneck: 3-6                        280,064
# │    │    └─Bottleneck: 3-7                        280,064
# │    └─Sequential: 2-7                             --
# │    │    └─Bottleneck: 3-8                        1,512,448
# │    │    └─Bottleneck: 3-9                        1,117,184
# │    │    └─Bottleneck: 3-10                       1,117,184
# │    │    └─Bottleneck: 3-11                       1,117,184
# │    │    └─Bottleneck: 3-12                       1,117,184
# │    │    └─Bottleneck: 3-13                       1,117,184
# │    └─Sequential: 2-8                             --
# │    │    └─Bottleneck: 3-14                       6,039,552
# │    │    └─Bottleneck: 3-15                       4,462,592
# │    │    └─Bottleneck: 3-16                       4,462,592
# ├─DeepLabHead: 1-2                                 --
# │    └─ASPP: 2-9                                   --
# │    │    └─ModuleList: 3-17                       15,206,912
# │    │    └─Sequential: 3-18                       328,192
# │    └─Conv2d: 2-10                                589,824
# │    └─BatchNorm2d: 2-11                           512
# │    └─ReLU: 2-12                                  --
# │    └─Conv2d: 2-13                                5,397
# ===========================================================================
# Total params: 39,638,869
# Trainable params: 39,638,869
# Non-trainable params: 0
# ===========================================================================
1 Like

Thank you Sir, but there is a doubt. Actually I am using the deeplabv3 library with resnet101 backbone. I have 2 classes, and I created a model and saved it as ‘segmodel.pth’ . and if i check the summary like the way you mentioned in the above comment by passing num_classes=2, I got the summary which is different from when I run model.eval().
In model.eval(), it shows the aux_classifier also i.e (FCN head), and in summary it is not showing that aux_classifier.
Moreover the output stride is 16 in summary , while in model.eval it is 8 (according to dilation rates =12,24,36).

and now I checked it like this following

model = deeplabv3_resnet101(num_classes=2)

model.load_state_dict(torch.load('segmodel.pth'))

summary(model, input_size=(batch_size,3,128,128))

and got the following error

RuntimeError                              Traceback (most recent call last)

<ipython-input-45-019b1f66b653> in <cell line: 8>()
      6 
      7 model = deeplabv3_resnet101(num_classes=2)
----> 8 model.load_state_dict(torch.load('segmodel.pth'))
      9 summary(model, input_size=(batch_size,3,128,128))
     10 

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict, assign)
   2150 
   2151         if len(error_msgs) > 0:
-> 2152             raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
   2153                                self.__class__.__name__, "\n\t".join(error_msgs)))
   2154         return _IncompatibleKeys(missing_keys, unexpected_keys)

RuntimeError: Error(s) in loading state_dict for DeepLabV3:
	Unexpected key(s) in state_dict: "aux_classifier.0.weight", "aux_classifier.1.weight", "aux_classifier.1.bias", "aux_classifier.1.running_mean", "aux_classifier.1.running_var", "aux_classifier.1.num_batches_tracked", "aux_classifier.4.weight", "aux_classifier.4.bias".

Please tell me where I am wrong