module cli.convert_validate#

Short summary#

module mlprodict.cli.convert_validate

Command line about validation of prediction runtime.

source on GitHub

Functions#

function

truncated documentation

convert_validate

Converts a model stored in pkl file and measure the differences between the model and the ONNX predictions.

Documentation#

Command line about validation of prediction runtime.

source on GitHub

mlprodict.cli.convert_validate.convert_validate(pkl, data=None, schema=None, method='predict', name='Y', target_opset=None, outonnx='model.onnx', runtime='python', metric='l1med', use_double=None, noshape=False, optim='onnx', rewrite_ops=True, options=None, fLOG=<built-in function print>, verbose=1, register=True)#

Converts a model stored in pkl file and measure the differences between the model and the ONNX predictions.

Parameters:
  • pkl – pickle file

  • data – data file, loaded with pandas, converted to a single array, the data is used to guess the schema if schema not specified

  • schema – initial type of the model

  • method – method to call

  • name – output name

  • target_opset – target opset

  • outonnx – produced ONNX model

  • runtime – runtime to use to compute predictions, ‘python’, ‘python_compiled’, ‘onnxruntime1’ or ‘onnxruntime2’

  • metric – the metric ‘l1med’ is given by function measure_relative_difference

  • noshape – run the conversion with no shape information

  • use_double – use double for the runtime if possible, two possible options, "float64" or 'switch', the first option produces an ONNX file with doubles, the second option loads an ONNX file (float or double) and replaces matrices in ONNX with the matrices coming from the model, this second way is just for testing purposes

  • optim – applies optimisations on the first ONNX graph, use ‘onnx’ to reduce the number of node Identity and redundant subgraphs

  • rewrite_ops – rewrites some converters from sklearn-onnx

  • options – additional options for conversion, dictionary as a string

  • verbose – verbose level

  • register – registers additional converters implemented by this package

  • fLOG – logging function

Returns:

a dictionary with the results

Converts and compares an ONNX file

The command converts and validates a scikit-learn model. An example to check the prediction of a logistic regression.

import os
import pickle
import pandas
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from mlprodict.__main__ import main
from mlprodict.cli import convert_validate

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, _ = train_test_split(X, y, random_state=11)
clr = LogisticRegression()
clr.fit(X_train, y_train)

pandas.DataFrame(X_test).to_csv("data.csv", index=False)
with open("model.pkl", "wb") as f:
    pickle.dump(clr, f)

And the command line to check the predictions using a command line.

convert_validate --pkl model.pkl --data data.csv
                 --method predict,predict_proba
                 --name output_label,output_probability
                 --verbose 1

<<<

python -m mlprodict convert_validate --help

>>>

usage: convert_validate [-h] [--pkl PKL] [-d DATA] [-s SCHEMA] [-m METHOD]
                        [-n NAME] [-t TARGET_OPSET] [-o OUTONNX] [-r RUNTIME]
                        [-me METRIC] [-u USE_DOUBLE] [-no NOSHAPE] [-op OPTIM]
                        [-re REWRITE_OPS] [-opt OPTIONS] [-v VERBOSE]
                        [-reg REGISTER]

Converts a model stored in *pkl* file and measure the differences between the
model and the ONNX predictions.

optional arguments:
  -h, --help            show this help message and exit
  --pkl PKL             pickle file (default: None)
  -d DATA, --data DATA  data file, loaded with pandas, converted to a single
                        array, the data is used to guess the schema if
                        *schema* not specified (default: )
  -s SCHEMA, --schema SCHEMA
                        initial type of the model (default: )
  -m METHOD, --method METHOD
                        method to call (default: predict)
  -n NAME, --name NAME  output name (default: Y)
  -t TARGET_OPSET, --target_opset TARGET_OPSET
                        target opset (default: )
  -o OUTONNX, --outonnx OUTONNX
                        produced ONNX model (default: model.onnx)
  -r RUNTIME, --runtime RUNTIME
                        runtime to use to compute predictions, 'python',
                        'python_compiled', 'onnxruntime1' or 'onnxruntime2'
                        (default: python)
  -me METRIC, --metric METRIC
                        the metric 'l1med' is given by function
                        :func:`measure_relative_difference <mlprodict.onnxrt.v
                        alidate.validate_difference.measure_relative_differenc
                        e>` (default: l1med)
  -u USE_DOUBLE, --use_double USE_DOUBLE
                        use double for the runtime if possible, two possible
                        options, ``"float64"`` or ``'switch'``, the first
                        option produces an ONNX file with doubles, the second
                        option loads an ONNX file (float or double) and
                        replaces matrices in ONNX with the matrices coming
                        from the model, this second way is just for testing
                        purposes (default: )
  -no NOSHAPE, --noshape NOSHAPE
                        run the conversion with no shape information (default:
                        False)
  -op OPTIM, --optim OPTIM
                        applies optimisations on the first ONNX graph, use
                        'onnx' to reduce the number of node Identity and
                        redundant subgraphs (default: onnx)
  -re REWRITE_OPS, --rewrite_ops REWRITE_OPS
                        rewrites some converters from :epkg:`sklearn-onnx`
                        (default: True)
  -opt OPTIONS, --options OPTIONS
                        additional options for conversion, dictionary as a
                        string (default: )
  -v VERBOSE, --verbose VERBOSE
                        verbose level (default: 1)
  -reg REGISTER, --register REGISTER
                        registers additional converters implemented by this
                        package (default: True)

source on GitHub