使用 Azure 机器学习部署现有模型Deploy your existing model with Azure Machine Learning

本文介绍如何注册和部署在 Azure 机器学习外部训练的机器学习模型。In this article, you learn how to register and deploy a machine learning model you trained outside Azure Machine Learning. 可以将其部署为 Web 服务或部署到 IoT Edge 设备。You can deploy as a web service or to an IoT Edge device. 部署后,可以在 Azure 机器学习中监视模型并检测数据偏移。Once deployed, you can monitor your model and detect data drift in Azure Machine Learning.

有关本文中的概念和术语的详细信息,请参阅管理、部署和监视机器学习模型For more information on the concepts and terms in this article, see Manage, deploy, and monitor machine learning models.

先决条件Prerequisites

注册模型Register the model(s)

通过注册模型可以让你在工作区中存储、版本和跟踪有关模型的元数据。Registering a model allows you to store, version, and track metadata about models in your workspace. 在以下 Python 和 CLI 示例中,models 目录包含 model.h5model.w2vencoder.pkltokenizer.pkl 文件。In the following Python and CLI examples, the models directory contains the model.h5, model.w2v, encoder.pkl, and tokenizer.pkl files. 此示例将 models 目录中包含的文件上传为名为 sentiment 的新模型注册:This example uploads the files contained in the models directory as a new model registration named sentiment:

from azureml.core.model import Model
# Tip: When model_path is set to a directory, you can use the child_paths parameter to include
#      only some of the files from the directory
model = Model.register(model_path = "./models",
                       model_name = "sentiment",
                       description = "Sentiment analysis model trained outside Azure Machine Learning",
                       workspace = ws)

有关详细信息,请参阅 Model.register() 参考。For more information, see the Model.register() reference.

az ml model register -p ./models -n sentiment -w myworkspace -g myresourcegroup

提示

还可以设置为向已注册的模型添加 tagsproperties 字典对象。You can also set add tags and properties dictionary objects to the registered model. 以后可以使用这些值来帮助识别特定的模型。These values can be used later to help identify a specific model. 例如使用的框架、训练参数等。For example, the framework used, training parameters, etc.

有关详细信息,请参阅 az ml model register 参考。For more information, see the az ml model register reference.

有关一般情况下模型注册的详细信息,请参阅管理、部署和监视机器学习模型For more information on model registration in general, see Manage, deploy, and monitor machine learning models.

定义推理配置Define inference configuration

推理配置定义用于运行已部署模型的环境。The inference configuration defines the environment used to run the deployed model. 推理配置引用以下实体,这些实体用于在部署模型时运行模型:The inference configuration references the following entities, which are used to run the model when it's deployed:

  • 入口脚本(名为 score.py)在部署的服务启动时加载模型。An entry script, named score.py, loads the model when the deployed service starts. 此脚本还负责接收数据,将数据传递到模型,然后返回响应。This script is also responsible for receiving data, passing it to the model, and then returning a response.
  • Azure 机器学习环境An Azure Machine Learning environment. 环境定义运行模型和入口脚本所需的软件依赖项。An environment defines the software dependencies needed to run the model and entry script.

以下示例演示如何使用 SDK 创建环境,然后将其用于推理配置:The following example shows how to use the SDK to create an environment and then use it with an inference configuration:

from azureml.core.model import InferenceConfig
from azureml.core.environment import Environment
from azureml.core.conda_dependencies import CondaDependencies

# Create the environment
myenv = Environment(name="myenv")
conda_dep = CondaDependencies()

# Define the packages needed by the model and scripts
conda_dep.add_conda_package("tensorflow")
conda_dep.add_conda_package("numpy")
conda_dep.add_conda_package("scikit-learn")
# You must list azureml-defaults as a pip dependency
conda_dep.add_pip_package("azureml-defaults")
conda_dep.add_pip_package("keras")
conda_dep.add_pip_package("gensim")

# Adds dependencies to PythonSection of myenv
myenv.python.conda_dependencies=conda_dep

inference_config = InferenceConfig(entry_script="score.py",
                                   environment=myenv)

有关详细信息,请参阅以下文章:For more information, see the following articles:

CLI 从 YAML 文件加载推理配置:The CLI loads the inference configuration from a YAML file:

{
   "entryScript": "score.py",
   "runtime": "python",
   "condaFile": "myenv.yml"
}

使用 CLI,在推理配置所引用的 myenv.yml 文件中定义 conda 环境。With the CLI, the conda environment is defined in the myenv.yml file referenced by the inference configuration. 以下 YAML 是此文件的内容:The following YAML is the contents of this file:

name: inference_environment
dependencies:
- python=3.6.2
- tensorflow
- numpy
- scikit-learn
- pip:
    - azureml-defaults
    - keras
    - gensim

有关推理配置的详细信息,请参阅使用 Azure 机器学习部署模型For more information on inference configuration, see Deploy models with Azure Machine Learning.

入口脚本 (score.py)Entry script (score.py)

入口脚本仅包含两个必需函数(init()run(data))。The entry script has only two required functions, init() and run(data). 这两个函数用于在启动时初始化服务,并使用客户端传入的请求数据运行模型。These functions are used to initialize the service at startup and run the model using request data passed in by a client. 脚本的其余部分用于加载和运行模型。The rest of the script handles loading and running the model(s).

重要

没有适用于所有模型的通用入口脚本。There isn't a generic entry script that works for all models. 入口脚本总是特定于所使用的模型。It is always specific to the model that is used. 请务必了解如何加载模型、模型所需的数据格式,以及如何使用模型对数据进行评分。It must understand how to load the model, the data format that the model expects, and how to score data using the model.

以下 Python 代码是一个入口脚本示例 (score.py):The following Python code is an example entry script (score.py):

import os
import pickle
import json
import time
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
from gensim.models.word2vec import Word2Vec

# SENTIMENT
POSITIVE = "POSITIVE"
NEGATIVE = "NEGATIVE"
NEUTRAL = "NEUTRAL"
SENTIMENT_THRESHOLDS = (0.4, 0.7)
SEQUENCE_LENGTH = 300

# Called when the deployed service starts
def init():
    global model
    global tokenizer
    global encoder
    global w2v_model

    # Get the path where the deployed model can be found.
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), './models')
    # load models
    model = load_model(model_path + '/model.h5')
    w2v_model = Word2Vec.load(model_path + '/model.w2v')

    with open(model_path + '/tokenizer.pkl','rb') as handle:
        tokenizer = pickle.load(handle)

    with open(model_path + '/encoder.pkl','rb') as handle:
        encoder = pickle.load(handle)

# Handle requests to the service
def run(data):
    try:
        # Pick out the text property of the JSON request.
        # This expects a request in the form of {"text": "some text to score for sentiment"}
        data = json.loads(data)
        prediction = predict(data['text'])
        #Return prediction
        return prediction
    except Exception as e:
        error = str(e)
        return error

# Determine sentiment from score
def decode_sentiment(score, include_neutral=True):
    if include_neutral:
        label = NEUTRAL
        if score <= SENTIMENT_THRESHOLDS[0]:
            label = NEGATIVE
        elif score >= SENTIMENT_THRESHOLDS[1]:
            label = POSITIVE
        return label
    else:
        return NEGATIVE if score < 0.5 else POSITIVE

# Predict sentiment using the model
def predict(text, include_neutral=True):
    start_at = time.time()
    # Tokenize text
    x_test = pad_sequences(tokenizer.texts_to_sequences([text]), maxlen=SEQUENCE_LENGTH)
    # Predict
    score = model.predict([x_test])[0]
    # Decode sentiment
    label = decode_sentiment(score, include_neutral=include_neutral)

    return {"label": label, "score": float(score),
       "elapsed_time": time.time()-start_at}  

有关入口脚本的详细信息,请参阅使用 Azure 机器学习部署模型For more information on entry scripts, see Deploy models with Azure Machine Learning.

定义部署Define deployment

Webservice 包中包含部署所用的类。The Webservice package contains the classes used for deployment. 所用的类确定模型的部署位置。The class you use determines where the model is deployed. 例如,要在 Azure Kubernetes Service 上部署为 Web 服务,可使用 AksWebService.deploy_configuration() 来创建部署配置。For example, to deploy as a web service on Azure Kubernetes Service, use AksWebService.deploy_configuration() to create the deployment configuration.

以下 Python 代码用于定义本地部署的部署配置。The following Python code defines a deployment configuration for a local deployment. 此配置将模型作为 Web 服务部署到你的本地计算机中。This configuration deploys the model as a web service to your local computer.

重要

本地部署要求在本地计算机上安装有效的 DockerA local deployment requires a working installation of Docker on your local computer:

from azureml.core.webservice import LocalWebservice

deployment_config = LocalWebservice.deploy_configuration()

有关详细信息,请参阅 LocalWebservice.deploy_configuration() 参考。For more information, see the LocalWebservice.deploy_configuration() reference.

CLI 从 YAML 文件加载部署配置:The CLI loads the deployment configuration from a YAML file:

{
    "computeType": "LOCAL"
}

部署到不同的计算目标(如 Azure 云中的 Azure Kubernetes 服务)与更改部署配置一样简单。Deploying to a different compute target, such as Azure Kubernetes Service in the Azure cloud, is as easy as changing the deployment configuration. 有关详细信息,请参阅部署模型的方式和位置For more information, see How and where to deploy models.

部署模型Deploy the model

以下示例加载名为 sentiment 的注册模型的信息,然后将其部署为名为 sentiment 的服务。The following example loads information on the registered model named sentiment, and then deploys it as a service named sentiment. 在部署期间,使用推理配置和部署配置来创建和配置服务环境:During deployment, the inference configuration and deployment configuration are used to create and configure the service environment:

from azureml.core.model import Model

model = Model(ws, name='sentiment')
service = Model.deploy(ws, 'myservice', [model], inference_config, deployment_config)

service.wait_for_deployment(True)
print(service.state)
print("scoring URI: " + service.scoring_uri)

有关详细信息,请参阅 Model.deploy() 参考。For more information, see the Model.deploy() reference.

若要从 CLI 部署模型,请使用以下命令。To deploy the model from the CLI, use the following command. 此命令使用存储在 inferenceConfig.jsondeploymentConfig.json 文件中的推理配置和部署配置来部署已注册模型 (sentiment:1) 的版本 1:This command deploys version 1 of the registered model (sentiment:1) using the inference and deployment configuration stored in the inferenceConfig.json and deploymentConfig.json files:

az ml model deploy -n myservice -m sentiment:1 --ic inferenceConfig.json --dc deploymentConfig.json

有关详细信息,请参阅 az ml model deploy 参考文档。For more information, see the az ml model deploy reference.

有关部署的详细信息,请参阅部署模型的方式和位置For more information on deployment, see How and where to deploy models.

请求-响应的使用Request-response consumption

部署完成后将显示评分 URI。After deployment, the scoring URI is displayed. 客户端可使用此 URI 将请求提交到服务。This URI can be used by clients to submit requests to the service. 以下示例是一个简单的 Python 客户端,该客户端将数据提交到服务并显示响应:The following example is a simple Python client that submits data to the service and displays the response:

import requests
import json

scoring_uri = 'scoring uri for your service'
headers = {'Content-Type':'application/json'}

test_data = json.dumps({'text': 'Today is a great day!'})

response = requests.post(scoring_uri, data=test_data, headers=headers)
print(response.status_code)
print(response.elapsed)
print(response.json())

有关如何使用已部署服务的详细信息,请参阅创建客户端For more information on how to consume the deployed service, see Create a client.

后续步骤Next steps