Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx Net Drawer

From Leeroopedia


Knowledge Sources
Domains Visualization, Debugging
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for visualizing ONNX neural network graphs by converting them into DOT format for rendering as diagrams, provided by the ONNX library.

Description

The net_drawer module converts ONNX computation graphs into DOT format files that can be rendered as visual diagrams (SVG, PNG, etc.) using the Graphviz toolset. The implementation is borrowed from the Caffe2 project.

GetOpNodeProducer is a factory function that returns a callable (_NodeProducer) for creating pydot node representations of ONNX operations. Each node displays the operator name, operator type, operation ID, and lists all inputs and outputs. When embed_docstring is enabled, the operator's doc_string is embedded as a JavaScript alert URL, which is useful for interactive SVG output. The factory accepts additional keyword arguments that are passed through to pydot.Node for styling.

GetPydotGraph is the main graph conversion function. It takes an ONNX GraphProto and produces a pydot.Dot graph object. The function iterates over all nodes in the graph, creating operation nodes (styled as green filled boxes by default) and tensor/blob nodes (styled as octagons). It tracks node names and counts to handle duplicate tensor names correctly. Edges are added to represent data flow between tensors and operations. The function supports customizable rank direction (default "LR" for left-to-right) and an optional custom node producer.

The module also provides a main entry point for command-line usage, accepting an input .pb file (serialized ModelProto), output .dot file path, rank direction, and an option to embed docstrings.

Usage

Use this module to create visual representations of ONNX model architectures for documentation, debugging, or presentation purposes. It can be invoked from the command line or used programmatically. The resulting DOT files can be converted to various image formats using Graphviz tools (e.g., dot -Tsvg output.dot -o output.svg).

Code Reference

Source Location

Signature

def GetOpNodeProducer(
    embed_docstring: bool = False, **kwargs: Any
) -> _NodeProducer

def GetPydotGraph(
    graph: GraphProto,
    name: str | None = None,
    rankdir: str = "LR",
    node_producer: _NodeProducer | None = None,
    embed_docstring: bool = False,
) -> pydot.Dot

def main() -> None

Import

from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer

I/O Contract

Inputs (GetPydotGraph)

Name Type Required Description
graph GraphProto Yes The ONNX graph to visualize
name str or None No Name for the pydot graph (default: None)
rankdir str No Rank direction for layout: "LR" (left-to-right), "TB" (top-to-bottom), etc. (default: "LR")
node_producer _NodeProducer or None No Custom callable for producing pydot nodes from ONNX operators (default: uses GetOpNodeProducer with OP_STYLE)
embed_docstring bool No Whether to embed operator docstrings as JavaScript alerts in the output (default: False)

Inputs (GetOpNodeProducer)

Name Type Required Description
embed_docstring bool No Whether to embed operator docstrings (default: False)
**kwargs Any No Additional keyword arguments passed to pydot.Node for styling (e.g., shape, color, style, fontcolor)

Outputs

Name Type Description
pydot_graph pydot.Dot A pydot graph object representing the ONNX computation graph, which can be written to DOT, SVG, PNG, and other formats
node_producer _NodeProducer A callable that takes (NodeProto, int) and returns a pydot.Node (from GetOpNodeProducer)

Default Styles

Element Style Properties
Operation nodes (OP_STYLE) shape="box", color="#0F9D58" (green), style="filled", fontcolor="#FFFFFF" (white)
Blob/Tensor nodes (BLOB_STYLE) shape="octagon"

Command-Line Interface

# Convert an ONNX model to DOT format
python -m onnx.tools.net_drawer --input model.pb --output model.dot

# With custom rank direction and embedded docstrings
python -m onnx.tools.net_drawer --input model.pb --output model.dot --rankdir TB --embed_docstring

Usage Examples

import onnx
from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer

# Load model and create visualization
model = onnx.load("model.onnx")
pydot_graph = GetPydotGraph(
    model.graph,
    name=model.graph.name,
    rankdir="LR",
)

# Write to DOT file
pydot_graph.write_dot("model.dot")

# Write directly to SVG
pydot_graph.write_svg("model.svg")

# Use custom node styling
custom_style = {"shape": "ellipse", "color": "blue", "style": "filled"}
node_producer = GetOpNodeProducer(embed_docstring=True, **custom_style)
pydot_graph = GetPydotGraph(model.graph, node_producer=node_producer)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment