onnxruntime/python/_downloads/plot_pipeline.ipynb
2019-12-20 13:35:58 -08: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": [
"\nDraw a pipeline\n===============\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\nRetrieve a model in JSON format\n+++++++++++++++++++++++++++++++\n\nThat's the most simple way.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from onnxruntime.datasets import get_example\nexample1 = get_example(\"mul_1.onnx\")\n\nimport onnx\nmodel = onnx.load(example1) # model is a ModelProto protobuf message\n\nprint(model)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw a model with ONNX\n++++++++++++++++++++++\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\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 GetPydotGraph, GetOpNodeProducer\npydot_graph = GetPydotGraph(model.graph, name=model.graph.name, rankdir=\"LR\",\n node_producer=GetOpNodeProducer(\"docstring\"))\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\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\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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}