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

Converting XGBoost to ONNX #421

Closed
dsmanl opened this issue Sep 8, 2020 · 0 comments
Closed

Converting XGBoost to ONNX #421

dsmanl opened this issue Sep 8, 2020 · 0 comments

Comments

@dsmanl
Copy link

dsmanl commented Sep 8, 2020

Dear all,

first of all thanks a lot for all of your efforts.

I am currently trying to convert a XGBoost classifier to ONNX format using a toy example. While doing that I am getting the following error AttributeError: 'numpy.uint8' object has no attribute 'encode'.

I am running on macOS Catalina, Version 10.15.6 and using the following Python (3.7.7) packages:

  • xgboost==1.1.0
  • onnx==1.6.0
  • onnxconverter-common==1.7.0
  • onnxmltools==1.7.0

The minimum working example is:

import tensorflow as tf
from pathlib import Path

from sklearn.preprocessing import StandardScaler
import xgboost as xgb
import onnxmltools

def xgboost_example():
	"""
	Train a simple xgboost model and store associated artefacts.
	"""
	path = Path('/PATH/TO/LOCAL/FOLDER')

	(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()

	X_train = X_train.reshape((X_train.shape[0], -1))
	#X_test = X_test.reshape((X_test.shape[0], -1))

	scaler = StandardScaler()

	X_train = scaler.fit_transform(X_train)
	#X_test = scaler.transform(X_test)

	sh = X_train[0].reshape(1, -1).shape

	clf = xgb.XGBClassifier(objective="multi:softprob", n_jobs=-1)
	clf.fit(X_train, y_train)

	onnx_model = onnxmltools.convert_xgboost(clf, initial_types=[
		('float_input', onnxmltools.convert.common.data_types.FloatTensorType(sh))])
	onnxmltools.utils.save_model(onnx_model, path.joinpath('model_onnxmltools.onnx'))


if __name__ == "__main__":
	xgboost_example()

The traceback is:

Traceback (most recent call last):
  File "test_xgb.py", line 40, in <module>
    xgboost_example()
  File "test_xgb.py", line 35, in xgboost_example
    ('float_input', onnxmltools.convert.common.data_types.FloatTensorType(sh))])
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxmltools/convert/main.py", line 85, in convert_xgboost
    return convert(*args, **kwargs)
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxmltools/convert/xgboost/convert.py", line 47, in convert
    onnx_model = convert_topology(topology, name, doc_string, target_opset, targeted_onnx)
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxconverter_common/topology.py", line 776, in convert_topology
    get_converter(operator.type)(scope, operator, container)
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 287, in convert_xgboost
    cls.convert(scope, operator, container)
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 248, in convert
    classes = np.array([s.encode('utf-8') for s in classes])
  File "/usr/local/anaconda3/envs/machine-learning/lib/python3.7/site-packages/onnxmltools/convert/xgboost/operator_converters/XGBoost.py", line 248, in <listcomp>
    classes = np.array([s.encode('utf-8') for s in classes])
AttributeError: 'numpy.uint8' object has no attribute 'encode'

Do you have any ideas on whether I am doing something wrong or whether there is another issue?

Thanks a lot!

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

1 participant