Skip to main content

Python Runtime for ONNX models, other helpers to convert machine learned models in C++.

Project description

https://github.com/sdpython/mlprodict/blob/master/_doc/sphinxdoc/source/phdoc_static/project_ico.png?raw=true

mlprodict

Build status Build Status Windows https://circleci.com/gh/sdpython/mlprodict/tree/master.svg?style=svg https://dev.azure.com/xavierdupre3/mlprodict/_apis/build/status/sdpython.mlprodict https://badge.fury.io/py/mlprodict.svg MIT License https://codecov.io/github/sdpython/mlprodict/coverage.svg?branch=master GitHub Issues Notebook Coverage Downloads Forks Stars https://mybinder.org/badge_logo.svg size

mlprodict was initially started to help implementing converters to ONNX. The main features is a python runtime for ONNX (class OnnxInference), visualization tools (see Visualization), and a numpy API for ONNX). The package also provides tools to compare predictions, to benchmark models converted with sklearn-onnx.

import numpy
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_iris
from mlprodict.onnxrt import OnnxInference
from mlprodict.onnxrt.validate.validate_difference import measure_relative_difference
from mlprodict import __max_supported_opset__, get_ir_version

iris = load_iris()
X = iris.data[:, :2]
y = iris.target
lr = LinearRegression()
lr.fit(X, y)

# Predictions with scikit-learn.
expected = lr.predict(X[:5])
print(expected)

# Conversion into ONNX.
from mlprodict.onnx_conv import to_onnx
model_onnx = to_onnx(lr, X.astype(numpy.float32),
                     black_op={'LinearRegressor'},
                     target_opset=__max_supported_opset__)
print("ONNX:", str(model_onnx)[:200] + "\n...")

# Predictions with onnxruntime
model_onnx.ir_version = get_ir_version(__max_supported_opset__)
oinf = OnnxInference(model_onnx, runtime='onnxruntime1')
ypred = oinf.run({'X': X[:5].astype(numpy.float32)})
print("ONNX output:", ypred)

# Measuring the maximum difference.
print("max abs diff:", measure_relative_difference(expected, ypred['variable']))

# And the python runtime
oinf = OnnxInference(model_onnx, runtime='python')
ypred = oinf.run({'X': X[:5].astype(numpy.float32)},
                 verbose=1, fLOG=print)
print("ONNX output:", ypred)

Installation

Installation from pip should work unless you need the latest development features.

pip install mlprodict

The package includes a runtime for ONNX. That’s why there is a limited number of dependencies. However, some features relies on sklearn-onnx, onnxruntime, scikit-learn. They can be installed with the following instructions:

pip install mlprodict[all]

The code is available at GitHub/mlprodict and has online documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlprodict-0.9.1883.tar.gz (814.9 kB view hashes)

Uploaded Source

Built Distributions

mlprodict-0.9.1883-cp310-cp310-win_amd64.whl (2.6 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

mlprodict-0.9.1883-cp310-cp310-manylinux_2_24_x86_64.whl (27.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ x86-64

mlprodict-0.9.1883-cp310-cp310-macosx_10_13_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.10 macOS 10.13+ x86-64

mlprodict-0.9.1883-cp39-cp39-win_amd64.whl (2.6 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

mlprodict-0.9.1883-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (40.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mlprodict-0.9.1883-cp39-cp39-macosx_10_13_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.9 macOS 10.13+ x86-64

mlprodict-0.9.1883-cp38-cp38-win_amd64.whl (2.6 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

mlprodict-0.9.1883-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (40.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mlprodict-0.9.1883-cp38-cp38-macosx_10_13_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.8 macOS 10.13+ x86-64

mlprodict-0.9.1883-cp37-cp37m-win_amd64.whl (2.6 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

mlprodict-0.9.1883-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page