Implementation:Microsoft Onnxruntime PlotPipeline
| 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
- Repository: Microsoft_Onnxruntime
- File: docs/python/examples/plot_pipeline.py
- Lines: 1-67
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")