How to Use the New and Updated torchinfo for Keras-Style Model Summary in PyTorch

Jan 26, 2021 | Data Science

In the world of deep learning, understanding the architecture of your models is paramount for effective debugging and optimization. Keras offers a sleek API for visualizing model summaries, but what if you’re working in PyTorch? Look no further! The torchinfo package serves as your trusty guide, providing a Keras-like summary for your PyTorch models. Here’s how you can leverage it!

Getting Started with torchinfo

To utilize torchinfo in your PyTorch projects, follow these simple steps:

  • Installation: Open your terminal and install torchinfo via pip or clone the repository:
    • pip install torchsummary
    • git clone https://github.com/sksq96/pytorch-summary
  • Usage: Import the summary function from torchsummary and invoke it:
    • from torchsummary import summary
    • summary(your_model, input_size=(channels, H, W))
  • Input Size: Make sure to provide the appropriate input_size to facilitate a successful forward pass through your network.

Code Examples

1. Convolutional Neural Network for MNIST

Here’s a basic CNN model for the MNIST dataset:


import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Net().to(device)
summary(model, (1, 28, 28))

2. VGG16 Model

If you’re looking to implement a pre-trained model like VGG16, here’s how:


import torch
from torchvision import models
from torchsummary import summary

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
vgg = models.vgg16().to(device)
summary(vgg, (3, 224, 224))

3. Handling Multiple Inputs

torchinfo also supports models with multiple inputs:


import torch
import torch.nn as nn
from torchsummary import summary

class SimpleConv(nn.Module):
    def __init__(self):
        super(SimpleConv, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
        )

    def forward(self, x, y):
        x1 = self.features(x)
        x2 = self.features(y)
        return x1, x2

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleConv().to(device)
summary(model, [(1, 16, 16), (1, 28, 28)])

Understanding torchinfo’s Output

The output of the summary function looks like a blueprint of your model, detailing each layer, output shape, and number of parameters.

Analogy: Think of it as Building a House

Imagine you’re constructing a house. Each layer in your model represents a room (like a living room, kitchen, etc.). Just as you need to know the dimensions (output shape) and materials (number of parameters) for each room, torchinfo gives you this critical information for each layer in your model. If one room (layer) is too small or under-equipped (lacking parameters), it could lead to inefficiencies in your entire house (model).

Troubleshooting

If you encounter issues using torchinfo, consider the following troubleshooting tips:

  • Ensure that the input size matches your model’s expectations.
  • If using CUDA, verify that your GPU is available and properly configured.
  • Check for any discrepancies in layer definitions that might confuse the summary generation.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

References

  • For the idea behind this package, refer to this PyTorch issue.
  • Special thanks to contributors such as @ncullen93 and @HTLife.
  • For model size estimation, check details here.

License

The torchinfo package is MIT-licensed.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox