.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_pipeline.py: Draw a pipeline =============== There is no other way to look into one model stored in ONNX format than looking into its node with *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. .. GENERATED FROM PYTHON SOURCE LINES 22-49 .. code-block:: default import skl2onnx import onnxruntime import sklearn import numpy import matplotlib.pyplot as plt import os from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer from onnx import ModelProto import onnx from skl2onnx.algebra.onnx_ops import OnnxAdd, OnnxMul onnx_fct = OnnxAdd( OnnxMul('X', numpy.array([2], dtype=numpy.float32), op_version=12), numpy.array([[1, 0], [0, 1]], dtype=numpy.float32), output_names=['Y'], op_version=12) X = numpy.array([[4, 5], [-2, 3]], dtype=numpy.float32) model = onnx_fct.to_onnx({'X': X}, target_opset=12) print(model) filename = "example1.onnx" with open(filename, "wb") as f: f.write(model.SerializeToString()) .. rst-class:: sphx-glr-script-out .. code-block:: none ir_version: 7 producer_name: "skl2onnx" producer_version: "1.14.0" domain: "ai.onnx" model_version: 0 graph { node { input: "X" input: "Mu_Mulcst" output: "Mu_C0" name: "Mu_Mul" op_type: "Mul" domain: "" } node { input: "Mu_C0" input: "Ad_Addcst" output: "Y" name: "Ad_Add" op_type: "Add" domain: "" } name: "OnnxAdd" initializer { dims: 1 data_type: 1 float_data: 2.0 name: "Mu_Mulcst" } initializer { dims: 2 dims: 2 data_type: 1 float_data: 1.0 float_data: 0.0 float_data: 0.0 float_data: 1.0 name: "Ad_Addcst" } input { name: "X" type { tensor_type { elem_type: 1 shape { dim { } dim { dim_value: 2 } } } } } output { name: "Y" type { tensor_type { elem_type: 1 shape { dim { dim_value: 2 } dim { dim_value: 2 } } } } } } opset_import { domain: "" version: 12 } .. GENERATED FROM PYTHON SOURCE LINES 50-57 Draw a model with ONNX ++++++++++++++++++++++ We use `net_drawer.py `_ included in *onnx* package. We use *onnx* to load the model in a different way than before. .. GENERATED FROM PYTHON SOURCE LINES 57-64 .. code-block:: default model = ModelProto() with open(filename, 'rb') as fid: content = fid.read() model.ParseFromString(content) .. GENERATED FROM PYTHON SOURCE LINES 65-66 We convert it into a graph. .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default pydot_graph = GetPydotGraph(model.graph, name=model.graph.name, rankdir="TB", node_producer=GetOpNodeProducer("docstring")) pydot_graph.write_dot("graph.dot") .. GENERATED FROM PYTHON SOURCE LINES 71-72 Then into an image .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: default os.system('dot -O -Tpng graph.dot') .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 75-76 Which we display... .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default image = plt.imread("graph.dot.png") plt.imshow(image) plt.axis('off') .. image-sg:: /auto_examples/images/sphx_glr_plot_pipeline_001.png :alt: plot pipeline :srcset: /auto_examples/images/sphx_glr_plot_pipeline_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (-0.5, 380.5, 602.5, -0.5) .. GENERATED FROM PYTHON SOURCE LINES 81-82 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: default print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) print("onnx: ", onnx.__version__) print("onnxruntime: ", onnxruntime.__version__) print("skl2onnx: ", skl2onnx.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none numpy: 1.23.5 scikit-learn: 1.2.2 onnx: 1.13.1 onnxruntime: 1.14.1 skl2onnx: 1.14.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.630 seconds) .. _sphx_glr_download_auto_examples_plot_pipeline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_