ONNX-Export einer SVM
Download Python Samples Ein Zip-Archiv mit allen Samples finden Sie hier: Beispiele zum ONNX-Export |
SVM Regressor mit Scikit-learn
from sklearn.svm import SVR
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
X = [[13,3],[1,16],[1,2]]
Y = [1.0,2.0,3.0]
model = SVR(kernel='rbf',gamma=10)
model.fit(X,Y)
out = model.predict(X)
input_type = [('float_input', FloatTensorType([None,len(X[0])]))]
onnx_filename = 'svr-rbf.onnx'
onx = convert_sklearn(model,initial_types=input_type)
with open(onnx_filename,"wb") as f:
f.write(onx.SerializeToString())
SVM Classifier mit Scikit-learn
One-Class-SVM
from sklearn.datasets import make_blobs
from sklearn.svm import OneClassSVM
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
n_samples = 150
input_dim = 3
X, _ = make_blobs(n_samples=n_samples, n_features=input_dim, centers=1, cluster_std=0.3, shuffle=True, random_state=42, )
svm = OneClassSVM(kernel='rbf', nu=0.3)
svm.fit(X)
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
onnx_model = convert_sklearn(svm, initial_types=initial_type)
filename = 'skl_oneclass_svm'
with open(filename + '.onnx', "wb") as f:
f.write( onnx_model.SerializeToString())
f.close()
Binary Classification
from sklearn.svm import SVC
import numpy as np
# random dataset
n_samples = 150
input_dim = 4
n_classes = 2 # binary classification
rand_in = np.random.random((n_samples, input_dim,))
rand_singleout_multiclass = np.random.randint(n_classes, size=(n_samples, 1))
X = rand_in
y = rand_singleout_multiclass
# train SVC
clr = SVC(kernel='linear', gamma=10) # decision_function_shape option is ignored for binary classification
clr.fit(X, y)
# Convert into ONNX format
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
# # Zipmap should be always turned off as it's not implemented in TF3800
onx = convert_sklearn(clr, initial_types=initial_type, options={type(clr): {'zipmap':False}})
with open("svc_random.onnx", "wb") as f:
f.write(onx.SerializeToString())
Multi-class Classification
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
# data set
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
# train SVC, note that decision_function_shape ovo is mandatory
clr = SVC(kernel='linear', gamma=10,decision_function_shape='ovo')
clr.fit(X_train, y_train)
# Convert into ONNX format
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
initial_type = [('float_input', FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type)
with open("svc_iris.onnx", "wb") as f:
f.write(onx.SerializeToString())
Form des ONNX-Graphen beachten! Bei Multi-Class-SVC-Modellen muss die Option decision_function_shape “ovo” genutzt werden, damit ein zu TF3800 kompatibler ONNX-Graph erzeugt wird. |
Nicht gültiger ONNX-Graph
Das folgende Beispiel zeigt den nicht gültigen ONNX-Graph: decision_function_shape “ovr”:
Gültiger ONNX-Graph
Das folgende Beispiel zeigt den gültigen ONNX-Graph: decision_function_shape “ovo”: