onnxruntime/docs/python/inference/examples/plot_pipeline.py

71 lines
1.6 KiB
Python
Raw Normal View History

2018-11-20 00:48:22 +00:00
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
Draw a pipeline
===============
There is no other way to look into one model stored
in ONNX format than looking into its node with
2018-11-20 00:48:22 +00:00
*onnx*. This example demonstrates
how to draw a model and to retrieve it in *json*
format.
.. contents::
:local:
Retrieve a model in JSON format
+++++++++++++++++++++++++++++++
That's the most simple way.
"""
from onnxruntime.datasets import get_example
example1 = get_example("mul_1.onnx")
2018-11-20 00:48:22 +00:00
import onnx
2018-11-20 00:48:22 +00:00
model = onnx.load(example1) # model is a ModelProto protobuf message
print(model)
2018-11-20 00:48:22 +00:00
#################################
# Draw a model with ONNX
# ++++++++++++++++++++++
# We use `net_drawer.py <https://github.com/onnx/onnx/blob/main/onnx/tools/net_drawer.py>`_
2018-11-20 00:48:22 +00:00
# included in *onnx* package.
# We use *onnx* to load the model
# in a different way than before.
from onnx import ModelProto
2018-11-20 00:48:22 +00:00
model = ModelProto()
with open(example1, "rb") as fid:
2018-11-20 00:48:22 +00:00
content = fid.read()
model.ParseFromString(content)
###################################
# We convert it into a graph.
from onnx.tools.net_drawer import GetOpNodeProducer, GetPydotGraph
pydot_graph = GetPydotGraph(
model.graph, name=model.graph.name, rankdir="LR", node_producer=GetOpNodeProducer("docstring")
)
2018-11-20 00:48:22 +00:00
pydot_graph.write_dot("graph.dot")
#######################################
# Then into an image
import os
os.system("dot -O -Tpng graph.dot")
2018-11-20 00:48:22 +00:00
################################
# Which we display...
import matplotlib.pyplot as plt
2018-11-20 00:48:22 +00:00
image = plt.imread("graph.dot.png")
plt.imshow(image)