Intermediate results and investigation#

There are many reasons why a user wants more than using the converted model into ONNX. Intermediate results may be needed, the output of every node in the graph. The ONNX may need to be altered to remove some nodes. Transfer learning is usually removing the last layers of a deep neural network. Another reaason is debugging. It often happens that the runtime fails to compute the predictions due to a shape mismatch. Then it is useful the get the shape of every intermediate result. This example looks into two ways of doing it.

Look into pipeline steps#

The first way is a tricky one: it overloads methods transform, predict and predict_proba to keep a copy of inputs and outputs. It then goes through every step of the pipeline. If the pipeline has n steps, it converts the pipeline with step 1, then the pipeline with steps 1, 2, then 1, 2, 3…

from pyquickhelper.helpgen.graphviz_helper import plot_graphviz
from mlprodict.onnxrt import OnnxInference
import numpy
from onnxruntime import InferenceSession
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
from skl2onnx import to_onnx
from skl2onnx.helpers import collect_intermediate_steps
from skl2onnx.common.data_types import FloatTensorType

The pipeline.

data = load_iris()
X = data.data

pipe = Pipeline(steps=[
    ('std', StandardScaler()),
    ('km', KMeans(3))
])
pipe.fit(X)

Out:

Pipeline(steps=[('std', StandardScaler()), ('km', KMeans(n_clusters=3))])

The function goes through every step, overloads the methods transform and returns an ONNX graph for every step.

steps = collect_intermediate_steps(
    pipe, "pipeline",
    [("X", FloatTensorType([None, X.shape[1]]))])

We call method transform to population the cache the overloaded methods transform keeps.

pipe.transform(X)

Out:

array([[3.98940603, 0.21295824, 3.12119834],
       [4.01793312, 0.99604549, 2.6755083 ],
       [4.19343668, 0.65198444, 2.97416665],
       [4.19784749, 0.9034561 , 2.88014429],
       [4.11157152, 0.40215457, 3.30022609],
       [3.89893116, 1.21154793, 3.50554424],
       [4.21638048, 0.50244932, 3.14856384],
       [3.97313411, 0.09132468, 2.99184826],
       [4.40757189, 1.42174651, 2.92515933],
       [4.05764261, 0.78993078, 2.79398956],
       [3.92088109, 0.78999385, 3.32125333],
       [4.07853631, 0.27618123, 3.0493632 ],
       [4.16440431, 1.03497888, 2.80635045],
       [4.63069748, 1.33482453, 3.21220972],
       [4.14619343, 1.63865558, 3.88834965],
       [4.49547518, 2.39898792, 4.4998303 ],
       [4.02966144, 1.20748818, 3.60978017],
       [3.91388548, 0.21618828, 3.05594182],
       [3.72562039, 1.20986655, 3.34493953],
       [4.10101938, 0.86706182, 3.50065397],
       [3.66383713, 0.50401564, 2.80825681],
       [3.94496718, 0.66826437, 3.27800809],
       [4.51061335, 0.68658071, 3.58990876],
       [3.57996434, 0.47945627, 2.55934697],
       [3.98817445, 0.36345425, 2.96493153],
       [3.88431906, 0.99023912, 2.55682739],
       [3.79088782, 0.22683089, 2.8279719 ],
       [3.89539875, 0.2947186 , 3.05970831],
       [3.88085622, 0.25361098, 2.95425291],
       [4.09851673, 0.65019824, 2.87745051],
       [4.01796142, 0.80138328, 2.73238773],
       [3.57350896, 0.52309257, 2.73361981],
       [4.5037664 , 1.57658655, 4.11853014],
       [4.4465301 , 1.87652483, 4.22845606],
       [3.97906378, 0.76858489, 2.71452112],
       [4.01986385, 0.54896332, 2.86508665],
       [3.80064093, 0.63079314, 3.0573692 ],
       [4.25136846, 0.45982568, 3.40284985],
       [4.42052558, 1.2336976 , 3.00742655],
       [3.90865188, 0.14580827, 2.95472117],
       [4.01192633, 0.20261743, 3.12324651],
       [4.64398605, 2.67055552, 2.90164193],
       [4.42154566, 0.90927099, 3.15411688],
       [3.70483773, 0.50081008, 2.8613548 ],
       [3.9078554 , 0.92159916, 3.34606471],
       [4.01421067, 1.01946042, 2.65231058],
       [4.14238152, 0.86953764, 3.53206587],
       [4.23577398, 0.72275914, 2.99813103],
       [3.97409784, 0.72324305, 3.34116935],
       [3.97223984, 0.30295342, 2.90222887],
       [0.95288059, 3.43619989, 1.9003878 ],
       [0.99352148, 2.97232682, 1.41851492],
       [0.72661726, 3.51850037, 1.68457079],
       [2.69898424, 3.33264308, 0.96940962],
       [1.11074501, 3.35747592, 0.9112523 ],
       [1.8143491 , 2.77550662, 0.35721918],
       [1.00650285, 3.01808184, 1.59351202],
       [3.31296552, 2.77360088, 1.50213315],
       [1.14114175, 3.21148368, 1.11632078],
       [2.42994048, 2.66294828, 0.77921299],
       [3.73666782, 3.62389817, 1.97194958],
       [1.45918639, 2.70011145, 0.77530513],
       [2.74268279, 3.53658932, 1.25941769],
       [1.28976474, 2.98813829, 0.66155141],
       [2.05251547, 2.32311723, 0.73833453],
       [0.98780965, 3.14311522, 1.46572707],
       [1.67700171, 2.68234835, 0.80185102],
       [2.12682734, 2.63954211, 0.568386  ],
       [2.33743839, 3.97369206, 1.19987895],
       [2.46667974, 2.87494798, 0.67881532],
       [1.1880022 , 3.03853641, 1.34222961],
       [1.63233668, 2.8022861 , 0.53061062],
       [1.65142259, 3.68305664, 0.79234309],
       [1.54593744, 2.96833851, 0.57371215],
       [1.2933375 , 2.9760862 , 0.90589785],
       [1.03085926, 3.13002382, 1.22490527],
       [1.09304603, 3.56679427, 1.26783271],
       [0.52050254, 3.5903606 , 1.42114042],
       [1.34712856, 2.93839428, 0.58974672],
       [2.44164622, 2.58203512, 0.76432091],
       [2.69027665, 2.99796537, 0.89738242],
       [2.76965187, 2.92597852, 0.98549851],
       [2.02829879, 2.68907313, 0.3921368 ],
       [1.4211892 , 3.42215998, 0.54223583],
       [1.88799766, 2.62771445, 0.90567816],
       [1.39853465, 2.75915071, 1.70872911],
       [0.78009974, 3.30075052, 1.48190142],
       [2.2083069 , 3.73017167, 1.06129323],
       [1.87666989, 2.37943811, 0.81863359],
       [2.41035271, 2.98789866, 0.599882  ],
       [2.26782134, 2.89079656, 0.4914813 ],
       [1.25085451, 2.86642713, 0.84409423],
       [2.11791607, 2.86642575, 0.38941349],
       [3.35089399, 2.96966239, 1.53271026],
       [2.05312152, 2.77003779, 0.30831638],
       [1.83091351, 2.38255534, 0.81726253],
       [1.80454586, 2.55559903, 0.56428027],
       [1.39825227, 2.8455521 , 0.72672271],
       [3.06324547, 2.56987887, 1.28805849],
       [1.89861511, 2.64007308, 0.38163798],
       [1.0584579 , 4.24274589, 2.31271244],
       [1.5185265 , 3.57067982, 0.76585766],
       [0.52472   , 4.44150237, 2.14762671],
       [0.77236486, 3.69480186, 1.17645413],
       [0.53031563, 4.11613683, 1.73594932],
       [1.2022172 , 5.03326801, 2.78128346],
       [2.74462238, 3.3503222 , 1.22550604],
       [0.92275933, 4.577021  , 2.2426558 ],
       [1.40314162, 4.363498  , 1.50462864],
       [1.48323372, 4.79334275, 3.22975724],
       [0.4787491 , 3.62749566, 1.71837714],
       [1.0325986 , 3.89360823, 1.10409694],
       [0.27818948, 4.1132966 , 1.80475907],
       [1.91870424, 3.82688169, 0.94858807],
       [1.49910975, 3.91538879, 1.39433359],
       [0.68622715, 3.89835633, 1.90677079],
       [0.46463058, 3.70128288, 1.39713702],
       [2.10127163, 5.18341242, 3.85224062],
       [1.83092395, 5.58136629, 2.95786451],
       [2.37017622, 4.02615768, 1.17790381],
       [0.52540209, 4.31907679, 2.27442972],
       [1.62249456, 3.4288432 , 0.91211061],
       [1.47042293, 5.19031307, 2.77937737],
       [1.15814207, 3.64273089, 0.84735471],
       [0.520093  , 4.00723617, 2.15695444],
       [0.66660166, 4.2637671 , 2.33581345],
       [1.08324891, 3.45930032, 0.79774043],
       [0.94925151, 3.27575645, 1.022307  ],
       [0.84098317, 4.05342943, 1.3842265 ],
       [0.75748198, 4.1585729 , 2.03854964],
       [1.07124861, 4.71100584, 2.28297732],
       [2.17345728, 5.12224641, 3.88774921],
       [0.87682321, 4.13401784, 1.47357101],
       [1.11534598, 3.39830644, 0.7964005 ],
       [1.59782917, 3.63719075, 0.80521086],
       [1.25982873, 5.08776655, 2.8607372 ],
       [1.07214028, 4.00416552, 2.3101089 ],
       [0.51434392, 3.58815834, 1.46990247],
       [1.0762733 , 3.19454679, 0.97017134],
       [0.23050145, 4.09907253, 1.97333575],
       [0.57373487, 4.28416057, 2.07939567],
       [0.51130902, 4.17402084, 2.06609741],
       [1.5185265 , 3.57067982, 0.76585766],
       [0.54141867, 4.32128686, 2.24723796],
       [0.85128501, 4.3480018 , 2.42521977],
       [0.52475835, 4.1240495 , 1.82594618],
       [1.52100812, 3.97564407, 1.03093862],
       [0.44371189, 3.7539635 , 1.44892686],
       [1.08437101, 3.7969924 , 2.17585453],
       [1.13739231, 3.25638099, 1.00508668]])

We compute every step and compare ONNX and scikit-learn outputs.

for step in steps:
    print('----------------------------')
    print(step['model'])
    onnx_step = step['onnx_step']
    sess = InferenceSession(onnx_step.SerializeToString(),
                            providers=['CPUExecutionProvider'])
    onnx_outputs = sess.run(None, {'X': X.astype(numpy.float32)})
    onnx_output = onnx_outputs[-1]
    skl_outputs = step['model']._debug.outputs['transform']

    # comparison
    diff = numpy.abs(skl_outputs.ravel() - onnx_output.ravel()).max()
    print("difference", diff)

# That was the first way: dynamically overwrite
# every method transform or predict in a scikit-learn
# pipeline to capture the input and output of every step,
# compare them to the output produced by truncated ONNX
# graphs built from the first one.
#

Out:

----------------------------
StandardScaler()
difference 4.799262827148709e-07
----------------------------
KMeans(n_clusters=3)
difference 4.332024853268002e-06

Python runtime to look into every node#

The python runtime may be useful to easily look into every node of the ONNX graph. This option can be used to check when the computation fails due to nan values or a dimension mismatch.

onx = to_onnx(pipe, X[:1].astype(numpy.float32))

oinf = OnnxInference(onx)
oinf.run({'X': X[:2].astype(numpy.float32)},
         verbose=1, fLOG=print)

Out:

+ki='Ad_Addcst': (3,) (dtype=float32 min=0.9830552339553833 max=5.035177230834961)
+ki='Ge_Gemmcst': (3, 4) (dtype=float32 min=-1.3049873113632202 max=1.1359702348709106)
+ki='Mu_Mulcst': (1,) (dtype=float32 min=0.0 max=0.0)
-- OnnxInference: run 8 nodes
Onnx-Scaler(X) -> variable    (name='Scaler')
+kr='variable': (2, 4) (dtype=float32 min=-1.340226411819458 max=1.0190045833587646)
Onnx-ReduceSumSquare(variable) -> Re_reduced0    (name='Re_ReduceSumSquare')
+kr='Re_reduced0': (2, 1) (dtype=float32 min=4.850505828857422 max=5.376197338104248)
Onnx-Mul(Re_reduced0, Mu_Mulcst) -> Mu_C0    (name='Mu_Mul')
+kr='Mu_C0': (2, 1) (dtype=float32 min=0.0 max=0.0)
Onnx-Gemm(variable, Ge_Gemmcst, Mu_C0) -> Ge_Y0    (name='Ge_Gemm')
+kr='Ge_Y0': (2, 3) (dtype=float32 min=-10.366023063659668 max=7.967349052429199)
Onnx-Add(Re_reduced0, Ge_Y0) -> Ad_C01    (name='Ad_Add')
+kr='Ad_C01': (2, 3) (dtype=float32 min=-4.98982572555542 max=12.817854881286621)
Onnx-Add(Ad_Addcst, Ad_C01) -> Ad_C0    (name='Ad_Add1')
+kr='Ad_C0': (2, 3) (dtype=float32 min=0.045351505279541016 max=16.14378547668457)
Onnx-Sqrt(Ad_C0) -> scores    (name='Sq_Sqrt')
+kr='scores': (2, 3) (dtype=float32 min=0.2129589319229126 max=4.017932891845703)
Onnx-ArgMin(Ad_C0) -> label    (name='Ar_ArgMin')
+kr='label': (2,) (dtype=int64 min=1 max=1)

{'label': array([1, 1]), 'scores': array([[3.9894059 , 0.21295893, 3.1211984 ],
       [4.017933  , 0.99604493, 2.675508  ]], dtype=float32)}

And to get a sense of the intermediate results.

oinf.run({'X': X[:2].astype(numpy.float32)},
         verbose=3, fLOG=print)

# This way is usually better if you need to investigate
# issues within the code of the runtime for an operator.
#

Out:

+ki='Ad_Addcst': (3,) (dtype=float32 min=0.9830552339553833 max=5.035177230834961
[3.32593    5.035177   0.98305523]
+ki='Ge_Gemmcst': (3, 4) (dtype=float32 min=-1.3049873113632202 max=1.1359702348709106
[[ 1.1359702   0.08842168  0.9961545   1.0175261 ]
 [-1.0145789   0.85326266 -1.3049873  -1.2548935 ]
 [-0.05021989 -0.8833765   0.34773782  0.2815273 ]]
+ki='Mu_Mulcst': (1,) (dtype=float32 min=0.0 max=0.0
[0.]
-kv='X' shape=(2, 4) dtype=float32 min=0.20000000298023224 max=5.099999904632568
-- OnnxInference: run 8 nodes
Onnx-Scaler(X) -> variable    (name='Scaler')
+kr='variable': (2, 4) (dtype=float32 min=-1.340226411819458 max=1.0190045833587646)
[[-0.9006812   1.0190046  -1.3402264  -1.3154442 ]
 [-1.1430167  -0.13197924 -1.3402264  -1.3154442 ]]
Onnx-ReduceSumSquare(variable) -> Re_reduced0    (name='Re_ReduceSumSquare')
+kr='Re_reduced0': (2, 1) (dtype=float32 min=4.850505828857422 max=5.376197338104248)
[[5.3761973]
 [4.850506 ]]
Onnx-Mul(Re_reduced0, Mu_Mulcst) -> Mu_C0    (name='Mu_Mul')
+kr='Mu_C0': (2, 1) (dtype=float32 min=0.0 max=0.0)
[[0.]
 [0.]]
Onnx-Gemm(variable, Ge_Gemmcst, Mu_C0) -> Ge_Y0    (name='Ge_Gemm')
+kr='Ge_Y0': (2, 3) (dtype=float32 min=-10.366023063659668 max=7.967349052429199)
[[  7.213233  -10.366023    3.3826268]
 [  7.967349   -8.893578    1.3247826]]
Onnx-Add(Re_reduced0, Ge_Y0) -> Ad_C01    (name='Ad_Add')
+kr='Ad_C01': (2, 3) (dtype=float32 min=-4.98982572555542 max=12.817854881286621)
[[12.58943   -4.9898257  8.758824 ]
 [12.817855  -4.0430717  6.175288 ]]
Onnx-Add(Ad_Addcst, Ad_C01) -> Ad_C0    (name='Ad_Add1')
+kr='Ad_C0': (2, 3) (dtype=float32 min=0.045351505279541016 max=16.14378547668457)
[[15.9153595   0.04535151  9.741879  ]
 [16.143785    0.9921055   7.1583433 ]]
Onnx-Sqrt(Ad_C0) -> scores    (name='Sq_Sqrt')
+kr='scores': (2, 3) (dtype=float32 min=0.2129589319229126 max=4.017932891845703)
[[3.9894059  0.21295893 3.1211984 ]
 [4.017933   0.99604493 2.675508  ]]
Onnx-ArgMin(Ad_C0) -> label    (name='Ar_ArgMin')
+kr='label': (2,) (dtype=int64 min=1 max=1)
[1 1]

{'label': array([1, 1]), 'scores': array([[3.9894059 , 0.21295893, 3.1211984 ],
       [4.017933  , 0.99604493, 2.675508  ]], dtype=float32)}

Final graph#

ax = plot_graphviz(oinf.to_dot())
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plot fbegin investigate

Total running time of the script: ( 0 minutes 1.956 seconds)

Gallery generated by Sphinx-Gallery