Skip to main content

Version License Commit Activity Last Commit Issues Platform

PyTorchLayerViz

PyTorchLayerViz is a Python library designed to assist developers and researchers in visualizing the weights and feature maps of PyTorch models. This tool provides easy-to-use functions to help understand and interpret deep learning models, making it an essential utility for anyone working with PyTorch.

Table of Contents

Installation

To install PyTorchLayerViz, you can use pip:

pip install pytorchlayerviz

Usage

Here is a basic example of how to use PyTorchLayerViz:

from PyTorchLayerViz import get_feature_maps
import matplotlib.pyplot as plt
from PIL import Image
import torch
from torch import nn
from torchvision import datasets, transforms, models
from torchvision.transforms import ToTensor

# Define your model
model = torch.nn.Sequential(
    torch.nn.Conv2d(3, 20, 5),
    torch.nn.ReLU(),
    torch.nn.Conv2d(20, 64, 5),
    torch.nn.ReLU()
)

layers_to_check = [nn.Conv2d] # Define all Layers you want to pass your picture

input_image_path = 'pictures/hamburger.jpg' # Path to your example picture

numpyArr = get_feature_maps(model = model, layers_to_check = layers_to_check, input_image_path = input_image_path, print_image=True) # Call function from pytorchlayerviz

Parameters

  • model (nn.Module) – The PyTorch model whose layers' feature maps you want to visualize. Required.
  • layers_to_check (arr of nn.Module) – List of layer types (e.g., nn.Conv2d) to check for feature maps. Required.
  • input_image_path (str) – Path to the input image file. Required.
  • transform (transforms.Compose, optional) – A function/transform that takes in an image and returns a transformed version. Default is None. Optional.
  • sequential_order (bool, optional) – If True, the layers are visualized in the order they are defined in the model. If false it will first go through the first layer defined in the arrDefault is True. Optional.
  • print_image (bool, optional) – If True the Images are getting printed with matplotlib. Default is False. Optional.

Return The function 'get_feature_maps()` returns the pictures as NumPy Arrays

If transform is none, this will be used:

    transform = transforms.Compose([
    transforms.Resize((224, 224)),  # Resize the image to 224x224 pixels
    transforms.ToTensor(),  # Convert the image to a PyTorch tensor
    ])

If you want to pass your own transform, make sure you resize the image and convert it to a tensor with transforms.ToTensor()

Features

  • Visualize Weights: Easily visualize the weights of each layer in your PyTorch model
  • Visualize Feature Maps: Generate and visualize feature maps for given inputs.
  • Customizable: Flexible options for customizing visualizations.

Advanced Usage

### Extracting Feature Maps with Layer Names

The get_feature_maps function now returns a list of tuples, each containing the feature map and its corresponding layer name. This enhancement allows you to identify which layer each feature map comes from.

feature_maps = get_feature_maps(
    model=model,
    layers_to_check=[nn.Conv2d, nn.MaxPool2d],
    input_image_path='path_to_image.jpg',
    print_image=False
)

for image_array, layer_name in feature_maps:
    print(f"Layer: {layer_name}")
    # Process or display the image as needed

Examples

Example Picture

Example Picture

Code

pretrained_model = models.vgg16(pretrained=True)
input_image_path = 'hamburger.jpg'
layers_to_check= [nn.MaxPool2d]

numpyArr = get_feature_maps(model = pretrained_model, layers_to_check = layers_to_check, input_image_path = input_image_path, sequential_order = False, print_image = True)

Output

Hamburger result Picture

Contributing

I welcome contributions to PyTorchLayerViz! If you'd like to contribute, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add new feature').
  5. Push to the branch (git push origin feature-branch).
  6. Open a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

For any questions, suggestions, or issues, please open an issue on GitHub or contact me.

Release files for PyTorchLayerViz 1.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyTorchLayerViz 1.2.5
File Size Uploaded
pytorchlayerviz-1.2.5.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyTorchLayerViz 1.2.5
File Interpreter ABI Platform
PyTorchLayerViz-1.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / pytorchlayerviz-1.2.5.tar.gz

Download URL pytorchlayerviz-1.2.5.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
385100eaf9de9b6be47fc1ce40f1067e1902a9579f0019ad9e035105a46fd5db
BLAKE2b-256 checksum
How to use checksums
563cdddc6ebecfa4956d72dfe5d459a1ed55bbe00e3a4fe96d6f86ca3639b07b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / PyTorchLayerViz-1.2.5-py3-none-any.whl

Download URL PyTorchLayerViz-1.2.5-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8c4f75428b456f6a2f2b91e0ce53d194ab68b9232d2ccbfacd08a07d4c29650
BLAKE2b-256 checksum
How to use checksums
2ca83684a01335a43b441eb05698a5fb91cfe178c10618900a4c2ea751d64caf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

1.2.5 This release

2 release files

1.2.4

1 release file

1.2.3

1 release file

1.2.2

2 release files

1.2.1

1 release file

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page