Torchsummary

1.5.1 · active · verified Fri Apr 17

`torchsummary` provides a Keras-like `model.summary()` functionality for PyTorch models, displaying layer names, output shapes, parameter counts, and trainable parameters. It helps in quickly understanding the architecture and memory footprint of a neural network. The current version is 1.5.1, with releases occurring as needed for bug fixes and minor enhancements rather than a strict schedule.

Common errors

Warnings

Install

Imports

Quickstart

This example defines a simple Convolutional Neural Network and uses `torchsummary.summary` to print its architecture, output shapes, and parameter counts. Note the importance of providing a correct `input_size` tuple (excluding batch dimension) and ensuring the model is on the specified device.

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

# Define a simple model
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=2)
        self.flatten = nn.Flatten()
        # Calculate input size for linear layer: (28 - 5 + 1) / 2 = 12
        # So, 10 channels * 12 * 12 pixels = 1440
        self.fc1 = nn.Linear(10 * 12 * 12, 50)
        self.relu2 = nn.ReLU()
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = self.pool1(self.relu1(self.conv1(x)))
        x = self.flatten(x)
        x = self.relu2(self.fc1(x))
        x = self.fc2(x)
        return x

model = SimpleCNN()

# Determine device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Print summary for an input image of 1 channel, 28x28 pixels
# input_size should be (channels, height, width) *without* the batch dimension
print(summary(model, input_size=(1, 28, 28), device=str(device)))

view raw JSON →