onnx / onnxmltools

ONNXMLTools enables conversion of models to ONNX
https://onnx.ai
Apache License 2.0
1k stars 181 forks source link

onnx converted : xgboostRegressor multioutput model predicts 1 dimension instead of original 210 dimensions. #676

Open devspatron opened 8 months ago

devspatron commented 8 months ago

Hello machine learning engineers, i have tried to convert an xgboost regressor multi-output model trained on dataset shape=(1000, 210). on the python runtime the model prediction predicts the correct shape = (1000,210) . However after converting the model into ONNX, the resulting onnx file is used with onnx_model = onnx_RT.InferenceSession(model_file_name), only predicts one dimension shape = (1000,1) instead of shape = (1000,210)

# code as follows:
import xgboost
import numpy as np
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import update_registered_converter

# Your data and labels
X = np.random.rand(100, 10)
y = np.random.rand(100, 210)

# Train XGBoost regressor
model = xgboost.XGBRegressor(objective='reg:squarederror', n_estimators=100)
model.fit(X, y)

# Define input type (adjust shape according to your input)
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]

# Convert XGBoost model to ONNX
onnx_model = convert_sklearn(model, initial_types=initial_type, target_opset=12)

# Save the ONNX model
with open("xgboost_model.onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())

please help ASAP, PLEASE

xadupre commented 8 months ago

Did you try since the PR was merged?

devspatron commented 7 months ago

Will try soonest possible

On Thu, Feb 8, 2024, 16:50 Xavier Dupré @.***> wrote:

Did you try since the PR was merged?

— Reply to this email directly, view it on GitHub https://github.com/onnx/onnxmltools/issues/676#issuecomment-1934163286, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUR5T7TKAUS3IEL5WAF3FJLYSTJY5AVCNFSM6AAAAABB46AY7SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZUGE3DGMRYGY . You are receiving this because you authored the thread.Message ID: @.***>

devspatron commented 7 months ago

Hello Xavier Dupré, its blurshift again with the same problem i have ran this code on google colab

the code

code= " //export models as ONNX !pip install onnx !pip install onnxmltools !pip install skl2onnx !pip install keras2onnx !pip install onnxruntime

//!pip uninstall protobuf //!pip install protobuf==4.21.9 !pip install protobuf==3.20.* !pip freeze | grep protobuf

//use my cpu specifi version !pip install -U xgboost==1.7.5

//code as follows: import xgboost import numpy as np import onnxmltools from skl2onnx import convert_sklearn from skl2onnx.common.data_types import FloatTensorType from skl2onnx import update_registered_converter

//Your data and labels [-Ox (253, 84) -Oy (253, 112) -] X = np.random.rand(100, 10 ) y = np.random.rand(100, 210 )

//Train XGBoost regressor model = xgboost.XGBRegressor(objective='reg:squarederror', n_estimators=10, ); model.fit(X, y);

//Define input type (adjust shape according to your input) num_features0 = X.shape[-1]; initial_type = [('float_input', FloatTensorType([None, num_features0 ]))]

//Convert XGBoost model to ONNX onnx_model = convert_sklearn( model, initial_types=initial_type, target_opset=12, ); //onnx_model = onnxmltools.convert_xgboost( model=model, initial_types=[('input', FloatTensorType([None, num_features0 ]))], );

//Save the ONNX model /content/sample_data with open( "/content/xgboost_model.onnx", "wb" ) as f: f.write( onnx_model.SerializeToString())

";

the error

the error i get error ="

MissingShapeCalculator Traceback (most recent call last)

https://localhost:8080/# in <cell line: 22>() 20 21 # Convert XGBoost model to ONNX ---> 22 onnx_model = convert_sklearn( model, initial_types=initial_type, target_opset=12, ); 23 # onnx_model = onnxmltools.convert_xgboost( model=model, initial_types=[('input', FloatTensorType([None, num_features0 ]))], ); 24

4 frames

/usr/local/lib/python3.10/dist-packages/skl2onnx/common/_topology.py https://localhost:8080/# in infer_types(self) 628 # Invoke a core inference function 629 if self.type is None: --> 630 raise MissingShapeCalculator( 631 "Unable to find a shape calculator for type '{}'.".format( 632 type(self.raw_operator)

MissingShapeCalculator: Unable to find a shape calculator for type '<class 'xgboost.sklearn.XGBRegressor'>'. It usually means the pipeline being converted contains a transformer or a predictor with no corresponding converter implemented in sklearn-onnx. If the converted is implemented in another library, you need to register the converted so that it can be used by sklearn-onnx (function update_registered_converter). If the model is not yet covered by sklearn-onnx, you may raise an issue to https://github.com/onnx/sklearn-onnx/issues to get the converter implemented or even contribute to the project. If the model is a custom model, a new converter must be implemented. Examples can be found in the gallery.

";

This problem still persists, even after installing new packages, Could you try running my code on google colab and try to examine the resulting converted onnx model using 'https://netron.app/'. Ensure the app has output shape=(-1, 210). Thank you.

On Wed, Feb 14, 2024 at 11:09 PM Patron Devs @.***> wrote:

Will try soonest possible

On Thu, Feb 8, 2024, 16:50 Xavier Dupré @.***> wrote:

Did you try since the PR was merged?

— Reply to this email directly, view it on GitHub https://github.com/onnx/onnxmltools/issues/676#issuecomment-1934163286, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUR5T7TKAUS3IEL5WAF3FJLYSTJY5AVCNFSM6AAAAABB46AY7SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZUGE3DGMRYGY . You are receiving this because you authored the thread.Message ID: @.***>

giorgostheo commented 3 months ago

Hi,

I am experiencing the same problem with @devspatron. Onnx'ed model predicts just 1d instead of 2d in my case. Any help?