Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically deploy open source AI models to production environments Sklearn, XGBoost, LightGBM, and PySpark

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to automatically deploy the open source AI model to the production environment Sklearn, XGBoost, LightGBM, and PySpark. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Background introduction

The widespread use of AI is driven by the progress of AI in open source technology. Using the powerful open source model library, data scientists can easily train a model with good performance. However, because of the difference between the model production environment and the development environment, different roles are involved: model training is the work of data scientists and data analysts, but model deployment is the job of developers and operation and maintenance engineers. It is not so easy to deploy the model online.

DaaS (Deployment-as-a-Service) is a Kubernetes-based AI model automatic deployment system developed by AutoDeployAI, which provides one-click automatic deployment of open source AI model to generate REST API, which is easy to call in the production environment. Next, we mainly demonstrate how to deploy classic machine learning models in DaaS, including Scikit-learn, XGBoost, LightGBM, and PySpark ML Pipelines. The deployment of the deep learning model will be described in the next chapter.

Deployment preparation

We use the Python client (DaaS-Client) provided by DaaS to deploy the model, and for XGBoost and LightGBM, we also use their Python API for model training. Before training and deploying the model, we need to do the following.

Install Python DaaS-Client.

Pip install-- upgrade git+ https://github.com/autodeployai/daas-client.git

Initialize DaasClient. Log in to the system using the URL, account and password of the DaaS system, and the text-based DaaS demo system is installed on the local Minikube. Complete Jupyter Notebook, please refer to: deploy-sklearn-xgboost-lightgbm-pyspark.ipynb

From daas_client import DaasClientclient = DaasClient ('https://192.168.64.3:30931',' username', 'password')

Create a project. DaaS uses projects to manage users' different analysis tasks, and a project can contain users' various analysis assets: models, deployments, program scripts, data, data sources, and so on. After the project is created successfully, it is set to the current active project, and the published model and the deployment created are stored under the project. The create_project function takes three parameters:

Project = 'deployment test' if not client.project_exists (project): client.create_project (project, 'deployment-test',' deployment test project') client.set_project (project)

Project name: can be any valid Linux file directory name.

Project routing: the current project is uniquely represented in the deployed REST URL. It can only be lowercase English characters (aMuz), numbers (0-9), and middle hyphens-and-cannot be at the beginning and end.

Project description (optional): can be any character.

Initialize the data. We use the popular classified data set iris to train different models, and divide the data into training data set and test data set to facilitate subsequent use.

From sklearn import datasetsfrom sklearn.model_selection import train_test_splitimport pandas as pdseed = 123456iris = datasets.load_iris () iris_target_name = 'Species'iris_feature_names = iris.feature_namesiris_df = pd.DataFrame (iris.data, columns=iris_feature_names) iris_ DF [Iris _ target_name] = iris.targetX, y = iris_ DF [Iris _ feature_names], iris_ DF [Iris _ target_name] X_train, X_test, y_train, y_test = train_test_split (X, y) Test_size=0.3, random_state=seed)

Model deployment process. It mainly includes the following steps:

Training model. Use the API provided by the model library to train the model on the iris dataset.

Publish the model. Call the publish function to publish the model to the DaaS system.

Test model (optional). Call the test function to get the test API information, you can use any REST client program to test whether the model works properly in DaaS, and use the DaaS system model to test API. Executing test for the first time is slow because the DaaS system needs to start the test runtime environment.

Deployment model. After the release is successful, call the deploy function to deploy the deployment model. You can use any REST client program to test the model deployment, using the DaaS system to formally deploy API.

Deploy the Scikit-learn model

Train a Scikit-learn classification model: SVC.

From sklearn.svm import SVCmodel = SVC (probability=True, random_state=seed) model.fit (X_train, y_train)

Publish the Scikit-learn model.

Publish_resp = client.publish (model, name='iris', mining_function='classification', X_test=X_test, y_test=y_test, description='A SVC model') pprint (publish_resp)

The test function must specify the first two parameters, the first model is the trained model object, the second is the model name, and the rest are optional parameters:

Publish_resp is the result of a dictionary type that records the name of the model and the version of the model released. This model is the first version of the iris model.

{'model_name':' iris', 'model_version':' 1'}

Mining_function: specifies the mining function, which can be specified as regression (regression), classification (classification), and clustering (clustering).

X_test and y_test: specify a test training set and calculate model evaluation indicators at release, such as correct rate (Accuracy) for classification models and interpretable variance (explained Variance) for regression models.

Data_test: the test training set is also specified, but this parameter is used on the Spark model, and the non-Spark model is specified through X_test and y_test.

Description: model description.

Params: record the model parameter settings.

Test the Scikit-learn model.

Test_resp = client.test (publish_resp ['model_name'], model_version=publish_resp [' model_version']) pprint (test_resp)

Test_resp is the result of a dictionary type that records the test REST API information. Below, where access_token is the access token, a long string that is not shown here. Endpoint_url specifies the test REST API address, and payload provides the request body format that needs to be entered to test the current model.

{'access_token':' Amurlon args': args':: [{'petal length (cm)': 1. 5 'petal width (cm)': 0.4, 'sepal length (cm)': 5.7, 'sepal width (cm)': 4.4}], 'model_name':' iris', 'model_version':' 1'}}

Use requests to call the test API. Here we directly use the test payload returned by test_resp. You can also use the custom data X, but the parameters model_name and model_version must use the values output above.

Response = requests.post (test_resp ['endpoint_url'], headers= {' Authorization': 'Bearer {token}' .format (token=test_resp ['access_token'])}, json=test_resp [' payload'], verify=False) pprint (response.json ())

The returned result is different from the formal deployment of API. In addition to the predicted result, the test API will return both standard console output and standard error output to facilitate users to view relevant information when they encounter errors.

{'result': [{' PredictedValue': 0, 'Probabilities': [0.8977133931668801,0.05476023239878367, 0.047526374434336216]},' stderr': [], 'stdout': []}

Deployment model.

Deploy_resp = client.deploy (model_name='iris', deployment_name='iris-svc', model_version=publish_resp ['model_version'], replicas=1) pprint (deploy_resp)

The deploy function must specify the model name and deployment name. The model version defaults to the current latest version (latest), and the number of copies defaults to 1. To ensure the stability of the deployment service, you can also enter the deployment runtime environment allocation to specify the number of CPU cores and the amount of memory used, which defaults to None, allowing the system to allocate automatically.

Deploy_resp is the result of a dictionary type that records the officially deployed REST API information. As shown below, you can see that similar to the test results, in payload, we do not need to enter the model name and version, because the formal deployment service has already recorded this information when it is created and is an exclusive service.

{'access_token':' Amurlon args': args':: [{'petal length (cm)': 1. 5 'petal width (cm)': 0.4, 'sepal length (cm)': 5.7, 'sepal width (cm)': 4.4}]}

Use requests to call test API. Here we directly use the test payload returned by test_resp. You can also use custom data.

Response = requests.post (deploy_resp ['endpoint_url'], headers= {' Authorization': 'Bearer {token}' .format (token=deploy_resp ['access_token'])}, json=deploy_resp [' payload'], verify=False) pprint (response.json ())

Return the result:

{'result': [{' PredictedValue': 0, 'Probabilities': [0.8977133931668801,0.05476023239878367, 0.047526374434336216]}]}

Deploy the XGBoost model

XGBoost provides two sets of Python API, one is native Python API, and the other is based on Scikit-learn packaging API. You can use either, and we use Scikit-learn-based Python API in the following example.

Train a classified XGBoost model:

From xgboost import XGBClassifiermodel = XGBClassifier (max_depth=3, objective='multi:softprob', random_state=seed) model = model.fit (X_train, y_train)

Publish the XGBoost model.

Publish_resp = client.publish (model, name='iris', mining_function='classification', X_test=X_test, y_test=y_test, description='A XGBClassifier model') pprint (publish_resp)

Because the model name iris is still used, this model is the second version of iris.

{'model_name':' iris', 'model_version':' 2'}

Test the XGBoost model. Same as the Scikit-learn process.

Deployment model. As with the Scikit-learn process, we will not create a stand-alone deployment for the time being. Later, we will describe how to manage the deployment in the DaaS system and how to switch the deployment model version.

Deploy the LightGBM model

Similar to XGBoost, LightGBM also provides two sets of Python API, one is native Python API, and the other is based on Scikit-learn packaging API. You can use either, and we use Scikit-learn-based Python API in the following example.

Train a classified LightGBM model:

From lightgbm import LGBMClassifiermodel = LGBMClassifier () model = model.fit (X_train, y_train, eval_set= [(X_test, y_test)])

Publish the LightGBM model.

Publish_resp = client.publish (model, name='iris', mining_function='classification', X_test=X_test, y_test=y_test, description='A LGBMClassifier model') pprint (publish_resp)

The LightGBM model is the third version of iris.

{'model_name':' iris', 'model_version':' 3'}

Test the LightGBM model. Same as the Scikit-learn process.

Deployment model. As with the Scikit-learn process, let's not create a stand-alone deployment for the time being.

Deploy the PySpark model

Train a PySpark classification model: RandomForestClassifier. The PySpark model must be a PipelineModel, which means that the model must be built using Pipeline, even if there is only one Pipeline node.

From pyspark.sql import SparkSessionfrom pyspark.ml.classification import RandomForestClassifierfrom pyspark.ml.feature import VectorAssemblerfrom pyspark.ml import Pipelinespark = SparkSession.builder.getOrCreate () df = spark.createDataFrame (iris_df) df_train, df_test = df.randomSplit ([0.7,0.3], seed=seed) assembler = VectorAssembler (inputCols=iris_feature_names, outputCol='features') rf = RandomForestClassifier (seed=seed) .setLabelCol (iris_target_name) pipe = Pipeline (stages= [assembler, rf]) model = pipe.fit (df_train)

Publish the PySpark model.

Publish_resp = client.publish (model, name='iris', mining_function='classification', data_test=df_test, description='A RandomForestClassifier of Spark model') pprint (publish_resp)

The PySpark model is the fourth version of iris.

{'model_name':' iris', 'model_version':' 4'}

Test the PySpark model. Same as the Scikit-learn process.

Deployment model. As with the Scikit-learn process, let's not create a stand-alone deployment for the time being.

Model deployment management

Open the browser and log in to the DaaS management system. Enter the project deployment test, switch to the model tab, there is an iris model, the latest version is v4, the type is Spark, our last release of the model.

Click on the model to go to the model home page (overview). The current v4 is a Spark Pipeline model with a correct rate of 94.23%, and shows a historical chart of the correct rate of different versions of iris. The input and output variables of the model, as well as the evaluation results, are listed below, which are currently empty because no model evaluation tasks have been performed in DaaS.

Click v4 and you are free to switch to another version. For example, switch to v1.

The v1 version is a Scikit-learn SVM classification model with a correct rate of 98.00%. Other information is similar to v4.

Switch to the model deployment tab, there is a deployment iris-svc we just created, move the mouse to the action menu, and select modify settings. As you can see, the current deployment service is associated with model v1, which is the first version of the Scikit-learn model of iris that we just deployed through the deploy function. Select the latest v4, click the command to save and redeploy, and the deployment will switch to the v4 version.

On how to automatically deploy the open source AI model to the production environment Sklearn, XGBoost, LightGBM, and PySpark to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report