module onnx_tools.optim.onnx_helper#

Short summary#

module mlprodict.onnx_tools.optim.onnx_helper

Statistics on ONNX models.

source on GitHub

Functions#

function

truncated documentation

change_input_first_dimension

Some models are converted under the assumption batch prediction is not necessary. This function changes the first …

onnx_statistics

Computes statistics on ONNX models, extracts informations about the model such as the number of nodes.

Documentation#

Statistics on ONNX models.

source on GitHub

mlprodict.onnx_tools.optim.onnx_helper.change_input_first_dimension(onnx_model, N=None, debug_info=None)#

Some models are converted under the assumption batch prediction is not necessary. This function changes the first dimension of an ONNX graph.

Parameters:
  • onnx_model – model onnx

  • N – new first dimension, None to avoid changing it, 0 to fix an undefined first dimension

  • debug_info – unused

Returns:

modified model onnx

source on GitHub

mlprodict.onnx_tools.optim.onnx_helper.onnx_statistics(onnx_model, recursive=True, optim=True, node_type=False)#

Computes statistics on ONNX models, extracts informations about the model such as the number of nodes.

Parameters:
  • onnx_model – onnx model

  • recursive – looks into subgraphs

  • optim – adds statistics because of optimisation

  • node_type – add distribution of node types

Returns:

dictionary

<<<

import pprint
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from mlprodict.onnx_tools.optim.onnx_helper import onnx_statistics
from mlprodict.onnx_conv import to_onnx

iris = load_iris()
X = iris.data
y = iris.target
lr = LogisticRegression()
lr.fit(X, y)
onx = to_onnx(lr, X[:1])
pprint.pprint((lr, onnx_statistics(onx)))

iris = load_iris()
X = iris.data
y = iris.target
rf = RandomForestClassifier()
rf.fit(X, y)
onx = to_onnx(rf, X[:1], target_opset=12)
pprint.pprint((rf, onnx_statistics(onx)))

>>>

    /usr/local/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:444: ConvergenceWarning: lbfgs failed to converge (status=1):
    STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
    
    Increase the number of iterations (max_iter) or scale the data as shown in:
        https://scikit-learn.org/stable/modules/preprocessing.html
    Please also refer to the documentation for alternative solver options:
        https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
      n_iter_i = _check_optimize_result(
    (LogisticRegression(),
     {'': 13,
      'ai.onnx.ml': 1,
      'doc_string': '',
      'domain': 'ai.onnx',
      'ir_version': 8,
      'model_version': 0,
      'ninits': 4,
      'ninits_optim': 4,
      'nnodes': 10,
      'nnodes_optim': 10,
      'op_Cast': 3,
      'op_Reshape': 1,
      'op_ZipMap': 1,
      'producer_name': 'skl2onnx',
      'producer_version': '1.12.999',
      'size': 1048,
      'size_optim': 1048})
    (RandomForestClassifier(),
     {'': 9,
      'ai.onnx.ml': 1,
      'doc_string': '',
      'domain': 'ai.onnx',
      'ir_version': 7,
      'mlprodict': 1,
      'model_version': 0,
      'ninits': 0,
      'ninits_optim': 0,
      'nnodes': 3,
      'nnodes_optim': 3,
      'op_Cast': 1,
      'op_ZipMap': 1,
      'producer_name': 'skl2onnx',
      'producer_version': '1.12.999',
      'size': 84228,
      'size_optim': 84228})

source on GitHub