将机器学习模型部署到 Azure 应用服务(预览版)Deploy a machine learning model to Azure App Service (preview)

适用于:是基本版是企业版               (升级到企业版APPLIES TO: yesBasic edition yesEnterprise edition                    (Upgrade to Enterprise edition)

了解如何从 Azure 机器学习将模型部署为 Azure 应用服务中的 Web 应用。Learn how to deploy a model from Azure Machine Learning as a web app in Azure App Service.

重要

尽管 Azure 机器学习和 Azure 应用服务都已正式发布,但将模型从机器学习服务部署到应用服务的功能处于预览阶段。While both Azure Machine Learning and Azure App Service are generally available, the ability to deploy a model from the Machine Learning service to App Service is in preview.

使用 Azure 机器学习,可通过经过训练的机器学习模型创建 Docker 映像。With Azure Machine Learning, you can create Docker images from trained machine learning models. 此映像包含一个 Web 服务,它会接收数据,将数据提交到模型,然后返回响应。This image contains a web service that receives data, submits it to the model, and then returns the response. Azure 应用服务可用于部署映像,并提供以下功能:Azure App Service can be used to deploy the image, and provides the following features:

  • 高级身份验证,可增强安全性。Advanced authentication for enhanced security. 身份验证方法包括 Azure Active Directory 和多重身份验证。Authentication methods include both Azure Active Directory and multi-factor auth.
  • 无需重新部署即可自动缩放Autoscale without having to redeploy.
  • SSL 支持,可在客户端和服务之间实现安全通信。SSL support for secure communications between clients and the service.

有关 Azure 应用服务提供的功能的详细信息,请参阅应用服务概述For more information on features provided by Azure App Service, see the App Service overview.

重要

如果需要记录已部署模型所使用的评分数据或评分结果的功能,应改为部署到 Azure Kubernetes 服务。If you need the ability to log the scoring data used with your deployed model, or the results of scoring, you should instead deploy to Azure Kubernetes Service. 有关详细信息,请参阅在生产模型上收集数据For more information, see Collect data on your production models.

必备条件Prerequisites

  • Azure 机器学习工作区。An Azure Machine Learning workspace. 有关详细信息,请参阅创建工作区一文。For more information, see the Create a workspace article.

  • Azure CLIThe Azure CLI.

  • 工作区中注册的已训练的机器学习模型。A trained machine learning model registered in your workspace. 如果没有模型,请使用图像分类教程:训练模型来训练和注册模型。If you do not have a model, use the Image classification tutorial: train model to train and register one.

    重要

    本文中的代码片段假设你已设置以下变量:The code snippets in this article assume that you have set the following variables:

    • ws - Azure 机器学习工作区。ws - Your Azure Machine Learning workspace.
    • model - 将要部署的注册模型。model - The registered model that will be deployed.
    • inference_config - 用于模型的推理配置。inference_config - The inference configuration for the model.

    有关设置这些变量的详细信息,请参阅使用 Azure 机器学习部署模型For more information on setting these variables, see Deploy models with Azure Machine Learning.

准备部署Prepare for deployment

在部署之前,必须确定将模型作为 Web 服务运行所需的内容。Before deploying, you must define what is needed to run the model as a web service. 以下列表描述了部署所需的基本项:The following list describes the basic items needed for a deployment:

  • 一个入口脚本 。An entry script. 此脚本接受请求,使用模型为请求评分并返回结果。This script accepts requests, scores the request using the model, and returns the results.

    重要

    入口脚本特定于你的模型;它必须能够识别传入请求数据的格式、模型所需数据的格式以及返回给客户端的数据的格式。The entry script is specific to your model; it must understand the format of the incoming request data, the format of the data expected by your model, and the format of the data returned to clients.

    如果请求数据的格式对模型不可用,则该脚本可以将其转换为可接受的格式。If the request data is in a format that is not usable by your model, the script can transform it into an acceptable format. 在将响应返回给客户端之前,它还可以对响应进行转换。It may also transform the response before returning to it to the client.

    重要

    Azure 机器学习 SDK 不为 Web 服务提供访问数据存储或数据集的方法。The Azure Machine Learning SDK does not provide a way for the web service access your datastore or data sets. 如果需要部署的模型访问存储在部署外的数据,例如 Azure 存储帐户中的数据,则必须使用相关的 SDK 开发自定义代码解决方案。If you need the deployed model to access data stored outside the deployment, such as in an Azure Storage account, you must develop a custom code solution using the relevant SDK. 例如,用于 Python 的 Azure 存储 SDKFor example, the Azure Storage SDK for Python.

    可能适用于该方案的另一种方法是批量预测,它在评分时提供对数据存储的访问权限。Another alternative that may work for your scenario is batch predictions, which does provide access to datastores when scoring.

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

  • 依赖项,如运行入口脚本或模型所需的帮助程序脚本或 Python/Conda 包 Dependencies, such as helper scripts or Python/Conda packages required to run the entry script or model

这些实体被封装到推理配置中 。These entities are encapsulated into an inference configuration. 推理配置引用入口脚本和其他依赖项。The inference configuration references the entry script and other dependencies.

重要

创建用于 Azure 应用服务的推理配置时,必须使用 Environment 对象。When creating an inference configuration for use with Azure App Service, you must use an Environment object. 请注意,如果要定义自定义环境,需要将版本不低于 1.0.45 的 azureml-defaults 添加为 Pip 依赖项。Please note that if you are defining a custom environment, you must add azureml-defaults with version >= 1.0.45 as a pip dependency. 此包包含将模型托管为 Web 服务所需的功能。This package contains the functionality needed to host the model as a web service. 下面的示例演示如何创建环境对象并将其用于推理配置:The following example demonstrates creating an environment object and using it with an inference configuration:

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

# Create an environment and add conda dependencies to it
myenv = Environment(name="myenv")
# Enable Docker based environment
myenv.docker.enabled = True
# Build conda dependencies
myenv.python.conda_dependencies = CondaDependencies.create(conda_packages=['scikit-learn'],
                                                           pip_packages=['azureml-defaults'])
inference_config = InferenceConfig(entry_script="score.py", environment=myenv)

有关环境的详细信息,请参阅创建和管理用于训练和部署的环境For more information on environments, see Create and manage environments for training and deployment.

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

重要

部署到 Azure 应用服务时,无需创建部署配置 。When deploying to Azure App Service, you do not need to create a deployment configuration.

创建映像Create the image

若要创建部署到 Azure 应用服务的 Docker 映像,请使用 Model.packageTo create the Docker image that is deployed to Azure App Service, use Model.package. 下面的代码片段演示如何从模型和推理配置生成新的映像:The following code snippet demonstrates how to build a new image from the model and inference configuration:

备注

代码段假定 model 包含已注册的模型,inference_config 包含推理环境的配置。The code snippet assumes that model contains a registered model, and that inference_config contains the configuration for the inference environment. 有关详细信息,请参阅使用 Azure 机器学习部署模型For more information, see Deploy models with Azure Machine Learning.

from azureml.core import Model

package = Model.package(ws, [model], inference_config)
package.wait_for_creation(show_output=True)
# Display the package location/ACR path
print(package.location)

show_output=True 时,将显示 Docker 生成过程的输出。When show_output=True, the output of the Docker build process is shown. 此过程完成后,即在 Azure 容器注册表中为工作区创建了映像。Once the process finishes, the image has been created in the Azure Container Registry for your workspace. 映像生成后,会显示其在 Azure 容器注册表中的位置。Once the image has been built, the location in your Azure Container Registry is displayed. 返回的位置采用 <acrinstance>.azurecr.io/package:<imagename> 格式。The location returned is in the format <acrinstance>.azurecr.io/package:<imagename>. 例如,myml08024f78fd10.azurecr.io/package:20190827151241For example, myml08024f78fd10.azurecr.io/package:20190827151241.

重要

保存位置信息,因为会在部署映像时使用。Save the location information, as it is used when deploying the image.

将映像部署为 Web 应用Deploy image as a web app

  1. 使用以下命令获取包含映像的 Azure 容器注册表的登录凭据。Use the following command to get the login credentials for the Azure Container Registry that contains the image. <acrinstance> 替换为之前从 package.location 返回的值:Replace <acrinstance> with th e value returned previously from package.location:

    az acr credential show --name <myacr>
    

    此命令的输出类似于以下 JSON 文档:The output of this command is similar to the following JSON document:

    {
    "passwords": [
        {
        "name": "password",
        "value": "Iv0lRZQ9762LUJrFiffo3P4sWgk4q+nW"
        },
        {
        "name": "password2",
        "value": "=pKCxHatX96jeoYBWZLsPR6opszr==mg"
        }
    ],
    "username": "myml08024f78fd10"
    }
    

    保存“用户名”和某个“密码”的值 。Save the value for username and one of the passwords.

  2. 如果还没有用于部署服务的资源组或应用服务计划,可使用以下命令来创建这两项:If you do not already have a resource group or app service plan to deploy the service, the following commands demonstrate how to create both:

    az group create --name myresourcegroup --location "China East"
    az appservice plan create --name myplanname --resource-group myresourcegroup --sku B1 --is-linux
    

    在此示例中,使用了基本定价层 (--sku B1) 。In this example, a Basic pricing tier (--sku B1) is used.

    重要

    Azure 机器学习创建的映像使用 Linux,因此需要使用 --is-linux 参数。Images created by Azure Machine Learning use Linux, so you must use the --is-linux parameter.

  3. 若要创建 Web 应用,请使用以下命令。To create the web app, use the following command. <app-name> 替换为要使用的名称。Replace <app-name> with the name you want to use. <acrinstance><imagename> 替换为之前返回的 package.location 的值:Replace <acrinstance> and <imagename> with the values from returned package.location earlier:

    az webapp create --resource-group myresourcegroup --plan myplanname --name <app-name> --deployment-container-image-name <acrinstance>.azurecr.io/package:<imagename>
    

    此命令将返回类似以下 JSON 文档的信息:This command returns information similar to the following JSON document:

    { 
    "adminSiteName": null,
    "appServicePlanName": "myplanname",
    "geoRegion": "China East",
    "hostingEnvironmentProfile": null,
    "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myplanname",
    "kind": "linux",
    "location": "China East",
    "maximumNumberOfWorkers": 1,
    "name": "myplanname",
    < JSON data removed for brevity. >
    "targetWorkerSizeId": 0,
    "type": "Microsoft.Web/serverfarms",
    "workerTierName": null
    }
    

    重要

    此时,已创建了 Web 应用。At this point, the web app has been created. 但是,由于你尚未向包含映像的 Azure 容器注册表提供凭据,因此 Web 应用未处于活动状态。However, since you haven't provided the credentials to the Azure Container Registry that contains the image, the web app is not active. 在下一步中,为容器注册表提供身份验证信息。In the next step, you provide the authentication information for the container registry.

  4. 若要为 Web 应用提供访问容器注册表所需的凭据,请使用以下命令。To provide the web app with the credentials needed to access the container registry, use the following command. <app-name> 替换为要使用的名称。Replace <app-name> with the name you want to use. <acrinstance><imagename> 替换为之前返回的 package.location 的值。Replace <acrinstance> and <imagename> with the values from returned package.location earlier. <username><password> 替换为之前检索到的 ACR 登录信息:Replace <username> and <password> with the ACR login information retrieved earlier:

    az webapp config container set --name <app-name> --resource-group myresourcegroup --docker-custom-image-name <acrinstance>.azurecr.io/package:<imagename> --docker-registry-server-url https://<acrinstance>.azurecr.io --docker-registry-server-user <username> --docker-registry-server-password <password>
    

    此命令将返回类似以下 JSON 文档的信息:This command returns information similar to the following JSON document:

    [
    {
        "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
        "slotSetting": false,
        "value": "false"
    },
    {
        "name": "DOCKER_REGISTRY_SERVER_URL",
        "slotSetting": false,
        "value": "https://myml08024f78fd10.azurecr.io"
    },
    {
        "name": "DOCKER_REGISTRY_SERVER_USERNAME",
        "slotSetting": false,
        "value": "myml08024f78fd10"
    },
    {
        "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
        "slotSetting": false,
        "value": null
    },
    {
        "name": "DOCKER_CUSTOM_IMAGE_NAME",
        "value": "DOCKER|myml08024f78fd10.azurecr.io/package:20190827195524"
    }
    ]
    

此时,Web 应用开始加载映像。At this point, the web app begins loading the image.

重要

加载映像可能需要几分钟时间。It may take several minutes before the image has loaded. 若要监视进度,请使用以下命令:To monitor progress, use the following command:

az webapp log tail --name <app-name> --resource-group myresourcegroup

加载映像且站点处于活动状态后,该日志将显示一条消息,表明 Container <container name> for site <app-name> initialized successfully and is ready to serve requestsOnce the image has been loaded and the site is active, the log displays a message that states Container <container name> for site <app-name> initialized successfully and is ready to serve requests.

部署映像后,可以使用以下命令查找主机名:Once the image is deployed, you can find the hostname by using the following command:

az webapp show --name <app-name> --resource-group myresourcegroup

此命令将返回类似以下主机名的信息 - <app-name>.chinacloudsites.cnThis command returns information similar to the following hostname - <app-name>.chinacloudsites.cn. 将此值用作服务基 URL 的一部分 。Use this value as part of the base url for the service.

使用 Web 应用Use the Web App

向模型传递请求的 Web 服务位于 {baseurl}/scoreThe web service that passes requests to the model is located at {baseurl}/score. 例如,https://<app-name>.chinacloudsites.cn/scoreFor example, https://<app-name>.chinacloudsites.cn/score. 以下 Python 代码演示了如何将数据提交到 URL 并显示响应:The following Python code demonstrates how to submit data to the URL and display the response:

import requests
import json

scoring_uri = "https://mywebapp.chinacloudsites.cn/score"

headers = {'Content-Type':'application/json'}

test_sample = json.dumps({'data': [
    [1,2,3,4,5,6,7,8,9,10],
    [10,9,8,7,6,5,4,3,2,1]
]})

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

后续步骤Next steps