使用 Azure Cache for Redis 将机器学习模型部署到 Azure FunctionsDeploy a machine learning model to Azure Functions with Azure Cache for Redis

本文将使用 Azure Cache for Redis 实例,将模型作为函数应用从 Azure 机器学习部署到 Azure Functions。In this article, you will deploy a model from Azure Machine Learning as a function app in Azure Functions using an Azure Cache for Redis instance.

Azure Cache for Redis 具有极高的性能和可伸缩性 - 在与 Azure 机器学习模型配对时,可以在应用程序中获得低延迟和高吞吐量。Azure Cache for Redis is extremely performant and scalable - when paired with an Azure Machine Learning model, you gain low latency and high throughput in your application. 几种情况下缓存特别有用,例如,可以将缓存用于数据推理和实际的模型推理结果。A couple scenarios where a cache is particularly beneficial is when inferencing the data and for the actual model inference results. 在任一情况下,元数据或结果存储在内存中,因而提高了性能。In either scenario, the meta data or results are stored in-memory, which leads to increased performance.

备注

虽然 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.

先决条件Prerequisites

重要

本文中的代码片段假设你已设置以下变量: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.

创建用于 Redis 的 Azure 缓存实例Create an Azure Cache for Redis instance

你将能够使用任何基本、标准或高级缓存实例将机器学习模型部署到 Azure Functions。You’ll be able to deploy a machine learning model to Azure Functions with any Basic, Standard, or Premium cache instance. 若要创建缓存实例,请执行以下步骤。To create a cache instance, follow these steps.

  1. 转到 Azure 门户主页或打开边栏菜单,然后选择“创建资源”。Go to the Azure portal homepage or open the sidebar menu, then select Create a resource.

  2. 在“新建”页上选择“数据库”,然后选择“Azure Cache for Redis”。On the New page, select Databases and then select Azure Cache for Redis.

    选择 Azure Cache for Redis。

  3. 在“新建 Redis 缓存”页上配置新缓存的设置。On the New Redis Cache page, configure the settings for your new cache.

    设置Setting 建议的值Suggested value 说明Description
    DNS 名称DNS name 输入任何全局唯一的名称。Enter a globally unique name. 缓存名称必须是包含 1 到 63 个字符的字符串,只能包含数字、字母或连字符。The cache name must be a string between 1 and 63 characters that contains only numbers, letters, or hyphens. 该名称必须以数字或字母开头和结尾,且不能包含连续的连字符。The name must start and end with a number or letter, and can't contain consecutive hyphens. 缓存实例的主机名将是 <DNS name>.redis.cache.chinacloudapi.cnYour cache instance's host name will be <DNS name>.redis.cache.chinacloudapi.cn.
    订阅Subscription 单击下拉箭头并选择你的订阅。Drop down and select your subscription. 要在其下创建此新的 Azure Cache for Redis 实例的订阅。The subscription under which to create this new Azure Cache for Redis instance.
    资源组Resource group 单击下拉箭头并选择一个资源组,或者选择“新建”并输入新的资源组名称。Drop down and select a resource group, or select Create new and enter a new resource group name. 要在其中创建缓存和其他资源的资源组的名称。Name for the resource group in which to create your cache and other resources. 将所有应用资源放入一个资源组可以轻松地统一管理或删除这些资源。By putting all your app resources in one resource group, you can easily manage or delete them together.
    位置Location 单击下拉箭头并选择一个位置。Drop down and select a location. 选择与要使用该缓存的其他服务靠近的区域Select a region near other services that will use your cache.
    定价层Pricing tier 单击下拉箭头并选择一个定价层Drop down and select a Pricing tier. 定价层决定可用于缓存的大小、性能和功能。The pricing tier determines the size, performance, and features that are available for the cache. 有关详细信息,请参阅用于 Redis 的 Azure 缓存概述For more information, see Azure Cache for Redis Overview.
  4. 选择“网络”选项卡,或单击页面底部的“网络”按钮 。Select the Networking tab or click the Networking button at the bottom of the page.

  5. 在“网络”选项卡中,选择你的连接方法。In the Networking tab, select your connectivity method.

  6. 选择页面底部的“下一步:高级”选项卡,或者单击页面底部的“下一步:高级”按钮。Select the Next: Advanced tab or click the Next: Advanced button on the bottom of the page.

  7. 在基本或标准缓存实例的“高级”选项卡中,如果想要启用非 TLS 端口,请选择启用开关。In the Advanced tab for a basic or standard cache instance, select the enable toggle if you want to enable a non-TLS port.

  8. 在高级缓存实例的“高级”选项卡中,配置非 TLS 端口、群集和数据持久性的设置。In the Advanced tab for premium cache instance, configure the settings for non-TLS port, clustering, and data persistence.

  9. 选择页面底部的“下一步:标记”选项卡,或者单击“下一步:标记”按钮。Select the Next: Tags tab or click the Next: Tags button at the bottom of the page.

  10. 或者,在“标记”选项卡中,如果希望对资源分类,请输入名称或值。Optionally, in the Tags tab, enter the name and value if you wish to categorize the resource.

  11. 选择“查看 + 创建” 。Select Review + create. 随后你会转到“查看 + 创建”选项卡,Azure 将在此处验证配置。You're taken to the Review + create tab where Azure validates your configuration.

  12. 显示绿色的“已通过验证”消息后,选择“创建”。After the green Validation passed message appears, select Create.

创建缓存需要花费片刻时间。It takes a while for the cache to create. 可以在 Azure Cache for Redis 的“概述”页上监视进度。 You can monitor progress on the Azure Cache for Redis Overview page. 如果“状态”显示为“正在运行”,则表示该缓存可供使用。 When Status shows as Running, the cache is ready to use.

准备部署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 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.

对于 run 函数,请确保它连接到 Redis 终结点。For the run function, ensure it connects to a Redis endpoint.

import json
import numpy as np
import os
import redis
from sklearn.externals import joblib

def init():
    global model
    global azrediscache
    azrediscache = redis.StrictRedis(host='<host_url>', port=6380, password="<access_key>", ssl=True)
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'sklearn_mnist_model.pkl')
    model = joblib.load(model_path)

@input_schema('data', NumpyParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))
def run(data):
    try:
        input = azrediscache.get(data)
        result = model.predict(input)
        data = np.array(json.loads(data))
        result = model.predict(data)
        # You can return any data type, as long as it is JSON serializable.
        return result.tolist()
    except Exception as e:
        error = str(e)
        return error

有关入口脚本的详细信息,请参阅定义评分代码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', 'redis'])
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 预览版包以获取 Functions 支持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. 下面的代码片段演示如何通过模型和推理配置创建包含 HTTP 触发器的新包:The following code snippet demonstrates how to create a new package with a HTTP 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 HTTP_TRIGGER
model_package = package(ws, [model], inference_config, functions_enabled=True, trigger=HTTP_TRIGGER)
model_package.wait_for_creation(show_output=True)
# Display the package location/ACR path
print(model_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@sha256:<imagename> 格式。The location returned is in the format <acrinstance>.azurecr.io/package@sha256:<imagename>.

备注

Functions 打包当前支持 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> 替换为之前从 package.location 返回的值:Replace <myacr> with the 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 North"
    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 chinanorth --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 容器注册表提供 HTTP 触发器的连接字符串或凭据,因此函数应用未处于活动状态。However, since you haven't provided the connection string for the HTTP 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. 若要为函数应用提供访问容器注册表所需的凭据,请使用以下命令。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.

测试 Azure Functions HTTP 触发器Test Azure Functions HTTP trigger

现在,我们将运行并测试 Azure Functions HTTP 触发器。We will now run and test our Azure Functions HTTP trigger.

  1. 在 Azure 门户中转到函数应用。Go to your function app in the Azure portal.
  2. 在“开发人员”下,选择“代码 + 测试”。Under developer, select Code + Test.
  3. 在右侧,选择“输入”选项卡。On the right hand side, select the Input tab.
  4. 单击“运行”按钮,以测试 Azure Functions HTTP 触发器。Click on the Run button to test the Azure Functions HTTP trigger.

现在,你已使用 Azure Cache for Redis 实例成功地将 Azure 机器学习中的模型部署为函数应用。You have now successfully deployed a model from Azure Machine Learning as a function app using an Azure Cache for Redis instance. 通过导航到以下部分中的链接,详细了解 Azure Cache for Redis。Learn more about Azure Cache for Redis by navigating to the links in the section below.

清理资源Clean up resources

如果想要继续学习下一篇教程,可以保留本快速入门中创建的资源,以便重复使用。If you're continuing to the next tutorial, you can keep the resources that you created in this quickstart and reuse them.

否则,如果你已完成本快速入门,可以删除本快速入门中创建的 Azure 资源,以免产生费用。Otherwise, if you're finished with the quickstart, you can delete the Azure resources that you created in this quickstart to avoid charges.

重要

删除资源组的操作不可逆。Deleting a resource group is irreversible. 删除资源组时,包含在其中的所有资源会被永久删除。When you delete a resource group, all the resources in it are permanently deleted. 请确保不会意外删除错误的资源组或资源。Make sure that you do not accidentally delete the wrong resource group or resources. 如果在现有资源组(其中包含要保留的资源)中为托管此示例而创建了相关资源,可从各自的边栏选项卡逐个删除这些资源,而不要删除资源组。If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually from their respective blades instead of deleting the resource group.

删除资源组的步骤To delete a resource group

  1. 登录到 Azure 门户,然后选择“资源组”。Sign in to the Azure portal, and then select Resource groups.

  2. 在“按名称筛选...”框中键入资源组的名称。In the Filter by name... box, type the name of your resource group. 在资源组的结果列表中选择“...”,然后选择“删除资源组” 。On your resource group, in the results list, select ..., and then select Delete resource group.

系统会要求确认是否删除资源组。You're asked to confirm the deletion of the resource group. 键入资源组的名称进行确认,然后选择“删除”。Type the name of your resource group to confirm, and then select Delete.

片刻之后,将会删除该资源组及其所有资源。After a few moments, the resource group and all of its resources are deleted.

后续步骤Next steps