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:Microsoft Onnxruntime PlotPipeline

From Leeroopedia
Revision as of 15:47, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Onnxruntime_PlotPipeline.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Documentation, Python, Visualization
Last Updated 2026-02-10 04:00 GMT

Overview

A Python example script demonstrating how to visualize an ONNX model graph as a rendered pipeline diagram using the onnx library's net_drawer utilities.

Description

The `plot_pipeline.py` script is a Sphinx gallery documentation example that shows two ways to inspect an ONNX model:

1. JSON/text representation: Loads the `mul_1.onnx` model via `onnx.load()` and prints the `ModelProto` protobuf message, which shows the full model structure including graph nodes, inputs, outputs, and initializers.

2. Graphical visualization: Demonstrates a visual pipeline rendering workflow:

  - Loads the model using `ModelProto.ParseFromString()` from raw bytes.
  - Converts the model graph to a pydot graph using `onnx.tools.net_drawer.GetPydotGraph` with left-to-right layout (`rankdir="LR"`) and `GetOpNodeProducer("docstring")` for node labeling.
  - Writes the graph to a DOT file (`graph.dot`).
  - Renders the DOT file to PNG using the system `dot` command (`dot -O -Tpng graph.dot`).
  - Displays the resulting image using matplotlib (`plt.imread` and `plt.imshow`).

Usage

Use this script as a reference for creating visual representations of ONNX model architectures. This is useful for debugging model structure and for documentation purposes.

Code Reference

Source Location

Signature

from onnxruntime.datasets import get_example
import onnx
from onnx import ModelProto
from onnx.tools.net_drawer import GetOpNodeProducer, GetPydotGraph

example1 = get_example("mul_1.onnx")
model = onnx.load(example1)

# Alternative loading
model = ModelProto()
with open(example1, "rb") as fid:
    content = fid.read()
    model.ParseFromString(content)

pydot_graph = GetPydotGraph(
    model.graph, name=model.graph.name, rankdir="LR",
    node_producer=GetOpNodeProducer("docstring"))
pydot_graph.write_dot("graph.dot")

Import

from onnxruntime.datasets import get_example
import onnx
from onnx import ModelProto
from onnx.tools.net_drawer import GetOpNodeProducer, GetPydotGraph
import matplotlib.pyplot as plt
import os

I/O Contract

Step Input Output Description
Load model ONNX file path ModelProto Loads model for inspection
GetPydotGraph model.graph, name, rankdir, node_producer pydot Graph Converts ONNX graph to pydot representation
write_dot pydot Graph DOT file Writes graph to Graphviz DOT format
dot command DOT file PNG image Renders graph visualization as image
plt.imshow PNG image Display Shows the rendered graph

Usage Examples

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

model = onnx.load("my_model.onnx")
pydot_graph = GetPydotGraph(
    model.graph,
    name=model.graph.name,
    rankdir="LR",
    node_producer=GetOpNodeProducer("docstring"))
pydot_graph.write_dot("my_model.dot")

import os
os.system("dot -O -Tpng my_model.dot")

Related Pages

Page Connections

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