onnx / sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Apache License 2.0
541 stars 99 forks source link

Unsupported model IR version: 10, max supported IR version: 9 #1118

Open coogle opened 2 months ago

coogle commented 2 months ago

I'm trying to use convert_sklearn to convert my model to ONNX for use. My notebook runs and I can use the converted model within the notebook to make predictions... but when I try to take my ONNX model and use it in its production environment I am getting a version error:

Unsupported model IR version: 10, max supported IR version: 9

I don't think I can do much about the max supported IR version 9 (I'll be 100% honest I'm also kind of unclear what's going on here as this is my very first attempt at making a model from scratch ever).. Is there something I need to do in my call to convert_sklearn?

Here's the relevant code:

clr = MultiOutputClassifier(
    LogisticRegression(max_iter=500),
)

model = clr.fit(
    cv_clean.transform(X_train_clean), # Uncomment to try and comment out below
    #tfidf_clean.transform(X_train_raw), 
    y_train
)

y_pred = model.predict(
    cv_clean.transform(X_test_clean), # Uncomment to try and comment out below
    #tfidf_clean.transform(X_test_raw)
)

smaller_classification_report(y_test.values.astype(int), y_pred)

initial_type = [('float_input', FloatTensorType([None, cv_clean.transform(X_train_clean).shape[1]]))]

onnx_model = convert_sklearn(model, initial_types=initial_type, options={type(clr.estimator): {'zipmap': False}})

onnx_model_path = "build/Arvee/tags-classifier/onnx/model_quantized.onnx"
with open(onnx_model_path, "wb") as f:
    f.write(onnx_model.SerializeToString())

print(f"ONNX model saved to {onnx_model_path}")

session = rt.InferenceSession(onnx_model_path)

# Prepare the test data for ONNX Runtime
X_test_onnx = cv_clean.transform(X_test_clean).toarray().astype(np.float32)

# Execute the model against the test data
input_name = session.get_inputs()[0].name
label_name = session.get_outputs()[0].name

y_pred_onnx = session.run([label_name], {input_name: X_test_onnx})[0]

# Print the classification report
smaller_classification_report(y_test.values.astype(int), y_pred_onnx)
coogle commented 2 months ago

Package versions

scikit-learn 1.4.2 onnxruntime 1.17.0 (prod) onnxruntime 1.18.0 (notebook) skl2onnx 1.17.0

xadupre commented 1 month ago

onnx version is missing in your message. You should add convert_sklearn(..., target_opset=18). onnxruntime is usually one or two releases behind onnx.

berkanttubi commented 1 month ago

Downgrading onnx version to 1.15.0 will solve your problem.