onnxruntime/docs/api/python/downloads/2dbd202de70c8b6a394b8a1c7c1e12b8/plot_pipeline.ipynb
github-actions[bot] 5e0a75ce4e
[Automated]: Update Python API docs (#12434)
Update Python API docs to commit 84f69d3

Co-authored-by: snnn <snnn@users.noreply.github.com>
2022-08-03 10:24:42 -07:00

126 lines
No EOL
3.4 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Draw a pipeline\n\nThere is no other way to look into one model stored\nin ONNX format than looking into its node with \n*onnx*. This example demonstrates\nhow to draw a model and to retrieve it in *json*\nformat.\n\n## Retrieve a model in JSON format\n\nThat's the most simple way.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from onnxruntime.datasets import get_example\n\nexample1 = get_example(\"mul_1.onnx\")\n\nimport onnx\n\nmodel = onnx.load(example1) # model is a ModelProto protobuf message\n\nprint(model)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Draw a model with ONNX\nWe use [net_drawer.py](https://github.com/onnx/onnx/blob/master/onnx/tools/net_drawer.py)\nincluded in *onnx* package.\nWe use *onnx* to load the model\nin a different way than before.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from onnx import ModelProto\n\nmodel = ModelProto()\nwith open(example1, \"rb\") as fid:\n content = fid.read()\n model.ParseFromString(content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We convert it into a graph.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from onnx.tools.net_drawer import GetOpNodeProducer, GetPydotGraph\n\npydot_graph = GetPydotGraph(\n model.graph, name=model.graph.name, rankdir=\"LR\", node_producer=GetOpNodeProducer(\"docstring\")\n)\npydot_graph.write_dot(\"graph.dot\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then into an image\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os\n\nos.system(\"dot -O -Tpng graph.dot\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which we display...\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n\nimage = plt.imread(\"graph.dot.png\")\nplt.imshow(image)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}