I need to convert an XGBoost model that has been saved in the .sav format into the ONNX format. I believe it's related to the XGBoost version, but the problem is, if I update the XGBoost version to solve this problem related to ONNX, I cannot properly load the model with joblib because the portability of a .sav model is not assured for a different environment than the one the model has been trained in. So, I'm stuck! How should I handle it?
The error I got is :
Traceback (most recent call last):
File "/mnt/c/users/dev/convert_onnx.py", line 29, in <module>
onnx_model = convert_sklearn(model,"gbdt_model",
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/skl2onnx/convert.py", line 208, in convert_sklearn
onnx_model = convert_topology(
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/skl2onnx/common/_topology.py", line 1532, in convert_topology
topology.convert_operators(container=container, verbose=verbose)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/skl2onnx/common/_topology.py", line 1350, in convert_operators
self.call_converter(operator, container, verbose=verbose)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/skl2onnx/common/_topology.py", line 1133, in call_converter
conv(self.scopes[0], operator, container)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/skl2onnx/common/_registration.py", line 27, in __call__
return self._fct(*args)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 430, in convert_xgboost
cls.validate(xgb_node)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 283, in validate
return XGBConverter.validate(xgb_node)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 26, in validate
params = XGBConverter.get_xgb_params(xgb_node)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 22, in get_xgb_params
return get_xgb_params(xgb_node)
File "/mnt/c/users/dev/flask/lib/python3.10/site-packages/onnxmltools/convert/xgboost/common.py", line 40, in get_xgb_params
gbp = config["learner"]["gradient_booster"]["gbtree_model_param"]
KeyError: 'gbtree_model_param'
The script is :
from skl2onnx import convert_sklearn
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import get_latest_tested_opset_version
from onnxmltools.utils import save_model
import joblib
from skl2onnx import convert_sklearn, to_onnx, update_registered_converter
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx.common.shape_calculator import (
calculate_linear_classifier_output_shapes,
calculate_linear_regressor_output_shapes,
)
from onnxmltools.convert.xgboost.operator_converters.XGBoost import convert_xgboost
from onnxmltools.convert import convert_xgboost as convert_xgboost_booster
from xgboost import XGBClassifier
model = joblib.load("model.sav")
model = model.set_params(flatten_transform=False)
n_features = 12
target_opset = get_latest_tested_opset_version()
update_registered_converter(
XGBClassifier,
"XGBoostXGBClassifier",
calculate_linear_classifier_output_shapes,
convert_xgboost,
options={"nocl": [True, False], "zipmap": [True, False, "columns"]},
)
onnx_model = convert_sklearn(model,"gbdt_model",
initial_types=[("input", FloatTensorType([None, n_features]))],
target_opset={"": target_opset, "ai.onnx.ml": 1})
save_model(onnx_model, 'model_converted.onnx')
The dependencies are:
Package Version
-------------------- -------
blinker 1.7.0
click 8.1.7
cloudpickle 3.0.0
coloredlogs 15.0.1
contourpy 1.2.0
cycler 0.12.1
Flask 3.0.2
flatbuffers 23.5.26
fonttools 4.48.1
humanfriendly 10.0
itsdangerous 2.1.2
Jinja2 3.1.3
joblib 1.3.2
kiwisolver 1.4.5
llvmlite 0.42.0
MarkupSafe 2.1.5
matplotlib 3.8.2
mpmath 1.3.0
numba 0.59.0
numpy 1.26.4
onnx 1.15.0
onnxconverter-common 1.14.0
onnxmltools 1.12.0
onnxruntime 1.17.0
packaging 23.2
pandas 2.2.0
pillow 10.2.0
pip 22.0.2
protobuf 3.20.2
pydot 2.0.0
pyparsing 3.1.1
python-dateutil 2.8.2
pytz 2024.1
PyYAML 6.0.1
scikit-learn 1.0.2
scipy 1.12.0
setuptools 59.6.0
shap 0.44.1
six 1.16.0
skl2onnx 1.16.0
slicer 0.0.7
sympy 1.12
threadpoolctl 3.2.0
tqdm 4.66.2
typing_extensions 4.9.0
tzdata 2024.1
Werkzeug 3.0.1
wheel 0.37.1
xgboost 1.4.2
I have used the tutorial : https://onnx.ai/sklearn-onnx/auto_examples/plot_pipeline_xgboost.html