Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The target opset is not set correctly during the conversion of a RandomForestClassifier #1142

Open
CardoFlare opened this issue Nov 20, 2024 · 3 comments

Comments

@CardoFlare
Copy link

Description

Summary: The ONNX domains and their versions are not appropriately set after converting a RandomForestClassifier model by specifying a specific target_opset.

Expected Behavior: The ONNX domains and their versions should be accordingly set when I pass them to to_onnx function.

Actual Behavior: The ONNX domains and their versions are always evaulated as follows:

domain: '', version: 9
domain: 'ai.onnx.ml', version: 1

Steps to Reproduce

  1. Download dependencies
joblib==1.4.2
numpy==2.1.3
onnx==1.17.0
onnxconverter-common==1.14.0
packaging==24.2
protobuf==3.20.2
scikit-learn==1.5.2
scipy==1.14.1
skl2onnx==1.17.0
threadpoolctl==3.5.0
  1. Run the following script:
from skl2onnx import to_onnx
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

# Load dataset
data = load_iris()
X, y = data.data, data.target

# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the Random Forest model
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)

# Convert the trained model to ONNX format
onx = to_onnx(
    rf_model, 
    X[:1].astype(np.float32),  # Sample input for conversion
    target_opset={"": 15, "ai.onnx.ml": 2}
)

# Display the ONNX domains and their versions
domains = onx.opset_import
for dom in domains:
    print("domain: %r, version: %r" % (dom.domain, dom.version))

Actual output:

domain: '', version: 9
domain: 'ai.onnx.ml', version: 1

Expected output:

domain: '', version: 15
domain: 'ai.onnx.ml', version: 2

Environment

  • Operating System: Linux Ubuntu
  • Python Version: 3.12

Thanks in advance for your help! I look forward to any suggestions or solutions.

@xadupre
Copy link
Collaborator

xadupre commented Nov 25, 2024

It is the expected behabiour. There is new version for the random forest defined in opset 2 so the converter chooses the maximum version used by the operator in the model. The model is valid for every opset between this value and the value requested by the user. Should we change this behaviour?

@CardoFlare
Copy link
Author

thank you for your answer. It makes sense. So, is there no way to convert a RandomForestClassifier model to target opset ai.onnx version 14, correct?

@xadupre
Copy link
Collaborator

xadupre commented Nov 26, 2024

RandomForestClassifier is using domain ai.onnx.ml, it is a different opset than ai.onnx. You may have a opset version 14 for ai.onnx and 3 for ai.onnx.ml. The same logic applies for the main opset. If you ask for opset 15 and the converter returns opset 9, it means the same model is valid for opset 9 to 15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants