How to get Model Summary in PyTorch

Model summary: number of trainable and non-trainable parameters, layer names, kernel size, all inclusive.

Siladittya Manna
The Owl
4 min readAug 25, 2022

--

Unlike Keras, there is no method in PyTorch nn.Module class to calculate the number of trainable and non-trainable parameters in a model and show the model summary layer-wise. Studying several posts on Stack Overflow, I have figured out three ways to do that. In this post, I am going to summarize those three methods I know of to calculate the number of trainable and non-trainable parameters in a PyTorch model.

1. Manually

There does exist a simple method using numel
(Ref:
Stack Overflow )

For ResNet18, the above function outputs the number of parameters like this

So, the output is in a parameter-wise manner, and we can see the trainable parameters for each parameter that exists in the model.

2. Using torchsummary

Now, there exists one library called torchsummary, which can be used to print out the trainable and non-trainable parameters in a Keras-like manner for PyTorch models. It is very user-friendly with minimal syntax. The current version is 1.5.1 and it is installed by default when installing the torch library.
It is available here.

You can install it using

if not already installed.

Import

Suppose the model you are using is a simple ResNet18 model

Then, the model summary is obtained by

There is another argument ‘device’ which is set to ‘cuda’ by default.

The output will look like this

Now, if your model looks something like this where the base model has several branches and each takes a different input,

That is the input to your model is a list of tensors, then to obtain the model summary,

The output will be similar to the previous one but will be a bit confusing since the torchsummary library squeezes the summary of each constituent ResNet module into one single summary file without any proper distinguishable boundary between the summaries of two consecutive modules.

3. Using torchinfo

previously torch-summary

It may look like it is the same library as the previous one. But it is not. In fact, it is the best of all three methods I am showing here, in my opinion. The current version is 1.7.0. It is available here.

You can install it using

This library also has a function named summary. But it comes with many more options and that is what makes it better. The arguments are model (nn.module), input_size (Sequence of Sizes), input_data (Sequence of Tensors), batch_dim (int), cache_forward_pass (bool), col_names (Iterable[str]), col_width (int), depth (int), device (torch.Device), dtypes (List[torch.dtype]), mode (str), row_settings (Iterable[str]), verbose (int) and **kwargs.

Using torchinfo.summary we can get a lot of information by giving currently supported options from (“input_size”, “output_size”, “num_params”, “kernel_size”, “mult_adds”, “trainable” ) as input for the argument col_names.

If we run the following line of code

Change the verbose to 1 if not using Jupyter Notebook or Google Colab.

The output of the above code snippet looks like this

A stark difference is observed when we print the summary of Model using torchinfo.summary. The following line of code

yields the following output

The default of the depth argument is 3 in torchinfo.summary. We can see that the constituent ResNet modules are easily distinguishable and the constituent modules are presented in a hierarchical fashion. This is what makes this library better for these types of models in my opinion.

Clap and share if you like this post. Follow for more posts like this.

--

--

Siladittya Manna
The Owl

Senior Research Fellow @ CVPR Unit, Indian Statistical Institute, Kolkata || Research Interest : Computer Vision, SSL, MIA. || https://sadimanna.github.io