Skip to main content

A neural network visualization tool

Project description

nnViewer

nnViewer is a Python library designed to provide an intuitive GUI for visualizing the structure and flow of a torch.nn.Module. Whether you're debugging or exploring complex neural networks, nnViewer makes it easier to understand your models.

Installation

Before installing nnViewer, you need to install graphviz and its development libraries. You can install them based on your operating system.

Linux (Ubuntu/Debian)

  1. Install system dependencies:

    sudo apt install graphviz libgraphviz-dev
    
  2. Install the Python package:

    pip install nnViewer
    

macOS

  1. Install system dependencies using Homebrew: If you don't have Homebrew installed, you can install it from here. Then, install graphviz:

    brew install graphviz
    
  2. Install the Python package:

    pip install nnViewer
    

Quick Start

Here's an example of how to use nnViewer with a Hugging Face model:

from transformers import AutoImageProcessor, AutoModel
from PIL import Image
import requests
from nnViewer import wrap_model, run_gui

# Load an image
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)

# Load the model and processor
processor = AutoImageProcessor.from_pretrained('facebook/dinov2-large')
model = AutoModel.from_pretrained('facebook/dinov2-large')

# Prepare the inputs
inputs = processor(images=image, return_tensors="pt")

# Wrap the model to initialize the graph
graph_init = wrap_model(model)

# Perform a forward pass to populate the graph
model(**inputs)

# Launch the GUI to visualize the computational graph
run_gui(graph_init.graph)

Overview

wrap_model(model: nn.Module, fn_to_wrap: Optional[Callable] = None) -> GraphInitializer

The wrap_model function wraps a torch.nn.Module and initializes the computational graph for visualization.

  • Arguments:

    • model (nn.Module): The model to wrap.
    • fn_to_wrap (Callable, optional): A custom forward function to wrap. If not provided, the default forward function of the model is used. The output of the fn_to_wrap should be a tensor with a grad_fn, meaning the output must be a tensor that is part of the computation graph (i.e., a tensor that requires gradients).
  • Returns: A GraphInitializer object that can be used to visualize the model's computational graph.

How to Use

  1. Wrap the model: First, you need to call wrap_model(model) to initialize the computational graph.
  2. Perform a forward pass: Once the model is wrapped, run a forward pass with some sample inputs. This will populate the computational graph.
  3. Visualize the graph: After the forward pass, you can visualize the model’s graph using the run_gui() function.

Navigating the Graph

Once the GUI is running, you can interact with the computational graph in the following ways:

  • Click on a node: Expands the node to show more information about the operation it represents.
  • Double-click on a node: Contracts the node and hides the details, simplifying the view.
  • Right-click on a node: Opens a context menu with options:
    • Get More Information: Shows additional details about the node, including its attributes. You can click on the attributes to view them.
    • Show Computation: Displays the matrices before and after the module for that specific node.

Contributing

Contributions are welcome! If you find any issues or have feature requests, feel free to open a GitHub issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nnviewer-0.1.4.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nnViewer-0.1.4-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file nnviewer-0.1.4.tar.gz.

File metadata

  • Download URL: nnviewer-0.1.4.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for nnviewer-0.1.4.tar.gz
Algorithm Hash digest
SHA256 de1e119b0382ba32ec53eba6c2e59f17872cd477ee58650f677c8674bcc10577
MD5 dd23c086c3f9e58f89d1226a3480dc2f
BLAKE2b-256 4d9d591b6898b275431f583a74c2a23e7c3c8546da02b6b54988b753edcfdfe0

See more details on using hashes here.

File details

Details for the file nnViewer-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: nnViewer-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for nnViewer-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e64d2e555f14413c455165d12f80e753a79e976fc128a31feba1299db59f1c3d
MD5 57fc1d8eee24c1ea860c9aec680f43eb
BLAKE2b-256 d7060c9a10ce14a5f7503e95d154e9564f99bf4b1c71582e8a715bcfb375aad4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page