将机器学习模型部署到 Azure Functions(预览版)Deploy a machine learning model to Azure Functions (preview)

了解如何将 Azure 机器学习中的模型部署为 Azure Functions 中的函数应用。Learn how to deploy a model from Azure Machine Learning as a function app in Azure Functions.

重要

虽然 Azure 机器学习和 Azure Functions 都已正式发布,但将模型从机器学习服务部署到 Functions 的功能目前处于预览阶段。While both Azure Machine Learning and Azure Functions are generally available, the ability to package a model from the Machine Learning service for Functions is in preview.

使用 Azure 机器学习,可通过经过训练的机器学习模型创建 Docker 映像。With Azure Machine Learning, you can create Docker images from trained machine learning models. Azure 机器学习现在提供了将这些机器学习模型生成到函数应用中的预览功能,而这些函数应用可部署到 Azure Functions 中。Azure Machine Learning now has the preview functionality to build these machine learning models into function apps, which can be deployed into Azure Functions.

必备条件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 core 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.

    默认情况下,在为函数打包时,输入被视为文本。By default when packaging for functions, the input is treated as text. 如果想使用输入的原始字节(例如用于 Blob 触发器),则应使用 AMLRequest 接受原始数据If you are interested in consuming the raw bytes of the input (for instance for Blob triggers), you should use AMLRequest to accept raw data.

有关入口脚本的详细信息,请参阅定义评分代码For more information on entry script, see Define scoring code

  • 依赖项,如运行入口脚本或模型所需的帮助程序脚本或 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 Functions 的推理配置时,需要使用 Environment 对象。When creating an inference configuration for use with Azure Functions, 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

# 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 Functions 时,无需创建部署配置。When deploying to Functions, you do not need to create a deployment configuration.

安装 SDK 预览版包以获取函数支持Install the SDK preview package for functions support

若要为 Azure Functions 生成包,需要安装 SDK 预览版包。To build packages for Azure Functions, you must install the SDK preview package.

pip install azureml-contrib-functions

创建映像Create the image

若想创建要部署到 Azure Functions 的 Docker 映像,请为想应用的触发器使用 azureml.contrib.functions.package 或特定包函数。To create the Docker image that is deployed to Azure Functions, use azureml.contrib.functions.package or the specific package function for the trigger you are interested in using. 下面的代码段演示如何通过模型和推理配置生成带有 blob 触发器的新包:The following code snippet demonstrates how to create a new package with a blob trigger 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.contrib.functions import package
from azureml.contrib.functions import BLOB_TRIGGER
blob = package(ws, [model], inference_config, functions_enabled=True, trigger=BLOB_TRIGGER, input_path="input/{blobname}.json", output_path="output/{blobname}_out.json")
blob.wait_for_creation(show_output=True)
# Display the package location/ACR path
print(blob.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@sha256:<imagename> 格式。The location returned is in the format <acrinstance>.azurecr.io/package@sha256:<imagename>.

备注

函数打包当前支持 HTTP 触发器、Blob 触发器和服务总线触发器。Packaging for functions currently supports HTTP Triggers, Blob triggers and Service bus triggers. 有关触发器的详细信息,请参阅 Azure Functions 绑定For more information on triggers, see Azure Functions bindings.

重要

保存位置信息,因为会在部署映像时使用。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. <myacr> 替换为之前从 blob.location 返回的值:Replace <myacr> with the value returned previously from blob.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
    

    此示例使用了“Linux 基本”定价层级 (--sku B1)。In this example, a Linux 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 作业存储的存储帐户并获取其连接字符串。Create the storage account to use for the web job storage and get its connection string. <webjobStorage> 替换为要使用的名称。Replace <webjobStorage> with the name you want to use.

    az storage account create --name <webjobStorage> --location chinaeast --resource-group myresourcegroup --sku Standard_LRS
    
    az storage account show-connection-string --resource-group myresourcegroup --name <webJobStorage> --query connectionString --output tsv
    
  4. 使用以下命令来创建函数应用。To create the function 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. <webjobStorage> 替换为上一步中存储帐户的名称:Replace <webjobStorage> with the name of the storage account from the previous step:

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

    重要

    此时,函数应用已创建。At this point, the function app has been created. 但是,由于你尚未向包含映像的 Azure 容器注册表提供 blob 触发器的连接字符串或凭据,因此函数应用未处于活动状态。However, since you haven't provided the connection string for the blob trigger or credentials to the Azure Container Registry that contains the image, the function app is not active. 在接下来的步骤中,为容器注册表提供连接字符串和身份验证信息。In the next steps, you provide the connection string and the authentication information for the container registry.

  5. 创建要用于 blob 触发器存储的存储帐户并获取其连接字符串。Create the storage account to use for the blob trigger storage and get its connection string. <triggerStorage> 替换为要使用的名称。Replace <triggerStorage> with the name you want to use.

    az storage account create --name <triggerStorage> --location chinaeast --resource-group myresourcegroup --sku Standard_LRS
    
    az storage account show-connection-string --resource-group myresourcegroup --name <triggerStorage> --query connectionString --output tsv
    

    记录要提供给函数应用的此连接字符串。Record this connection string to provide to the function app. 稍后在要求提供 <triggerConnectionString> 时会用到该字符串We will use it later when we ask for <triggerConnectionString>

  6. 创建用于存储帐户中输入和输出信息的容器。Create the containers for the input and output in the storage account. <triggerConnectionString> 替换为先前返回的连接字符串:Replace <triggerConnectionString> with the connection string returned earlier:

    az storage container create -n input --connection-string <triggerConnectionString>
    
    az storage container create -n output --connection-string <triggerConnectionString>
    
  7. 若要将触发器连接字符串与函数应用关联,请使用以下命令。To associate the trigger connection string with the function app, use the following command. <app-name> 替换为函数应用的名称。Replace <app-name> with the name of the function app. <triggerConnectionString> 替换为先前返回的连接字符串:Replace <triggerConnectionString> with the connection string returned earlier:

    az functionapp config appsettings set --name <app-name> --resource-group myresourcegroup --settings "TriggerConnectionString=<triggerConnectionString>"
    
  8. 需要使用以下命令检索与创建的容器关联的标记。You will need to retrieve the tag associated with the created container using the following command. <username> 替换为先前从容器注册表返回的用户名:Replace <username> with the username returned earlier from the container registry:

    az acr repository show-tags --repository package --name <username> --output tsv
    

    保存返回的值,它将在下一步中用作 imagetagSave the value returned, it will be used as the imagetag in the next step.

  9. 若要为函数应用提供访问容器注册表所需的凭据,请使用以下命令。To provide the function app with the credentials needed to access the container registry, use the following command. <app-name> 替换为函数应用的名称。Replace <app-name> with the name of the function app. <acrinstance><imagetag> 替换为上一步中 AZ CLI 调用中的值。Replace <acrinstance> and <imagetag> with the values from the AZ CLI call in the previous step. <username><password> 替换为之前检索到的 ACR 登录信息:Replace <username> and <password> with the ACR login information retrieved earlier:

    az functionapp config container set --name <app-name> --resource-group myresourcegroup --docker-custom-image-name <acrinstance>.azurecr.io/package:<imagetag> --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"
    }
    ]
    

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

重要

加载映像可能需要几分钟时间。It may take several minutes before the image has loaded. 可以使用 Azure 门户监视进度。You can monitor progress using the Azure Portal.

测试部署Test the deployment

加载映像并且应用可用后,使用以下步骤来触发应用:Once the image has loaded and the app is available, use the following steps to trigger the app:

  1. 创建一个文本文件,使其包含 score.py 文件所需的数据。Create a text file that contains the data that the score.py file expects. 以下示例中的 score.py 需要其中包含 10 个数字的数组:The following example would work with a score.py that expects an array of 10 numbers:

    {"data": [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]]}
    

    重要

    数据的格式取决于你的 score.py 和模型所需的内容。The format of the data depends on what your score.py and model expects.

  2. 使用以下命令将此文件上传到之前创建的触发器存储 blob 中的输入容器。Use the following command to upload this file to the input container in the trigger storage blob created earlier. <file> 替换为包含数据的文件的名称。Replace <file> with the name of the file containing the data. <triggerConnectionString> 替换为先前返回的连接字符串。Replace <triggerConnectionString> with the connection string returned earlier. 在此示例中,input 是前面创建的输入容器的名称。In this example, input is the name of the input container created earlier. 如果你使用了其他名称,请替换此值:If you used a different name, replace this value:

    az storage blob upload --container-name input --file <file> --name <file> --connection-string <triggerConnectionString>
    

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

    {
    "etag": "\"0x8D7C21528E08844\"",
    "lastModified": "2020-03-06T21:27:23+00:00"
    }
    
  3. 若要查看函数生成的输出,请使用以下命令列出生成的输出文件。To view the output produced by the function, use the following command to list the output files generated. <triggerConnectionString> 替换为先前返回的连接字符串。Replace <triggerConnectionString> with the connection string returned earlier. 在此示例中,output 是前面创建的输出容器的名称。In this example, output is the name of the output container created earlier. 如果你使用了其他名称,请替换此值:If you used a different name, replace this value::

    az storage blob list --container-name output --connection-string <triggerConnectionString> --query '[].name' --output tsv
    

    此命令的输出类似于 sample_input_out.jsonThe output of this command is similar to sample_input_out.json.

  4. 若要下载文件并检查内容,请使用以下命令。To download the file and inspect the contents, use the following command. <file> 替换为上一命令返回的文件名。Replace <file> with the file name returned by the previous command. <triggerConnectionString> 替换为先前返回的连接字符串:Replace <triggerConnectionString> with the connection string returned earlier:

    az storage blob download --container-name output --file <file> --name <file> --connection-string <triggerConnectionString>
    

    在该命令完成后,打开文件。Once the command completes, open the file. 它包含模型返回的数据。It contains the data returned by the model.

有关使用 blob 触发器的详细信息,请参阅创建 Azure Blob 存储触发的函数一文。For more information on using blob triggers, see the Create a function triggered by Azure Blob storage article.

后续步骤Next steps