2023-03-07

Converting LightGBM Models to ONNX and Performing Inference

Introduction

n this article, we will explore the process of converting a LightGBM model to an ONNX format for enhanced interoperability and ease of deployment across various platforms. We will cover preparing the LightGBM model, converting the model, and inferring with the converted model.

Preparing the LightGBM Model

Before we can convert the LightGBM model to ONNX, we need to prepare our LightGBM model by installing necessary libraries, loading a dataset, and training the model.

Installing Necessary Libraries

First, let's install the necessary libraries for working with LightGBM:

bash
$ pip install lightgbm numpy scikit-learn

Loading a Dataset and Training the Model

In this section, I will use the Iris dataset to demonstrate the process of training a LightGBM model. The Iris dataset consists of 150 samples with four features each, describing the length and width of the sepals and petals of three species of iris flowers.

The example code below demonstrates loading the dataset and training the model:

python
import lightgbm as lgb
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

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

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

# Train the LightGBM model
train_data = lgb.Dataset(X_train, label=y_train)
params = {'objective': 'multiclass', 'num_class': 3, 'metric': 'multi_logloss'}
model = lgb.train(params, train_data, num_boost_round=50)

In the code above, we first import the necessary libraries and load the Iris dataset. We then split the dataset into training and testing sets using a 80-20 ratio. Next, we create a LightGBM dataset object for training and set up the model parameters. Finally, we train the model using the lgb.train() function.

Converting the LightGBM Model to ONNX

After preparing the LightGBM model, the next step is to convert it to ONNX format. ONNX provides a standard format for representing machine learning models, enabling interoperability between different frameworks and tools.

Installing the ONNXMLTools Library

To convert the LightGBM model to ONNX, we'll use the onnxmltools library, which offers conversion functions for various machine learning models. Install the library using the following command:

bash
$ pip install onnxmltools

Conversion Process

Now that we have the necessary library installed, we can proceed to convert the trained LightGBM model to an ONNX format. The example code below demonstrates the conversion process:

python
import onnxmltools

# Convert the LightGBM model to ONNX
onnx_model = onnxmltools.convert_lightgbm(model)

# Save the ONNX model to a file
onnxmltools.utils.save_model(onnx_model, 'lightgbm_iris.onnx')

In this code snippet, we import the onnxmltools library and use the convert_lightgbm() function to convert our LightGBM model to an ONNX model. After the conversion is complete, we save the ONNX model to a file named lightgbm_iris.onnx.

Inferring with the Converted ONNX Model

Once the LightGBM model has been converted to ONNX format, we can use the ONNX Runtime library to perform inference. ONNX Runtime is a high-performance, cross-platform inference library that provides support for various hardware accelerators and platforms.

Installing ONNX Runtime

To infer using the ONNX model, we need to install the ONNX Runtime library:

bash
$ pip install onnxruntime

Loading and Running the ONNX Model

Now that the ONNX Runtime library is installed, we can load the ONNX model and perform inference on our test dataset. The example code below demonstrates loading the ONNX model and running inference:

python
import onnxruntime as rt
import numpy as np

# Load the ONNX model
sess = rt.InferenceSession('lightgbm_iris.onnx')

# Run inference using the ONNX model
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name

# Perform inference on the test dataset
predictions = sess.run([output_name], {input_name: X_test.astype(np.float32)})[0]

# Convert predictions to class labels
predicted_labels = np.argmax(predictions, axis=1)

# Calculate and print the accuracy of the ONNX model
accuracy = np.sum(predicted_labels == y_test) / len(y_test)
print(f"Accuracy of the ONNX model: {accuracy:.2f}")

In the code above, we first import the ONNX Runtime library and load the ONNX model using the InferenceSession class. We then retrieve the input and output names for the model. Next, we perform inference on the test dataset using the sess.run() method and convert the predictions to class labels by selecting the class with the highest probability for each sample. Finally, we calculate the accuracy of the ONNX model by comparing the predicted labels to the true labels in the test dataset.

References

https://github.com/onnx/onnxmltools
http://onnx.ai/sklearn-onnx/auto_tutorial/plot_gexternal_lightgbm.html
http://onnx.ai/sklearn-onnx/auto_tutorial/plot_gexternal_lightgbm_reg.html

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!