.. 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. Retrieve a model in JSON format +++++++++++++++++++++++++++++++ That's the most simple way. .. GENERATED FROM PYTHON SOURCE LINES 19-47 .. code-block:: Python 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 opset_import { domain: "" version: 12 } producer_name: "skl2onnx" producer_version: "1.17.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 name: "Mu_Mulcst" } initializer { dims: 2 dims: 2 data_type: 1 float_data: 1 float_data: 0 float_data: 0 float_data: 1 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 } } } } } } .. GENERATED FROM PYTHON SOURCE LINES 48-55 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 55-62 .. code-block:: Python model = ModelProto() with open(filename, "rb") as fid: content = fid.read() model.ParseFromString(content) .. GENERATED FROM PYTHON SOURCE LINES 63-64 We convert it into a graph. .. GENERATED FROM PYTHON SOURCE LINES 64-72 .. code-block:: Python 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 73-74 Then into an image .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python os.system("dot -O -Tpng graph.dot") .. rst-class:: sphx-glr-script-out .. code-block:: none 0 .. GENERATED FROM PYTHON SOURCE LINES 77-78 Which we display... .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: Python 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, 431.5, 602.5, -0.5) .. GENERATED FROM PYTHON SOURCE LINES 83-84 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python 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.26.4 scikit-learn: 1.6.dev0 onnx: 1.17.0 onnxruntime: 1.18.0+cu118 skl2onnx: 1.17.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.652 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-jupyter :download:`Download Jupyter notebook: plot_pipeline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_