Project Introduction: sklearn-onnx
Overview
sklearn-onnx is a powerful tool designed to convert machine learning models built with scikit-learn into the ONNX format. This conversion is crucial for leveraging tools like ONNX Runtime, which can significantly enhance the performance of model scoring. The project is robust, with converters regularly tested using onnxruntime, ensuring compatibility and reliability. Additionally, sklearn-onnx can incorporate external converters to seamlessly convert pipelines that include models or transformers from external libraries.
Documentation
Comprehensive documentation, including tutorials, can be accessed at this site. Here, users can find detailed guides and resources to support their learning and application of the library. Notably, a list of supported scikit-learn models is available here, with the last supported opset being version 21. For issues or queries, the community can browse through existing issues or submit new ones for resolution.
Installation
Users can seamlessly integrate sklearn-onnx into their projects by installing it directly from PyPi using the following command:
pip install skl2onnx
Alternatively, for those who wish to explore the latest updates and features, installing directly from the source is possible:
pip install git+https://github.com/onnx/sklearn-onnx.git
Getting Started
To illustrate how sklearn-onnx can be utilized, here is a quick example:
-
Training a Model:
First, we train a RandomForestClassifier on the well-known Iris dataset:
import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier iris = load_iris() X, y = iris.data, iris.target X = X.astype(np.float32) X_train, X_test, y_train, y_test = train_test_split(X, y) clr = RandomForestClassifier() clr.fit(X_train, y_train)
-
Converting to ONNX Format:
Using the
to_onnx
function from skl2onnx, the trained model can be converted to ONNX format:from skl2onnx import to_onnx onx = to_onnx(clr, X[:1]) with open("rf_iris.onnx", "wb") as f: f.write(onx.SerializeToString())
-
Making Predictions with ONNX Runtime:
An inference session is established with onnxruntime to make predictions:
import onnxruntime as rt sess = rt.InferenceSession("rf_iris.onnx", providers=["CPUExecutionProvider"]) input_name = sess.get_inputs()[0].name label_name = sess.get_outputs()[0].name pred_onx = sess.run([label_name], {input_name: X_test.astype(np.float32)})[0]
Contributing
Sklearn-onnx encourages contributions from the community in various forms, such as feedback, ideas, or code enhancements. This collaborative approach aims to continuously improve the project and its usability.
License
The project is released under the Apache License v2.0, ensuring open-source accessibility and collaboration.
In summary, sklearn-onnx serves as a bridge between scikit-learn and ONNX, enhancing the usability and performance of machine learning models across different environments.