2.2.0
Machine Learning support (ONNX-ML operators)


ST Edge AI Core

Machine Learning support (ONNX-ML operators)


ST Edge AI Core Technology 2.2.0



r1.1

Overview

Machine Learning algorithms from Scikit-learn framework or XGBoost package are not directly supported. After training step, the algorithms should be converted in ONNX format to be deployed and imported. As for ONNX DL models, the same embedded C-inference API is generated (see “Embedded Inference Client ST Edge AI API” article). The skl2onnx utility is typically used to convert the models in ONNX format.

https://xgboost.readthedocs.io/en/latest/index.html
https://scikit-learn.org/
https://onnx.ai/sklearn-onnx/


import numpy as np
from sklearn.ensemble import RandomForestClassifier
from skl2onnx import to_onnx

X = np.array(..., dtype=np.float32)
Y = np.array(..., dtype=np.float32)

clf = RandomForestClassifier(random_state=0, n_estimators=4, max_depth=3)
clf.fit(X, Y)

exported_onnx = to_onnx(clf, np.array(X).astype(np.float32))
with open("random_forest.onnx", "wb") as f:
    f.write(exported_onnx.SerializeToString())

Note

Other ML framework with ONNX exporter can be used, but to be aware that the import of the ONNX-ML models in ST Edge AI Core has been mainly tested with Scikit-learn v0.23.1, skl2onnx v1.10.3 and XGBoost v1.5.1.

Supported ONNX-ML operators

Reference: https://github.com/onnx/onnx/blob/master/docs/Operators-ml.md

ai.onnx.ml description
ai.onnx.ArrayFeatureExtractor Select elements of the input tensor based on the indices passed.
ai.onnx.LabelEncoder Maps each element in the input tensor to another value.
ai.onnx.LinearClassifier Linear Classifier.
ai.onnx.Normalizer Normalize the input.
ai.onnx.SVMClassifier Support Vector Machine classifier.
ai.onnx.SVMRegressor Support Vector Machine regression prediction and one-class SVM anomaly detection.
ai.onnx.Scaler Rescale input data, for example to standardize features by removing the mean and scaling to unit variance.
ai.onnx.TreeEnsembleClassifier Tree Ensemble classifier. Returns the top class for each of N inputs.
ai.onnx.TreeEnsembleRegressor Tree Ensemble regressor. Returns the regressed values for each input in N.
ai.onnx.ZipMap Creates a map from the input and the attributes.

Supported scikit-learn algorithms

Reference: https://scikit-learn.org/

Scikit-learn is a complete framework for machine learning (ML) in Python which allows to build a large range of supervided or unsupervied ML algorithms: classifier, regressor, decision Trees, …

Random Forest example

Reference: http://onnx.ai/sklearn-onnx/auto_examples/plot_convert_model.html

from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import convert_sklearn
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_train, X_test, y_train, y_test = train_test_split(X, y)
clr = RandomForestClassifier()
clr.fit(X_train, y_train)

initial_type = [('float_input', FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type,
                      target_opset=10)

with open("rf_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())