onnxruntime/docs/api/python/auto_examples/plot_pipeline.html

277 lines
13 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
2020-08-13 02:12:50 +00:00
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-156955408-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-156955408-1');
</script>
<title>Draw a pipeline &#8212; ONNX Runtime 1.14.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../static/alabaster.css" />
<link rel="stylesheet" type="text/css" href="../static/graphviz.css" />
<link rel="stylesheet" type="text/css" href="../static/sg_gallery.css" />
<link rel="stylesheet" type="text/css" href="../static/sg_gallery-binder.css" />
<link rel="stylesheet" type="text/css" href="../static/sg_gallery-dataframe.css" />
<link rel="stylesheet" type="text/css" href="../static/sg_gallery-rendered-html.css" />
<script data-url_root="../" id="documentation_options" src="../static/documentation_options.js"></script>
<script src="../static/jquery.js"></script>
<script src="../static/underscore.js"></script>
<script src="../static/_sphinx_javascript_frameworks_compat.js"></script>
<script src="../static/doctools.js"></script>
<script src="../static/sphinx_highlight.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Load and predict with ONNX Runtime and a very simple model" href="plot_load_and_predict.html" />
<link rel="prev" title="Gallery of examples" href="index.html" />
<link rel="stylesheet" href="../static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">Note</p>
<p>Click <a class="reference internal" href="#sphx-glr-download-auto-examples-plot-pipeline-py"><span class="std std-ref">here</span></a>
to download the full example code</p>
</div>
<section class="sphx-glr-example-title" id="draw-a-pipeline">
<span id="sphx-glr-auto-examples-plot-pipeline-py"></span><h1>Draw a pipeline<a class="headerlink" href="#draw-a-pipeline" title="Permalink to this heading"></a></h1>
<p>There is no other way to look into one model stored
in ONNX format than looking into its node with
<em>onnx</em>. This example demonstrates
how to draw a model and to retrieve it in <em>json</em>
format.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#retrieve-a-model-in-json-format" id="id1">Retrieve a model in JSON format</a></p></li>
<li><p><a class="reference internal" href="#draw-a-model-with-onnx" id="id2">Draw a model with ONNX</a></p></li>
</ul>
</div>
<section id="retrieve-a-model-in-json-format">
<h2><a class="toc-backref" href="#id1">Retrieve a model in JSON format</a><a class="headerlink" href="#retrieve-a-model-in-json-format" title="Permalink to this heading"></a></h2>
<p>Thats the most simple way.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">onnxruntime.datasets</span> <span class="kn">import</span> <span class="n">get_example</span>
<span class="n">example1</span> <span class="o">=</span> <span class="n">get_example</span><span class="p">(</span><span class="s2">&quot;mul_1.onnx&quot;</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">onnx</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">onnx</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">example1</span><span class="p">)</span> <span class="c1"># model is a ModelProto protobuf message</span>
2020-08-13 02:12:50 +00:00
<span class="nb">print</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>ir_version: 3
producer_name: &quot;chenta&quot;
graph {
node {
input: &quot;X&quot;
input: &quot;W&quot;
output: &quot;Y&quot;
name: &quot;mul_1&quot;
op_type: &quot;Mul&quot;
}
name: &quot;mul test&quot;
initializer {
dims: 3
dims: 2
data_type: 1
float_data: 1.0
float_data: 2.0
float_data: 3.0
float_data: 4.0
float_data: 5.0
float_data: 6.0
name: &quot;W&quot;
}
input {
name: &quot;X&quot;
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
dim {
dim_value: 2
}
}
}
}
}
output {
name: &quot;Y&quot;
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
domain: &quot;&quot;
version: 7
}
</pre></div>
</div>
</section>
<section id="draw-a-model-with-onnx">
<h2><a class="toc-backref" href="#id2">Draw a model with ONNX</a><a class="headerlink" href="#draw-a-model-with-onnx" title="Permalink to this heading"></a></h2>
<p>We use <a class="reference external" href="https://github.com/onnx/onnx/blob/main/onnx/tools/net_drawer.py">net_drawer.py</a>
included in <em>onnx</em> package.
We use <em>onnx</em> to load the model
in a different way than before.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">onnx</span> <span class="kn">import</span> <span class="n">ModelProto</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">ModelProto</span><span class="p">()</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">example1</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fid</span><span class="p">:</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">fid</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">model</span><span class="o">.</span><span class="n">ParseFromString</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
</pre></div>
</div>
<p>We convert it into a graph.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">onnx.tools.net_drawer</span> <span class="kn">import</span> <span class="n">GetOpNodeProducer</span><span class="p">,</span> <span class="n">GetPydotGraph</span>
<span class="n">pydot_graph</span> <span class="o">=</span> <span class="n">GetPydotGraph</span><span class="p">(</span>
<span class="n">model</span><span class="o">.</span><span class="n">graph</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rankdir</span><span class="o">=</span><span class="s2">&quot;LR&quot;</span><span class="p">,</span> <span class="n">node_producer</span><span class="o">=</span><span class="n">GetOpNodeProducer</span><span class="p">(</span><span class="s2">&quot;docstring&quot;</span><span class="p">)</span>
<span class="p">)</span>
<span class="n">pydot_graph</span><span class="o">.</span><span class="n">write_dot</span><span class="p">(</span><span class="s2">&quot;graph.dot&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Then into an image</p>
2020-08-13 02:12:50 +00:00
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s2">&quot;dot -O -Tpng graph.dot&quot;</span><span class="p">)</span>
</pre></div>
</div>
2020-08-13 02:12:50 +00:00
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>0
</pre></div>
</div>
<p>Which we display…</p>
2020-08-13 02:12:50 +00:00
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="n">image</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s2">&quot;graph.dot.png&quot;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
</pre></div>
</div>
<img src="../images/sphx_glr_plot_pipeline_001.png" srcset="../images/sphx_glr_plot_pipeline_001.png" alt="plot pipeline" class = "sphx-glr-single-img"/><div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage object at 0x7f0fecf2bd90&gt;
2020-08-13 02:12:50 +00:00
</pre></div>
</div>
<p class="sphx-glr-timing"><strong>Total running time of the script:</strong> ( 0 minutes 0.136 seconds)</p>
<div class="sphx-glr-footer sphx-glr-footer-example docutils container" id="sphx-glr-download-auto-examples-plot-pipeline-py">
2020-08-13 02:12:50 +00:00
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<p><a class="reference download internal" download="" href="../downloads/d436e9922b51a71358604ec00f09e7e4/plot_pipeline.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">plot_pipeline.py</span></code></a></p>
</div>
2020-08-13 02:12:50 +00:00
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<p><a class="reference download internal" download="" href="../downloads/2dbd202de70c8b6a394b8a1c7c1e12b8/plot_pipeline.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">plot_pipeline.ipynb</span></code></a></p>
</div>
</div>
2020-08-13 02:12:50 +00:00
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../index.html">
<img class="logo" src="../static/ONNX_Runtime_icon.png" alt="Logo"/>
</a></p>
<h1 class="logo"><a href="../index.html">ONNX Runtime</a></h1>
<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api_summary.html">API</a></li>
<li class="toctree-l1"><a class="reference internal" href="index.html">Gallery of examples</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Gallery of examples</a><ul>
<li>Previous: <a href="index.html" title="previous chapter">Gallery of examples</a></li>
<li>Next: <a href="plot_load_and_predict.html" title="next chapter">Load and predict with ONNX Runtime and a very simple model</a></li>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2018-2023, Microsoft.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 5.3.0</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
|
<a href="../sources/auto_examples/plot_pipeline.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>