.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_convert_syntax.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_convert_syntax.py: .. _l-convert-syntax: Different ways to convert a model ================================= This example leverages some code added to implement custom converters in an easy way. .. contents:: :local: Predict with onnxruntime ++++++++++++++++++++++++ Simple function to check the converted model works fine. .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: default import onnxruntime import onnx import numpy import numpy as np from sklearn.base import BaseEstimator, TransformerMixin from sklearn.cluster import KMeans from sklearn.pipeline import make_pipeline from onnxruntime import InferenceSession from skl2onnx import convert_sklearn, to_onnx, wrap_as_onnx_mixin from skl2onnx.common.data_types import FloatTensorType from skl2onnx.algebra.onnx_ops import OnnxSub, OnnxDiv from skl2onnx.algebra.onnx_operator_mixin import OnnxOperatorMixin def predict_with_onnxruntime(onx, X): sess = InferenceSession(onx.SerializeToString()) input_name = sess.get_inputs()[0].name res = sess.run(None, {input_name: X.astype(np.float32)}) return res[0] .. GENERATED FROM PYTHON SOURCE LINES 43-47 Simple KMeans +++++++++++++ The first way: :func:`convert_sklearn`. .. GENERATED FROM PYTHON SOURCE LINES 47-58 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = KMeans(n_clusters=2) tr.fit(X) onx = convert_sklearn( tr, initial_types=[('X', FloatTensorType((None, X.shape[1])))], target_opset=12) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 0 0 0 0 1 1 1 1 1] .. GENERATED FROM PYTHON SOURCE LINES 59-61 The second way: :func:`to_onnx`: no need to play with :class:`FloatTensorType` anymore. .. GENERATED FROM PYTHON SOURCE LINES 61-70 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = KMeans(n_clusters=2) tr.fit(X) onx = to_onnx(tr, X.astype(np.float32), target_opset=12) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 0 0 0 0 1 1 1 1 1] .. GENERATED FROM PYTHON SOURCE LINES 71-74 The third way: :func:`wrap_as_onnx_mixin`: wraps the machine learned model into a new class inheriting from :class:`OnnxOperatorMixin`. .. GENERATED FROM PYTHON SOURCE LINES 74-84 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = KMeans(n_clusters=2) tr.fit(X) tr_mixin = wrap_as_onnx_mixin(tr, target_opset=12) onx = tr_mixin.to_onnx(X.astype(np.float32)) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 85-87 The fourth way: :func:`wrap_as_onnx_mixin`: can be called before fitting the model. .. GENERATED FROM PYTHON SOURCE LINES 87-96 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = wrap_as_onnx_mixin(KMeans(n_clusters=2), target_opset=12) tr.fit(X) onx = tr.to_onnx(X.astype(np.float32)) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 97-101 Pipeline and a custom object ++++++++++++++++++++++++++++ This is a simple scaler. .. GENERATED FROM PYTHON SOURCE LINES 101-137 .. code-block:: default class CustomOpTransformer(BaseEstimator, TransformerMixin, OnnxOperatorMixin): def __init__(self): BaseEstimator.__init__(self) TransformerMixin.__init__(self) self.op_version = 12 def fit(self, X, y=None): self.W_ = np.mean(X, axis=0) self.S_ = np.std(X, axis=0) return self def transform(self, X): return (X - self.W_) / self.S_ def onnx_shape_calculator(self): def shape_calculator(operator): operator.outputs[0].type = operator.inputs[0].type return shape_calculator def to_onnx_operator(self, inputs=None, outputs=('Y', ), target_opset=None, **kwargs): if inputs is None: raise RuntimeError("Parameter inputs should contain at least " "one name.") opv = target_opset or self.op_version i0 = self.get_inputs(inputs, 0) W = self.W_.astype(np.float32) S = self.S_.astype(np.float32) return OnnxDiv(OnnxSub(i0, W, op_version=12), S, output_names=outputs, op_version=opv) .. GENERATED FROM PYTHON SOURCE LINES 138-139 Way 1 .. GENERATED FROM PYTHON SOURCE LINES 139-150 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2)) tr.fit(X) onx = convert_sklearn( tr, initial_types=[('X', FloatTensorType((None, X.shape[1])))], target_opset=12) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 151-152 Way 2 .. GENERATED FROM PYTHON SOURCE LINES 152-160 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2)) tr.fit(X) onx = to_onnx(tr, X.astype(np.float32), target_opset=12) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 161-162 Way 3 .. GENERATED FROM PYTHON SOURCE LINES 162-172 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2)) tr.fit(X) tr_mixin = wrap_as_onnx_mixin(tr, target_opset=12) tr_mixin.to_onnx(X.astype(np.float32)) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 173-174 Way 4 .. GENERATED FROM PYTHON SOURCE LINES 174-185 .. code-block:: default X = np.arange(20).reshape(10, 2) tr = wrap_as_onnx_mixin( make_pipeline(CustomOpTransformer(), KMeans(n_clusters=2)), target_opset=12) tr.fit(X) onx = tr.to_onnx(X.astype(np.float32)) print(predict_with_onnxruntime(onx, X)) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 0 0 0 0 1 1 1 1 1] .. GENERATED FROM PYTHON SOURCE LINES 186-190 Display the ONNX graph ++++++++++++++++++++++ Finally, let's see the graph converted with *sklearn-onnx*. .. GENERATED FROM PYTHON SOURCE LINES 190-207 .. code-block:: default from onnx.tools.net_drawer import GetPydotGraph, GetOpNodeProducer # noqa pydot_graph = GetPydotGraph(onx.graph, name=onx.graph.name, rankdir="TB", node_producer=GetOpNodeProducer( "docstring", color="yellow", fillcolor="yellow", style="filled")) pydot_graph.write_dot("pipeline_onnx_mixin.dot") import os # noqa os.system('dot -O -Gdpi=300 -Tpng pipeline_onnx_mixin.dot') import matplotlib.pyplot as plt # noqa image = plt.imread("pipeline_onnx_mixin.dot.png") fig, ax = plt.subplots(figsize=(40, 20)) ax.imshow(image) ax.axis('off') .. image-sg:: /auto_examples/images/sphx_glr_plot_convert_syntax_001.png :alt: plot convert syntax :srcset: /auto_examples/images/sphx_glr_plot_convert_syntax_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (-0.5, 2525.5, 6900.5, -0.5) .. GENERATED FROM PYTHON SOURCE LINES 208-209 **Versions used for this example** .. GENERATED FROM PYTHON SOURCE LINES 209-217 .. code-block:: default import sklearn # noqa print("numpy:", numpy.__version__) print("scikit-learn:", sklearn.__version__) import skl2onnx # noqa 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 13.799 seconds) .. _sphx_glr_download_auto_examples_plot_convert_syntax.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_convert_syntax.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_convert_syntax.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_