PyTorch Architecture Plotter
This is a professional, modular utility for visualizing the architecture of a PyTorch nn.Module. It generates a clear, longitudinal diagram where the size and color of each block (representing a layer) are scaled according to its input shape (channels and spatial dimensions).
The package is designed for clarity, reusability, and professional integration into any PyTorch project.
Features
-
Modular Design: Code is separated into
visualization,arch_extractor, andplottermodules. -
Dynamic Shape Extraction: Performs a real forward pass to accurately determine input and output shapes for every layer, correctly handling transitions like Flattening before Linear layers.
-
3D Visualization: Layers are rendered as 3D cuboids for enhanced visual appeal and depth perception.
-
Scaling Heuristic: Block width is scaled by the number of channels and height is scaled logarithmically by the spatial dimensions ($H \times W$), providing an intuitive representation of feature map changes.
-
Configurable: Easily skip layers like
ReLUto declutter the diagram.
Installation
To use this utility, you should have PyTorch and Matplotlib installed.
-
Clone or download the
arch_plotterdirectory. -
Install the package in editable mode from the root directory:
pip install -e .
Usage Example
You can import the main function and use it on any standard PyTorch nn.Module.
import torch.nn as nn
from torchvisualizer import plot_architecture
class SimpleCNN(nn.Module):
def __init__(self):
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(16, 32, 3, padding=1),
nn.ReLU(),
)
self.classifier = nn.Sequential(
nn.Linear(32 * 8 * 8, 10), # Assuming 32x32 input
)
def forward(self, x):
x = self.features(x)
x = x.flatten(1)
x = self.classifier(x)
return x
# Instantiate the model
model = SimpleCNN()
# Generate and save the diagram
plot_architecture(
model=model,
input_shape=(3, 32, 32), # (C, H, W) of the input image
save_path="simple_cnn_diagram.png",
skip_layers=["ReLU"] # Skip ReLU for a cleaner look
)
# Output: Architecture diagram saved to: simple_cnn_diagram.png
🔧 API Reference
plot_architecture(model, input_shape, batch_size=1, save_path="architecture.png", skip_layers=None)
| Parameter | Type | Default | Description |
|---|---|---|---|
model |
torch.nn.Module |
- | The PyTorch model to visualize. |
input_shape |
tuple |
(3, 227, 227) |
The expected (C, H, W) dimensions of the input tensor. |
batch_size |
int |
1 |
Batch size to use for the dummy forward pass. |
save_path |
str |
"architecture.png" |
The filename for the output image. |
skip_layers |
list[str] |
["ReLU"] |
List of layer class names to exclude from the plot. |
🛠️ Development & Customization
The core visualization logic is found in:
arch_plotter/visualization.py: Modify drawing style, color schemes, and size scaling heuristics here.arch_plotter/arch_extractor.py: Customize how shapes are extracted, especially for complex or custom layer types.
Metadata
Release files for torchvisualizer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| torchvisualizer-0.1.0.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchvisualizer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / torchvisualizer-0.1.0.tar.gz
| Download URL | torchvisualizer-0.1.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a076127db74b291eaac831acd4649b7179460e7df8dda90bbe878d9aba3ad2db
|
|
BLAKE2b-256 checksum How to use checksums |
67697a9f109d0be8315cf7400f12b7c04726907939056819fff74c52949e0aa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|
Release files / torchvisualizer-0.1.0-py3-none-any.whl
| Download URL | torchvisualizer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cabf57fd305410e41aecb237e4a81debaee23d35b3c03f32221345b286cc7c80
|
|
BLAKE2b-256 checksum How to use checksums |
067d2a438e5a08741a86ba04494246295f3040705953f5e3f16b7005131dc96c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.0
|