使用 Azure Machine Learning 部署模型Deploy models with Azure Machine Learning

瞭解如何將您的機器學習模型部署為 Azure 雲端中的 web 服務,或 Azure IoT Edge 裝置。Learn how to deploy your machine learning model as a web service in the Azure cloud or to Azure IoT Edge devices.

無論您部署模型的位置為何,工作流程都很類似:The workflow is similar no matter where you deploy your model:

  1. 註冊模型。Register the model.
  2. 準備部署。Prepare to deploy. (指定資產、使用量、計算目標)。(Specify assets, usage, compute target.)
  3. 將模型部署到計算目標。Deploy the model to the compute target.
  4. 測試已部署的模型,也稱為 web 服務。Test the deployed model, also called a web service.

如需部署工作流程中相關概念的詳細資訊,請參閱使用 Azure Machine Learning 來管理、部署和監視模型For more information on the concepts involved in the deployment workflow, see Manage, deploy, and monitor models with Azure Machine Learning.

必要條件Prerequisites

連接到您的工作區Connect to your workspace

下列程式碼示範如何使用快取至本機開發環境的資訊,連接到 Azure Machine Learning 的工作區:The following code shows how to connect to an Azure Machine Learning workspace by using information cached to the local development environment:

  • 使用 SDKUsing the SDK

    from azureml.core import Workspace
    ws = Workspace.from_config(path=".file-path/ws_config.json")
    

    如需使用 SDK 連線到工作區的詳細資訊,請參閱適用于 Python 的 AZURE MACHINE LEARNING SDK檔。For more information on using the SDK to connect to a workspace, see the Azure Machine Learning SDK for Python documentation.

  • 使用 CLIUsing the CLI

    使用 CLI 時,請使用-w--workspace-name參數來指定命令的工作區。When using the CLI, use the -w or --workspace-name parameter to specify the workspace for the command.

  • 使用 VS CodeUsing VS Code

    當您使用 VS Code 時,您可以使用圖形化介面來選取工作區。When you use VS Code, you select the workspace by using a graphical interface. 如需詳細資訊,請參閱 VS Code 擴充功能檔中的部署和管理模型For more information, see Deploy and manage models in the VS Code extension documentation.

註冊您的模型Register your model

已註冊的模型是組成模型的一或多個檔案的邏輯容器。A registered model is a logical container for one or more files that make up your model. 例如,如果您有儲存在多個檔案中的模型,您可以在工作區中將其註冊為單一模型。For example, if you have a model that's stored in multiple files, you can register them as a single model in the workspace. 註冊檔案之後,您就可以下載或部署已註冊的模型,並接收您註冊的所有檔案。After you register the files, you can then download or deploy the registered model and receive all the files that you registered.

提示

當您註冊模型時,您會提供雲端位置(從定型回合)或本機目錄的路徑。When you register a model, you provide the path of either a cloud location (from a training run) or a local directory. 此路徑只是為了在註冊過程中,尋找要上傳的檔案。This path is just to locate the files for upload as part of the registration process. 它不需要符合輸入腳本中使用的路徑。It doesn't need to match the path used in the entry script. 如需詳細資訊,請參閱在您的輸入腳本中尋找模型檔案。For more information, see Locate model files in your entry script.

機器學習模型會在您的 Azure Machine Learning 工作區中註冊。Machine learning models are registered in your Azure Machine Learning workspace. 模型可以來自 Azure Machine Learning 或其他地方。The model can come from Azure Machine Learning or from somewhere else. 下列範例示範如何註冊模型。The following examples demonstrate how to register a model.

從實驗執行註冊模型Register a model from an experiment run

本節中的程式碼片段會示範如何從定型回合註冊模型:The code snippets in this section demonstrate how to register a model from a training run:

重要

若要使用這些程式碼片段,您必須先執行定型回合,而且必須能夠存取Run物件(SDK 範例)或執行識別碼值(CLI 範例)。To use these snippets, you need to have previously performed a training run and you need to have access to the Run object (SDK example) or the run ID value (CLI example). 如需定型模型的詳細資訊,請參閱設定模型定型的計算目標For more information on training models, see Set up compute targets for model training.

  • 使用 SDKUsing the SDK

    當您使用 SDK 來定型模型時,您可以接收執行物件或AutoMLRun物件,視您如何訓練模型而定。When you use the SDK to train a model, you can receive either a Run object or an AutoMLRun object, depending on how you trained the model. 每個物件都可以用來註冊實驗執行所建立的模型。Each object can be used to register a model created by an experiment run.

    • azureml.core.Run物件註冊模型:Register a model from an azureml.core.Run object:

      model = run.register_model(model_name='sklearn_mnist', model_path='outputs/sklearn_mnist_model.pkl')
      print(model.name, model.id, model.version, sep='\t')
      

      model_path參數會參考模型的雲端位置。The model_path parameter refers to the cloud location of the model. 在此範例中,會使用單一檔案的路徑。In this example, the path of a single file is used. 若要在模型註冊中包含多個檔案model_path ,請將設定為包含檔案的資料夾路徑。To include multiple files in the model registration, set model_path to the path of a folder that contains the files. 如需詳細資訊,請參閱register_model檔。For more information, see the Run.register_model documentation.

    • azureml.train.automl.run.AutoMLRun物件註冊模型:Register a model from an azureml.train.automl.run.AutoMLRun object:

          description = 'My AutoML Model'
          model = run.register_model(description = description)
      
          print(run.model_id)
      

      在此範例中, metriciteration指定和參數,因此將會註冊具有最佳主要度量的反復專案。In this example, the metric and iteration parameters aren't specified, so the iteration with the best primary metric will be registered. 會使用從執行傳回的值,而不是模型名稱。model_idThe model_id value returned from the run is used instead of a model name.

      如需詳細資訊,請參閱AutoMLRun. register_model檔。For more information, see the AutoMLRun.register_model documentation.

  • 使用 CLIUsing the CLI

    az ml model register -n sklearn_mnist  --asset-path outputs/sklearn_mnist_model.pkl  --experiment-name myexperiment --run-id myrunid
    

    提示

    如果您收到錯誤訊息,指出未安裝 ml 延伸模組,請使用下列命令來安裝它:If you get an error message stating that the ml extension isn't installed, use the following command to install it:

    az extension add -n azure-cli-ml
    

    --asset-path參數會參考模型的雲端位置。The --asset-path parameter refers to the cloud location of the model. 在此範例中,會使用單一檔案的路徑。In this example, the path of a single file is used. 若要在模型註冊中包含多個檔案--asset-path ,請將設定為包含檔案的資料夾路徑。To include multiple files in the model registration, set --asset-path to the path of a folder that contains the files.

  • 使用 VS CodeUsing VS Code

    使用VS Code擴充功能,透過任何模型檔案或資料夾來註冊模型。Register models using any model files or folders by using the VS Code extension.

從本機檔案註冊模型Register a model from a local file

您可以藉由提供模型的本機路徑來註冊模型。You can register a model by providing the local path of the model. 您可以提供資料夾或單一檔案的路徑。You can provide the path of either a folder or a single file. 您可以使用這個方法來註冊使用 Azure Machine Learning 定型的模型,然後下載。You can use this method to register models trained with Azure Machine Learning and then downloaded. 您也可以使用這個方法來註冊在 Azure Machine Learning 外部定型的模型。You can also use this method to register models trained outside of Azure Machine Learning.

重要

您應該只使用您從信任的來源建立或取得的模型。You should use only models that you create or obtain from a trusted source. 您應該將序列化模型視為程式碼,因為已探索到許多常用格式的安全性弱點。You should treat serialized models as code, because security vulnerabilities have been discovered in a number of popular formats. 此外,模型可能會刻意以惡意意圖訓練,以提供偏差或不正確的輸出。Also, models might be intentionally trained with malicious intent to provide biased or inaccurate output.

  • 使用 SDK 和 ONNXUsing the SDK and ONNX

    import os
    import urllib.request
    from azureml.core.model import Model
    # Download model
    onnx_model_url = "https://www.cntk.ai/OnnxModels/mnist/opset_7/mnist.tar.gz"
    urllib.request.urlretrieve(onnx_model_url, filename="mnist.tar.gz")
    os.system('tar xvzf mnist.tar.gz')
    # Register model
    model = Model.register(workspace = ws,
                            model_path ="mnist/model.onnx",
                            model_name = "onnx_mnist",
                            tags = {"onnx": "demo"},
                            description = "MNIST image classification CNN from ONNX Model Zoo",)
    

    若要在模型註冊中包含多個檔案model_path ,請將設定為包含檔案的資料夾路徑。To include multiple files in the model registration, set model_path to the path of a folder that contains the files.

  • 使用 CLIUsing the CLI

    az ml model register -n onnx_mnist -p mnist/model.onnx
    

    若要在模型註冊中包含多個檔案-p ,請將設定為包含檔案的資料夾路徑。To include multiple files in the model registration, set -p to the path of a folder that contains the files.

估計時間:大約 10 秒。Time estimate: Approximately 10 seconds.

如需詳細資訊,請參閱模型類別的檔。For more information, see the documentation for the Model class.

如需使用 Azure Machine Learning 外部定型模型的詳細資訊,請參閱如何部署現有的模型For more information on working with models trained outside Azure Machine Learning, see How to deploy an existing model.

選擇計算目標Choose a compute target

您可以使用下列計算目標或計算資源來裝載您的 web 服務部署:You can use the following compute targets, or compute resources, to host your web service deployment:

計算目標Compute target 用於Used for GPU 支援GPU support FPGA 支援FPGA support 描述Description
本機 web 服務Local web service 測試/調試Testing/debugging     用於有限的測試和疑難排解。Use for limited testing and troubleshooting. 硬體加速取決於使用本機系統中的程式庫。Hardware acceleration depends on use of libraries in the local system.
筆記本 VM web 服務Notebook VM web service 測試/調試Testing/debugging     用於有限的測試和疑難排解。Use for limited testing and troubleshooting.
Azure Kubernetes Service (AKS)Azure Kubernetes Service (AKS) 即時推斷Real-time inference (web 服務部署)Yes (web service deployment) Yes 用於大規模生產環境部署。Use for high-scale production deployments. 提供已部署服務的快速回應時間和自動調整。Provides fast response time and autoscaling of the deployed service. 不支援透過 Azure Machine Learning SDK 進行叢集自動調整。Cluster autoscaling isn't supported through the Azure Machine Learning SDK. 若要變更 AKS 叢集中的節點,請在 Azure 入口網站中使用 AKS 叢集的 UI。To change the nodes in the AKS cluster, use the UI for your AKS cluster in the Azure portal. AKS 是視覺化介面唯一可用的選項。AKS is the only option available for the visual interface.
Azure 容器執行個體Azure Container Instances 測試或開發Testing or development     針對需要少於 48 GB RAM 的低規模 CPU 型工作負載,使用。Use for low-scale CPU-based workloads that require less than 48 GB of RAM.
Azure Machine Learning ComputeAzure Machine Learning Compute 預覽批 次推斷(Preview) Batch inference (機器學習管線)Yes (machine learning pipeline)   在無伺服器計算上執行批次評分。Run batch scoring on serverless compute. 支援一般和低優先順序的 Vm。Supports normal and low-priority VMs.
Azure IoT EdgeAzure IoT Edge 預覽IoT 模組(Preview) IoT module     在 IoT 裝置上部署和提供 ML 模型。Deploy and serve ML models on IoT devices.
Azure Data Box EdgeAzure Data Box Edge Via IoT EdgeVia IoT Edge   Yes 在 IoT 裝置上部署和提供 ML 模型。Deploy and serve ML models on IoT devices.

注意

雖然計算目標(例如本機、筆記本 VM 和 Azure Machine Learning 計算)支援用於定型和實驗的 GPU,但__在部署為 web 服務時__,只支援在 Azure Kubernetes Service 上使用 gpu 進行推斷。Although compute targets like local, Notebook VM, and Azure Machine Learning Compute support GPU for training and experimentation, using GPU for inference when deployed as a web service is supported only on Azure Kubernetes Service.

只有在 Azure Machine Learning 計算上才支援使用__機器學習管線進行評分時__,使用 GPU 進行推斷。Using a GPU for inference when scoring with a machine learning pipeline is supported only on Azure Machine Learning Compute.

準備部署Prepare to deploy

若要部署模型,您需要下列專案:To deploy the model, you need the following items:

  • 輸入腳本An entry script. 此腳本會接受要求,使用模型來評分要求,並傳回結果。This script accepts requests, scores the requests by 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's not usable by your model, the script can transform it into an acceptable format. 它也可以在將回應傳回給用戶端之前,先將它轉換。It can also transform the response before returning it to the client.

    • Azure Machine Learning SDK 不會提供 web 服務或 IoT Edge 部署的方式來存取您的資料存放區或資料集。The Azure Machine Learning SDK doesn't provide a way for web services or IoT Edge deployments to access your data store or datasets. 如果您已部署的模型需要存取儲存在部署外部的資料,例如 Azure 儲存體帳戶中的資料,您必須使用相關的 SDK 開發自訂程式碼解決方案。If your deployed model needs to access data stored outside the deployment, like data in an Azure storage account, you must develop a custom code solution by using the relevant SDK. 例如,適用于 Python 的 AZURE 儲存體 SDKFor example, the Azure Storage SDK for Python.

      可能適用于您案例的替代方法是批次預測,這會在計分期間提供資料存放區的存取權。An alternative that might work for your scenario is batch prediction, which does provide access to data stores during scoring.

  • 相依性,例如執行專案腳本或模型所需的 helper腳本或 Python/Conda 套件。Dependencies, like helper scripts or Python/Conda packages required to run the entry script or model.

  • 裝載已部署模型之計算目標的部署設定。The deployment configuration for the compute target that hosts the deployed model. 此設定會描述執行模型所需的記憶體和 CPU 需求等事項。This configuration describes things like memory and CPU requirements needed to run the model.

這些專案會封裝成推斷設定和部署設定。These items are encapsulated into an inference configuration and a deployment configuration. 推斷設定會參考專案腳本和其他相依性。The inference configuration references the entry script and other dependencies. 當您使用 SDK 執行部署時,會以程式設計方式定義這些設定。You define these configurations programmatically when you use the SDK to perform the deployment. 當您使用 CLI 時,您會在 JSON 檔案中定義它們。You define them in JSON files when you use the CLI.

1.定義您的輸入腳本和相依性1. Define your entry script and dependencies

專案腳本會接收提交至已部署 web 服務的資料,並將它傳遞至模型。The entry script receives data submitted to a deployed web service and passes it to the model. 然後,它會採用模型傳回的回應,並將該回應傳回至用戶端。It then takes the response returned by the model and returns that to the client. 此腳本專屬於您的模型The script is specific to your model. 它必須瞭解模型預期和傳回的資料。It must understand the data that the model expects and returns.

此腳本包含兩個載入和執行模型的函式:The script contains two functions that load and run the model:

  • init():此函式通常會將模型載入至全域物件。init(): Typically, this function loads the model into a global object. 當您的 web 服務的 Docker 容器已啟動時,此函式只會執行一次。This function is run only once, when the Docker container for your web service is started.

  • run(input_data):此函式會使用模型,依據輸入資料來預測值。run(input_data): This function uses the model to predict a value based on the input data. 執行的輸入和輸出通常會使用 JSON 進行序列化和還原序列化。Inputs and outputs of the run typically use JSON for serialization and deserialization. 您也可以使用原始的二進位資料。You can also work with raw binary data. 您可以先轉換資料,然後再將它傳送至模型,或將它傳回給用戶端。You can transform the data before sending it to the model or before returning it to the client.

在您的輸入腳本中尋找模型檔案Locate model files in your entry script

有兩種方式可在您的輸入腳本中尋找模型:There are two ways to locate models in your entry script:

  • AZUREML_MODEL_DIR:包含模型位置路徑的環境變數。AZUREML_MODEL_DIR: An environment variable containing the path to the model location.
  • Model.get_model_path:此 API 會使用已註冊的模型名稱,傳回模型檔案的路徑。Model.get_model_path: An API that returns the path to model file using the registered model name.
AZUREML_MODEL_DIRAZUREML_MODEL_DIR

AZUREML_MODEL_DIR 是在服務部署期間建立的環境變數。AZUREML_MODEL_DIR is an environment variable created during service deployment. 您可以使用這個環境變數來尋找已部署之模型的位置。You can use this environment variable to find the location of the deployed model(s).

下表描述 AZUREML_MODEL_DIR 的值,視部署的模型數目而定:The following table describes the value of AZUREML_MODEL_DIR depending on the number of models deployed:

部署Deployment 環境變數值Environment variable value
單一模型Single model 包含模型的資料夾路徑。The path to the folder containing the model.
多個模型Multiple models 包含所有模型之資料夾的路徑。The path to the folder containing all models. 模型是以名稱和版本的形式在此資料夾$MODEL_NAME/$VERSION中找到()Models are located by name and version in this folder ($MODEL_NAME/$VERSION)

若要取得模型中檔案的路徑,請將環境變數與您要尋找的檔案名結合。To get the path to a file in a model, combine the environment variable with the filename you're looking for. 在註冊和部署期間,會保留模型檔案的檔案名。The filenames of the model files are preserved during registration and deployment.

單一模型範例Single model example

model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'sklearn_regression_model.pkl')

多個模型範例Multiple model example

model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'sklearn_model/1/sklearn_regression_model.pkl')
get_model_pathget_model_path

當您註冊模型時,您會提供用來在登錄中管理模型的模型名稱。When you register a model, you provide a model name that's used for managing the model in the registry. 您會將此名稱與模型搭配使用。 get _model_path ()方法可在本機檔案系統上取出模型檔案的路徑。You use this name with the Model.get_model_path() method to retrieve the path of the model file or files on the local file system. 如果您註冊資料夾或檔案集合,此 API 會傳回包含這些檔案的目錄路徑。If you register a folder or a collection of files, this API returns the path of the directory that contains those files.

當您註冊模型時,會為它命名。When you register a model, you give it a name. 名稱會對應至模型的放置位置,不論是在本機或在服務部署期間。The name corresponds to where the model is placed, either locally or during service deployment.

重要

如果您使用自動化機器學習來定型模型, model_id則會使用值做為模型名稱。If you used automated machine learning to train a model, a model_id value is used as the model name. 如需註冊和部署使用自動化機器學習服務定型之模型的範例,請參閱 GitHub 上的Azure/MachineLearningNotebooksFor an example of registering and deploying a model trained with automated machine learning, see Azure/MachineLearningNotebooks on GitHub.

下列範例會傳回名sklearn_mnist_model.pkl為之單一檔案的路徑(已使用名稱sklearn_mnist註冊):The following example will return a path to a single file called sklearn_mnist_model.pkl (which was registered with the name sklearn_mnist):

model_path = Model.get_model_path('sklearn_mnist')

選擇性自動產生架構(Optional) Automatic schema generation

若要自動產生 web 服務的架構,請在其中一個已定義的類型物件的函式中提供輸入和/或輸出的範例。To automatically generate a schema for your web service, provide a sample of the input and/or output in the constructor for one of the defined type objects. 型別和範例用來自動建立架構。The type and sample are used to automatically create the schema. Azure Machine Learning 接著會在部署期間建立 web 服務的OpenAPI (Swagger)規格。Azure Machine Learning then creates an OpenAPI (Swagger) specification for the web service during deployment.

目前支援下列類型:These types are currently supported:

  • pandas
  • numpy
  • pyspark
  • 標準 Python 物件Standard Python object

若要使用架構產生,請inference-schema在您的 Conda 環境檔案中包含封裝。To use schema generation, include the inference-schema package in your Conda environment file.

範例相依性檔案Example dependencies file

下列 YAML 是用於推斷的 Conda 相依性檔案範例:The following YAML is an example of a Conda dependencies file for inference:

name: project_environment
dependencies:
  - python=3.6.2
  - pip:
    - azureml-defaults
    - scikit-learn==0.20.0
    - inference-schema[numpy-support]

如果您想要使用自動產生架構,您的輸入腳本必須匯inference-schema入封裝。If you want to use automatic schema generation, your entry script must import the inference-schema packages.

input_sampleoutput_sample變數中定義輸入和輸出範例格式, 其代表 web 服務的要求和回應格式。Define the input and output sample formats in the input_sample and output_sample variables, which represent the request and response formats for the web service. 在函式的 input 和 output 函數裝飾專案run()中使用這些範例。Use these samples in the input and output function decorators on the run() function. 下列 scikit-learn 學習範例會使用架構產生。The following scikit-learn example uses schema generation.

範例專案腳本Example entry script

下列範例示範如何接受並傳回 JSON 資料:The following example demonstrates how to accept and return JSON data:

#Example: scikit-learn and Swagger
import json
import numpy as np
import os
from sklearn.externals import joblib
from sklearn.linear_model import Ridge

from inference_schema.schema_decorators import input_schema, output_schema
from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType


def init():
    global model
    # AZUREML_MODEL_DIR is an environment variable created during deployment. Join this path with the filename of the model file.
    # It holds the path to the directory that contains the deployed model (./azureml-models/$MODEL_NAME/$VERSION).
    # If there are multiple models, this value is the path to the directory containing all deployed models (./azureml-models).
    # Alternatively: model_path = Model.get_model_path('sklearn_mnist')
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'sklearn_mnist_model.pkl')
    # Deserialize the model file back into a sklearn model
    model = joblib.load(model_path)


input_sample = np.array([[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]])
output_sample = np.array([3726.995])


@input_schema('data', NumpyParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))
def run(data):
    try:
        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

下列範例示範如何使用資料框架,將輸入資料定義為<key: value>字典。The following example demonstrates how to define the input data as a <key: value> dictionary by using a DataFrame. 此方法支援從 Power BI 使用已部署的 web 服務。This method is supported for consuming the deployed web service from Power BI. 深入瞭解如何使用 Power BI 的 web 服務)。(Learn more about how to consume the web service from Power BI.)

import json
import pickle
import numpy as np
import pandas as pd
import azureml.train.automl
from sklearn.externals import joblib
from azureml.core.model import Model

from inference_schema.schema_decorators import input_schema, output_schema
from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType
from inference_schema.parameter_types.pandas_parameter_type import PandasParameterType


def init():
    global model
    # Replace filename if needed.
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model_file.pkl')
    # Deserialize the model file back into a sklearn model.
    model = joblib.load(model_path)


input_sample = pd.DataFrame(data=[{
    # This is a decimal type sample. Use the data type that reflects this column in your data.
    "input_name_1": 5.1,
    # This is a string type sample. Use the data type that reflects this column in your data.
    "input_name_2": "value2",
    # This is an integer type sample. Use the data type that reflects this column in your data.
    "input_name_3": 3
}])

# This is an integer type sample. Use the data type that reflects the expected result.
output_sample = np.array([0])


@input_schema('data', PandasParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))
def run(data):
    try:
        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 examples, see the following scripts:

二進位資料Binary data

如果您的模型接受二進位資料(例如影像),您必須修改score.py用於部署的檔案,以接受原始的 HTTP 要求。If your model accepts binary data, like an image, you must modify the score.py file used for your deployment to accept raw HTTP requests. 若要接受原始資料,請AMLRequest在您的輸入腳本中使用類別@rawhttp ,並將run()裝飾專案新增至函式。To accept raw data, use the AMLRequest class in your entry script and add the @rawhttp decorator to the run() function.

以下是score.py可接受二進位資料的範例:Here's an example of a score.py that accepts binary data:

from azureml.contrib.services.aml_request import AMLRequest, rawhttp
from azureml.contrib.services.aml_response import AMLResponse


def init():
    print("This is init()")


@rawhttp
def run(request):
    print("This is run()")
    print("Request: [{0}]".format(request))
    if request.method == 'GET':
        # For this example, just return the URL for GETs.
        respBody = str.encode(request.full_path)
        return AMLResponse(respBody, 200)
    elif request.method == 'POST':
        reqBody = request.get_data(False)
        # For a real-world solution, you would load the data from reqBody
        # and send it to the model. Then return the response.

        # For demonstration purposes, this example just returns the posted data as the response.
        return AMLResponse(reqBody, 200)
    else:
        return AMLResponse("bad request", 500)

重要

AMLRequest類別是azureml.contrib在命名空間中。The AMLRequest class is in the azureml.contrib namespace. 當我們改善服務時,此命名空間中的實體會經常變更。Entities in this namespace change frequently as we work to improve the service. 此命名空間中的任何專案都應視為不受 Microsoft 完全支援的預覽。Anything in this namespace should be considered a preview that's not fully supported by Microsoft.

如果您需要在本機開發環境中測試此項,您可以使用下列命令來安裝元件:If you need to test this in your local development environment, you can install the components by using the following command:

pip install azureml-contrib-services

跨原始來源資源分享(CORS)Cross-origin resource sharing (CORS)

跨原始資源分享是允許從另一個網域要求網頁上資源的一種方式。Cross-origin resource sharing is a way to allow resources on a web page to be requested from another domain. CORS 的運作方式是透過與用戶端要求一起傳送的 HTTP 標頭,並傳回服務回應。CORS works via HTTP headers sent with the client request and returned with the service response. 如需 CORS 和有效標頭的詳細資訊,請參閱維琪百科中的跨原始資源分享For more information on CORS and valid headers, see Cross-origin resource sharing in Wikipedia.

若要將您的模型部署設定為支援 CORS AMLResponse ,請在您的輸入腳本中使用類別。To configure your model deployment to support CORS, use the AMLResponse class in your entry script. 這個類別可讓您在回應物件上設定標頭。This class allows you to set the headers on the response object.

下列範例會從專案Access-Control-Allow-Origin腳本設定回應的標頭:The following example sets the Access-Control-Allow-Origin header for the response from the entry script:

from azureml.contrib.services.aml_response import AMLResponse

def init():
    print("This is init()")

def run(request):
    print("This is run()")
    print("Request: [{0}]".format(request))
    if request.method == 'GET':
        # For this example, just return the URL for GETs.
        respBody = str.encode(request.full_path)
        return AMLResponse(respBody, 200)
    elif request.method == 'POST':
        reqBody = request.get_data(False)
        # For a real-world solution, you would load the data from reqBody
        # and send it to the model. Then return the response.

        # For demonstration purposes, this example
        # adds a header and returns the request body.
        resp = AMLResponse(reqBody, 200)
        resp.headers['Access-Control-Allow-Origin'] = "http://www.example.com"
        return resp
    else:
        return AMLResponse("bad request", 500)

重要

AMLResponse類別是azureml.contrib在命名空間中。The AMLResponse class is in the azureml.contrib namespace. 當我們改善服務時,此命名空間中的實體會經常變更。Entities in this namespace change frequently as we work to improve the service. 此命名空間中的任何專案都應視為不受 Microsoft 完全支援的預覽。Anything in this namespace should be considered a preview that's not fully supported by Microsoft.

如果您需要在本機開發環境中測試此項,您可以使用下列命令來安裝元件:If you need to test this in your local development environment, you can install the components by using the following command:

pip install azureml-contrib-services

2.定義您的 InferenceConfig2. Define your InferenceConfig

推斷設定會描述如何設定模型來進行預測。The inference configuration describes how to configure the model to make predictions. 此設定不屬於您的輸入腳本。This configuration isn't part of your entry script. 它會參考您的輸入腳本,並用來尋找部署所需的所有資源。It references your entry script and is used to locate all the resources required by the deployment. 稍後當您部署模型時,就會用到它。It's used later, when you deploy the model.

推斷設定可以使用 Azure Machine Learning 環境來定義您的部署所需的軟體相依性。Inference configuration can use Azure Machine Learning environments to define the software dependencies needed for your deployment. 環境可讓您建立、管理及重複使用定型和部署所需的軟體相依性。Environments allow you to create, manage, and reuse the software dependencies required for training and deployment. 下列範例示範如何從您的工作區載入環境,然後將它與推斷設定搭配使用:The following example demonstrates loading an environment from your workspace and then using it with the inference configuration:

from azureml.core import Environment
from azureml.core.model import InferenceConfig

deploy_env = Environment.get(workspace=ws,name="myenv",version="1")
inference_config = InferenceConfig(entry_script="x/y/score.py",
                                   environment=deploy_env)

如需環境的詳細資訊,請參閱建立和管理用於定型和部署的環境For more information on environments, see Create and manage environments for training and deployment.

您也可以直接指定相依性,而不使用環境。You can also directly specify the dependencies without using an environment. 下列範例示範如何建立推斷設定,以從 Conda 檔案載入軟體相依性:The following example demonstrates how to create an inference configuration that loads software dependencies from a Conda file:

from azureml.core.model import InferenceConfig

inference_config = InferenceConfig(runtime="python",
                                   entry_script="x/y/score.py",
                                   conda_file="env/myenv.yml")

如需詳細資訊,請參閱InferenceConfig類別檔。For more information, see the InferenceConfig class documentation.

如需有關使用自訂 Docker 映射搭配推斷設定的詳細資訊,請參閱如何使用自訂 docker 映射部署模型For information on using a custom Docker image with an inference configuration, see How to deploy a model using a custom Docker image.

InferenceConfig 的 CLI 範例CLI example of InferenceConfig

inferenceconfig.json檔中的專案會對應至InferenceConfig類別的參數。The entries in the inferenceconfig.json document map to the parameters for the InferenceConfig class. 下表描述 JSON 檔中的實體與方法的參數之間的對應:The following table describes the mapping between entities in the JSON document and the parameters for the method:

JSON 實體JSON entity 方法參數Method parameter 描述Description
entryScript entry_script 本機檔案的路徑,其中包含要為映射執行的程式碼。Path to a local file that contains the code to run for the image.
runtime runtime 要用於影像的執行時間。Which runtime to use for the image. 目前支援的運行spark-py時間python為和。Current supported runtimes are spark-py and python.
condaFile conda_file 選擇性。Optional. 本機檔案的路徑,其中包含要用於影像的 Conda 環境定義。Path to a local file that contains a Conda environment definition to use for the image.
extraDockerFileSteps extra_docker_file_steps 選擇性。Optional. 本機檔案的路徑,其中包含設定映射時要執行的其他 Docker 步驟。Path to a local file that contains additional Docker steps to run when setting up the image.
sourceDirectory source_directory 選擇性。Optional. 資料夾的路徑,其中包含要建立映射的所有檔案。Path to folders that contain all files to create the image.
enableGpu enable_gpu 選擇性。Optional. 是否要在映射中啟用 GPU 支援。Whether to enable GPU support in the image. GPU 映射必須用於 Azure 服務,例如 Azure 容器實例、Azure Machine Learning 計算、Azure 虛擬機器和 Azure Kubernetes Service。The GPU image must be used on an Azure service, like Azure Container Instances, Azure Machine Learning Compute, Azure Virtual Machines, and Azure Kubernetes Service. 預設值為 False。The default is False.
baseImage base_image 選擇性。Optional. 要當做基底映射使用的自訂映射。Custom image to be used as a base image. 如果未提供基底映射,則映射會以提供的執行時間參數為基礎。If no base image is provided, the image will be based on the provided runtime parameter.
baseImageRegistry base_image_registry 選擇性。Optional. 包含基底映射的映射登錄。Image registry that contains the base image.
cudaVersion cuda_version 選擇性。Optional. 要為需要 GPU 支援的映射安裝的 CUDA 版本。Version of CUDA to install for images that need GPU support. GPU 映射必須用於 Azure 服務,例如 Azure 容器實例、Azure Machine Learning 計算、Azure 虛擬機器和 Azure Kubernetes Service。The GPU image must be used on an Azure service, like Azure Container Instances, Azure Machine Learning Compute, Azure Virtual Machines, and Azure Kubernetes Service. 支援的版本為9.0、9.1 和10.0。Supported versions are 9.0, 9.1, and 10.0. 如果enable_gpu已設定,則預設值為9.1。If enable_gpu is set, the default is 9.1.
description description 影像的描述。A description for the image.

下列 JSON 是與 CLI 搭配使用的範例推斷設定:The following JSON is an example inference configuration for use with the CLI:

{
    "entryScript": "score.py",
    "runtime": "python",
    "condaFile": "myenv.yml",
    "extraDockerfileSteps": null,
    "sourceDirectory": null,
    "enableGpu": false,
    "baseImage": null,
    "baseImageRegistry": null
}

下列命令示範如何使用 CLI 來部署模型:The following command demonstrates how to deploy a model by using the CLI:

az ml model deploy -n myservice -m mymodel:1 --ic inferenceconfig.json

在此範例中,設定會指定下列設定:In this example, the configuration specifies the following settings:

  • 此模型需要 Python。That the model requires Python.
  • 輸入腳本,用來處理傳送至已部署服務的 web 要求。The entry script, which is used to handle web requests sent to the deployed service.
  • Conda 檔案,描述推斷所需的 Python 套件。The Conda file that describes the Python packages needed for inference.

如需有關使用自訂 Docker 映射搭配推斷設定的詳細資訊,請參閱如何使用自訂 docker 映射部署模型For information on using a custom Docker image with an inference configuration, see How to deploy a model using a custom Docker image.

3.定義您的部署設定3. Define your deployment configuration

在部署模型之前,您必須先定義部署設定。Before deploying your model, you must define the deployment configuration. 部署設定適用于將裝載 web 服務的計算目標。The deployment configuration is specific to the compute target that will host the web service. 例如,當您在本機部署模型時,您必須指定服務接受要求的埠。For example, when you deploy a model locally, you must specify the port where the service accepts requests. 部署設定不屬於您的輸入腳本。The deployment configuration isn't part of your entry script. 它是用來定義將裝載模型和專案腳本之計算目標的特性。It's used to define the characteristics of the compute target that will host the model and entry script.

您可能也需要建立計算資源(例如,您的工作區尚未有相關聯的 Azure Kubernetes Service (AKS)實例)。You might also need to create the compute resource, if, for example, you don't already have an Azure Kubernetes Service (AKS) instance associated with your workspace.

下表提供建立每個計算目標之部署設定的範例:The following table provides an example of creating a deployment configuration for each compute target:

計算目標Compute target 部署設定範例Deployment configuration example
本機Local deployment_config = LocalWebservice.deploy_configuration(port=8890)
Azure Container InstancesAzure Container Instances deployment_config = AciWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1)
Azure Kubernetes ServiceAzure Kubernetes Service deployment_config = AksWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1)

本機、Azure 容器實例和 AKS web 服務的類別可以從下列來源azureml.core.webservice匯入:The classes for local, Azure Container Instances, and AKS web services can be imported from azureml.core.webservice:

from azureml.core.webservice import AciWebservice, AksWebservice, LocalWebservice

分析Profiling

在您將模型部署為服務之前,您可能會想要進行分析,以判斷最佳的 CPU 和記憶體需求。Before you deploy your model as a service, you might want to profile it to determine optimal CPU and memory requirements. 您可以使用 SDK 或 CLI 來分析您的模型。You can use either the SDK or the CLI to profile your model. 下列範例示範如何使用 SDK 來分析模型。The following examples show how to profile a model by using the SDK.

重要

當您流量分析時,您所提供的推斷設定無法參考 Azure Machine Learning 環境。When you use profiling, the inference configuration that you provide can't reference an Azure Machine Learning environment. 相反地,請使用conda_file InferenceConfig物件的參數來定義軟體相依性。Instead, define the software dependencies by using the conda_file parameter of the InferenceConfig object.

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

profile = Model.profile(ws, "profilemymodel", [model], inference_config, test_data)
profile.wait_for_profiling(true)
profiling_results = profile.get_results()
print(profiling_results)

此程式碼會顯示類似下列輸出的結果:This code displays a result similar to the following output:

{'cpu': 1.0, 'memoryInGB': 0.5}

模型分析結果會以物件的Run形式發出。Model profiling results are emitted as a Run object.

如需從 CLI 流量分析的詳細資訊,請參閱az ml model profileFor information on using profiling from the CLI, see az ml model profile.

如需詳細資訊,請參閱下列檔:For more information, see these documents:

部署至目標Deploy to target

部署會使用推斷設定部署設定來部署模型。Deployment uses the inference configuration deployment configuration to deploy the models. 無論計算目標為何,部署程式都很類似。The deployment process is similar regardless of the compute target. 部署至 AKS 稍有不同,因為您必須提供 AKS 叢集的參考。Deploying to AKS is slightly different because you must provide a reference to the AKS cluster.

本機部署Local deployment

若要在本機部署模型,您必須在本機電腦上安裝 Docker。To deploy a model locally, you need to have Docker installed on your local machine.

使用 SDKUsing the SDK

from azureml.core.webservice import LocalWebservice, Webservice

deployment_config = LocalWebservice.deploy_configuration(port=8890)
service = Model.deploy(ws, "myservice", [model], inference_config, deployment_config)
service.wait_for_deployment(show_output = True)
print(service.state)

如需詳細資訊,請參閱LocalWebserviceModel. deploy ()Webservice的檔。For more information, see the documentation for LocalWebservice, Model.deploy(), and Webservice.

使用 CLIUsing the CLI

若要使用 CLI 部署模型,請使用下列命令。To deploy a model by using the CLI, use the following command. mymodel:1已註冊模型的名稱和版本取代:Replace mymodel:1 with the name and version of the registered model:

az ml model deploy -m mymodel:1 --ic inferenceconfig.json --dc deploymentconfig.json

deploymentconfig.json檔中的專案會對應至 LocalWebservice 的參數。 deploy_configurationThe entries in the deploymentconfig.json document map to the parameters for LocalWebservice.deploy_configuration. 下表描述 JSON 檔中的實體與方法的參數之間的對應:The following table describes the mapping between the entities in the JSON document and the parameters for the method:

JSON 實體JSON entity 方法參數Method parameter 描述Description
computeType NANA 計算目標。The compute target. 若為本機目標,此值必須local是。For local targets, the value must be local.
port port 要在其上公開服務之 HTTP 端點的本機埠。The local port on which to expose the service's HTTP endpoint.

此 JSON 是與 CLI 搭配使用的範例部署設定:This JSON is an example deployment configuration for use with the CLI:

{
    "computeType": "local",
    "port": 32267
}

如需詳細資訊,請參閱az ml model deploy檔。For more information, see the az ml model deploy documentation.

筆記本 VM web 服務(開發/測試)Notebook VM web service (dev/test)

請參閱將模型部署至筆記本 vmSee Deploy a model to Notebook VMs.

Azure 容器實例(開發/測試)Azure Container Instances (dev/test)

請參閱部署至 Azure 容器實例See Deploy to Azure Container Instances.

Azure Kubernetes Service (開發/測試和生產)Azure Kubernetes Service (dev/test and production)

請參閱部署至 Azure Kubernetes ServiceSee Deploy to Azure Kubernetes Service.

取用 Web 服務Consume web services

每個已部署的 web 服務都會提供 REST API, 因此您可以使用各種程式設計語言來建立用戶端應用程式。Every deployed web service provides a REST API, so you can create client applications in a variety of programming languages. 如果您已啟用服務的金鑰驗證,則需要提供服務金鑰做為要求標頭中的權杖。If you've enabled key authentication for your service, you need to provide a service key as a token in your request header. 如果您已啟用服務的權杖驗證,則需要提供 Azure Machine Learning JWT 權杖做為要求標頭中的持有人權杖。If you've enabled token authentication for your service, you need to provide an Azure Machine Learning JWT token as a bearer token in your request header.

提示

您可以在部署服務之後,取得架構 JSON 檔。You can retrieve the schema JSON document after you deploy the service. 從已部署的 web 服務使用swagger_uri 屬性(例如,service.swagger_uri),以取得本機 Web 服務 swagger 檔案的 uri。Use the swagger_uri property from the deployed web service (for example, service.swagger_uri) to get the URI to the local web service's Swagger file.

要求-回應耗用量Request-response consumption

以下範例說明如何在 Python 中叫用您的服務:Here's an example of how to invoke your service in Python:

import requests
import json

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

if service.auth_enabled:
    headers['Authorization'] = 'Bearer '+service.get_keys()[0]
elif service.token_auth_enabled:
    headers['Authorization'] = 'Bearer '+service.get_token()[0]

print(headers)

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(
    service.scoring_uri, data=test_sample, headers=headers)
print(response.status_code)
print(response.elapsed)
print(response.json())

如需詳細資訊,請參閱建立用戶端應用程式以使用 web 服務For more information, see Create client applications to consume web services.

Web 服務架構(OpenAPI 規格)Web service schema (OpenAPI specification)

如果您在部署中使用自動產生架構,您可以使用swagger_uri 屬性取得服務的 OpenAPI 規格位址。If you used automatic schema generation with your deployment, you can get the address of the OpenAPI specification for the service by using the swagger_uri property. (例如,print(service.swagger_uri))。使用 GET 要求,或在瀏覽器中開啟 URI 以取得規格。(For example, print(service.swagger_uri).) Use a GET request or open the URI in a browser to retrieve the specification.

下列 JSON 檔是針對部署所產生的架構(OpenAPI 規格)範例:The following JSON document is an example of a schema (OpenAPI specification) generated for a deployment:

{
    "swagger": "2.0",
    "info": {
        "title": "myservice",
        "description": "API specification for Azure Machine Learning myservice",
        "version": "1.0"
    },
    "schemes": [
        "https"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "securityDefinitions": {
        "Bearer": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header",
            "description": "For example: Bearer abc123"
        }
    },
    "paths": {
        "/": {
            "get": {
                "operationId": "ServiceHealthCheck",
                "description": "Simple health check endpoint to ensure the service is up at any given point.",
                "responses": {
                    "200": {
                        "description": "If service is up and running, this response will be returned with the content 'Healthy'",
                        "schema": {
                            "type": "string"
                        },
                        "examples": {
                            "application/json": "Healthy"
                        }
                    },
                    "default": {
                        "description": "The service failed to execute due to an error.",
                        "schema": {
                            "$ref": "#/definitions/ErrorResponse"
                        }
                    }
                }
            }
        },
        "/score": {
            "post": {
                "operationId": "RunMLService",
                "description": "Run web service's model and get the prediction output",
                "security": [
                    {
                        "Bearer": []
                    }
                ],
                "parameters": [
                    {
                        "name": "serviceInputPayload",
                        "in": "body",
                        "description": "The input payload for executing the real-time machine learning service.",
                        "schema": {
                            "$ref": "#/definitions/ServiceInput"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "The service processed the input correctly and provided a result prediction, if applicable.",
                        "schema": {
                            "$ref": "#/definitions/ServiceOutput"
                        }
                    },
                    "default": {
                        "description": "The service failed to execute due to an error.",
                        "schema": {
                            "$ref": "#/definitions/ErrorResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "ServiceInput": {
            "type": "object",
            "properties": {
                "data": {
                    "type": "array",
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "integer",
                            "format": "int64"
                        }
                    }
                }
            },
            "example": {
                "data": [
                    [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
                ]
            }
        },
        "ServiceOutput": {
            "type": "array",
            "items": {
                "type": "number",
                "format": "double"
            },
            "example": [
                3726.995
            ]
        },
        "ErrorResponse": {
            "type": "object",
            "properties": {
                "status_code": {
                    "type": "integer",
                    "format": "int32"
                },
                "message": {
                    "type": "string"
                }
            }
        }
    }
}

如需詳細資訊,請參閱OpenAPI 規格For more information, see OpenAPI specification.

如需可從規格建立用戶端程式庫的公用程式,請參閱swagger-codegenFor a utility that can create client libraries from the specification, see swagger-codegen.

批次推斷Batch inference

Azure Machine Learning 計算目標是由 Azure Machine Learning 建立和管理。Azure Machine Learning Compute targets are created and managed by Azure Machine Learning. 它們可以用於 Azure Machine Learning 管線的批次預測。They can be used for batch prediction from Azure Machine Learning pipelines.

如需使用 Azure Machine Learning 計算進行批次推斷的逐步解說,請參閱如何執行批次預測For a walkthrough of batch inference with Azure Machine Learning Compute, see How to run batch predictions.

IoT Edge 推斷IoT Edge inference

支援部署至 edge 目前處於預覽階段。Support for deploying to the edge is in preview. 如需詳細資訊,請參閱將 Azure Machine Learning 部署為 IoT Edge 模組For more information, see Deploy Azure Machine Learning as an IoT Edge module.

更新 web 服務Update web services

若要更新 web 服務,請使用update方法。To update a web service, use the update method. 您可以更新 web 服務,以使用新的模型、新的輸入腳本,或可在推斷設定中指定的新相依性。You can update the web service to use a new model, a new entry script, or new dependencies that can be specified in an inference configuration. 如需詳細資訊,請參閱Webservice的檔。For more information, see the documentation for Webservice.update.

重要

當您建立模型的新版本時,您必須手動更新您想要使用的每個服務。When you create a new version of a model, you must manually update each service that you want to use it.

使用 SDKUsing the SDK

下列程式碼示範如何使用 SDK 來更新 web 服務的模型、環境和專案腳本:The following code shows how to use the SDK to update the model, environment, and entry script for a web service:

from azureml.core import Environment
from azureml.core.webservice import Webservice
from azureml.core.model import Model, InferenceConfig

# Register new model.
new_model = Model.register(model_path="outputs/sklearn_mnist_model.pkl",
                           model_name="sklearn_mnist",
                           tags={"key": "0.1"},
                           description="test",
                           workspace=ws)

# Use version 3 of the environment.
deploy_env = Environment.get(workspace=ws,name="myenv",version="3")
inference_config = InferenceConfig(entry_script="score.py",
                                   environment=deploy_env)

service_name = 'myservice'
# Retrieve existing service.
service = Webservice(name=service_name, workspace=ws)



# Update to new model(s).
service.update(models=[new_model], inference_config=inference_config)
print(service.state)
print(service.get_logs())

使用 CLIUsing the CLI

您也可以使用 ML CLI 來更新 web 服務。You can also update a web service by using the ML CLI. 下列範例示範如何註冊新的模型,然後將 web 服務更新為使用新的模型:The following example demonstrates registering a new model and then updating a web service to use the new model:

az ml model register -n sklearn_mnist  --asset-path outputs/sklearn_mnist_model.pkl  --experiment-name myexperiment --output-metadata-file modelinfo.json
az ml service update -n myservice --model-metadata-file modelinfo.json

提示

在此範例中,會使用 JSON 檔,將來自註冊命令的模型資訊傳遞至 update 命令。In this example, a JSON document is used to pass the model information from the registration command into the update command.

若要將服務更新為使用新的專案腳本或環境,請建立推斷設定檔,並使用ic參數來指定它。To update the service to use a new entry script or environment, create an inference configuration file and specify it with the ic parameter.

如需詳細資訊,請參閱az ml 服務更新檔。For more information, see the az ml service update documentation.

持續部署模型Continuously deploy models

您可以使用Azure DevOps的 Machine Learning 延伸模組,持續部署模型。You can continuously deploy models by using the Machine Learning extension for Azure DevOps. 在 Azure Machine Learning 工作區中註冊新的機器學習模型時,您可以使用 Azure DevOps 的 Machine Learning 擴充功能來觸發部署管線。You can use the Machine Learning extension for Azure DevOps to trigger a deployment pipeline when a new machine learning model is registered in an Azure Machine Learning workspace.

  1. 註冊Azure Pipelines,讓您的應用程式持續整合並傳遞至任何平臺或雲端。Sign up for Azure Pipelines, which makes continuous integration and delivery of your application to any platform or cloud possible. (請注意,Azure Pipelines 與Machine Learning 管線不同)。(Note that Azure Pipelines isn't the same as Machine Learning pipelines.)

  2. 建立 Azure DevOps 專案。Create an Azure DevOps project.

  3. 安裝Azure Pipelines 的 Machine Learning 延伸模組。Install the Machine Learning extension for Azure Pipelines.

  4. 使用服務連線來設定與您 Azure Machine Learning 工作區的服務主體連線,以便您可以存取您的構件。Use service connections to set up a service principal connection to your Azure Machine Learning workspace so you can access your artifacts. 移至 專案設定,選取 服務連接,然後選取 Azure Resource ManagerGo to project settings, select Service connections, and then select Azure Resource Manager:

    選取 Azure Resource ManagerSelect Azure Resource Manager

  5. 在 [範圍層級] 清單中,選取 [ AzureMLWorkspace],然後輸入其餘的值:In the Scope level list, select AzureMLWorkspace, and then enter the rest of the values:

    選取 AzureMLWorkspace

  6. 若要使用 Azure Pipelines 持續部署機器學習模型,請在 [管線] 底下,選取 [發行]。To continuously deploy your machine learning model by using Azure Pipelines, under pipelines, select release. 新增 [成品],然後選取 [ AzureML 模型成品] 和您稍早建立的服務連接。Add a new artifact, and then select the AzureML Model artifact and the service connection that you created earlier. 選取要觸發部署的模型和版本:Select the model and version to trigger a deployment:

    選取 AzureML 模型Select AzureML Model

  7. 在您的模型成品上啟用模型觸發程式。Enable the model trigger on your model artifact. 當您開啟觸發程式時,每次在工作區中註冊該模型的指定版本(也就是最新版本)時,就會觸發 Azure DevOps 發行管線。When you turn on the trigger, every time the specified version (that is, the newest version) of that model is registered in your workspace, an Azure DevOps release pipeline is triggered.

    啟用模型觸發程式Enable the model trigger

如需更多範例專案和範例,請參閱 GitHub 中的下列範例存放庫:For more sample projects and examples, see these sample repos in GitHub:

下載模型Download a model

如果您想要下載您的模型以在自己的執行環境中使用,您可以使用下列 SDK/CLI 命令來執行此動作:If you want to download your model to use it in your own execution environment, you can do so with the following SDK / CLI commands:

SDKSDK:

model_path = Model(ws,'mymodel').download()

CLI:CLI:

az ml model download --model-id mymodel:1 --target-dir model_folder

封裝模型Package models

在某些情況下,您可能會想要建立 Docker 映射,而不部署模型(例如,您打算部署至 Azure App Service)。In some cases, you might want to create a Docker image without deploying the model (if, for example, you plan to deploy to Azure App Service). 或者,您可能會想要下載映射,並在本機 Docker 安裝上加以執行。Or you might want to download the image and run it on a local Docker installation. 您甚至可能會想要下載用來建立映射、加以檢查、修改,以及手動建立映射的檔案。You might even want to download the files used to build the image, inspect them, modify them, and build the image manually.

模型封裝可讓您執行這些動作。Model packaging enables you to do these things. 它會封裝裝載模型做為 web 服務所需的所有資產,並可讓您下載完全建立的 Docker 映射或建立元件所需的檔案。It packages all the assets needed to host a model as a web service and allows you to download either a fully built Docker image or the files needed to build one. 有兩種方式可以使用模型封裝:There are two ways to use model packaging:

下載封裝的模型: 下載包含模型的 Docker 映射,以及將其裝載為 web 服務所需的其他檔案。Download a packaged model: Download a Docker image that contains the model and other files needed to host it as a web service.

產生 Dockerfile: 下載建立 Docker 映射所需的 Dockerfile、模型、專案腳本和其他資產。Generate a Dockerfile: Download the Dockerfile, model, entry script, and other assets needed to build a Docker image. 接著,您可以在本機建立映射之前,先檢查檔案或進行變更。You can then inspect the files or make changes before you build the image locally.

這兩個套件都可以用來取得本機 Docker 映射。Both packages can be used to get a local Docker image.

提示

建立封裝與部署模型類似。Creating a package is similar to deploying a model. 您可以使用已註冊的模型和推斷設定。You use a registered model and an inference configuration.

重要

若要下載完整的映射,或在本機建立映射,您必須在開發環境中安裝DockerTo download a fully built image or build an image locally, you need to have Docker installed in your development environment.

下載封裝的模型Download a packaged model

下列範例會建立映射,它會在您工作區的 Azure container registry 中註冊:The following example builds an image, which is registered in the Azure container registry for your workspace:

package = Model.package(ws, [model], inference_config)
package.wait_for_creation(show_output=True)

建立封裝之後,您可以使用package.pull()將映射提取到您的本機 Docker 環境。After you create a package, you can use package.pull() to pull the image to your local Docker environment. 此命令的輸出會顯示映射的名稱。The output of this command will display the name of the image. 例如:For example:

Status: Downloaded newer image for myworkspacef78fd10.azurecr.io/package:20190822181338.Status: Downloaded newer image for myworkspacef78fd10.azurecr.io/package:20190822181338.

下載模型之後,請使用docker images命令來列出本機映射:After you download the model, use the docker images command to list the local images:

REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
myworkspacef78fd10.azurecr.io/package    20190822181338      7ff48015d5bd        4 minutes ago       1.43GB

若要根據此映射啟動本機容器,請使用下列命令,從 shell 或命令列啟動已命名的容器。To start a local container based on this image, use the following command to start a named container from the shell or command line. 將值取代為docker images命令所傳回的映射識別碼。 <imageid>Replace the <imageid> value with the image ID returned by the docker images command.

docker run -p 6789:5001 --name mycontainer <imageid>

此命令會啟動名為myimage的最新映射版本。This command starts the latest version of the image named myimage. 它會將本機埠6789對應至 web 服務所接聽之容器中的埠(5001)。It maps local port 6789 to the port in the container on which the web service is listening (5001). 它也會將名稱mycontainer指派給容器,讓容器更容易停止。It also assigns the name mycontainer to the container, which makes the container easier to stop. 啟動容器之後,您可以將要求提交至http://localhost:6789/scoreAfter the container is started, you can submit requests to http://localhost:6789/score.

產生 Dockerfile 和相依性Generate a Dockerfile and dependencies

下列範例示範如何下載 Dockerfile、模型和其他在本機建立映射所需的資產。The following example shows how to download the Dockerfile, model, and other assets needed to build an image locally. generate_dockerfile=True參數表示您想要檔案,而不是完整建立的映射。The generate_dockerfile=True parameter indicates that you want the files, not a fully built image.

package = Model.package(ws, [model], inference_config, generate_dockerfile=True)
package.wait_for_creation(show_output=True)
# Download the package.
package.save("./imagefiles")
# Get the Azure container registry that the model/Dockerfile uses.
acr=package.get_container_registry()
print("Address:", acr.address)
print("Username:", acr.username)
print("Password:", acr.password)

此程式碼會將建立映射所需的檔案下載imagefiles到目錄。This code downloads the files needed to build the image to the imagefiles directory. 儲存檔案中所包含的 Dockerfile 會參考存放在 Azure container registry 中的基底映射。The Dockerfile included in the saved files references a base image stored in an Azure container registry. 當您在本機 Docker 安裝上建立映射時,您必須使用位址、使用者名稱和密碼來向登錄進行驗證。When you build the image on your local Docker installation, you need to use the address, user name, and password to authenticate to the registry. 使用下列步驟,使用本機 Docker 安裝來建立映射:Use the following steps to build the image by using a local Docker installation:

  1. 在 shell 或命令列會話中,使用下列命令來向 Azure container registry 驗證 Docker。From a shell or command-line session, use the following command to authenticate Docker with the Azure container registry. <address><username> <password> 取出package.get_container_registry()的值取代、和。Replace <address>, <username>, and <password> with the values retrieved by package.get_container_registry().

    docker login <address> -u <username> -p <password>
    
  2. 若要建立映射,請使用下列命令。To build the image, use the following command. <imagefiles>取代為package.save()儲存檔案的目錄路徑。Replace <imagefiles> with the path of the directory where package.save() saved the files.

    docker build --tag myimage <imagefiles>
    

    此命令會將映射名稱設定myimage為。This command sets the image name to myimage.

若要驗證映射是否已建立,請使用docker images命令。To verify that the image is built, use the docker images command. 您應該會在myimage清單中看到影像:You should see the myimage image in the list:

REPOSITORY      TAG                 IMAGE ID            CREATED             SIZE
<none>          <none>              2d5ee0bf3b3b        49 seconds ago      1.43GB
myimage         latest              739f22498d64        3 minutes ago       1.43GB

若要根據此映射啟動新的容器,請使用下列命令:To start a new container based on this image, use the following command:

docker run -p 6789:5001 --name mycontainer myimage:latest

此命令會啟動名為myimage的最新映射版本。This command starts the latest version of the image named myimage. 它會將本機埠6789對應至 web 服務所接聽之容器中的埠(5001)。It maps local port 6789 to the port in the container on which the web service is listening (5001). 它也會將名稱mycontainer指派給容器,讓容器更容易停止。It also assigns the name mycontainer to the container, which makes the container easier to stop. 啟動容器之後,您可以將要求提交至http://localhost:6789/scoreAfter the container is started, you can submit requests to http://localhost:6789/score.

測試本機容器的範例用戶端Example client to test the local container

下列程式碼是可搭配容器使用的 Python 用戶端範例:The following code is an example of a Python client that can be used with the container:

import requests
import json

# URL for the web service.
scoring_uri = 'http://localhost:6789/score'

# Two sets of data to score, so we get two results back.
data = {"data":
        [
            [ 1,2,3,4,5,6,7,8,9,10 ],
            [ 10,9,8,7,6,5,4,3,2,1 ]
        ]
        }
# Convert to JSON string.
input_data = json.dumps(data)

# Set the content type.
headers = {'Content-Type': 'application/json'}

# Make the request and display the response.
resp = requests.post(scoring_uri, input_data, headers=headers)
print(resp.text)

例如,如需其他程式設計語言的用戶端,請參閱使用部署為 web 服務的模型For example clients in other programming languages, see Consume models deployed as web services.

停止 Docker 容器Stop the Docker container

若要停止容器,請從不同的 shell 或命令列使用下列命令:To stop the container, use the following command from a different shell or command line:

docker kill mycontainer

清除資源Clean up resources

若要刪除已部署的 Web 服務,請使用 service.delete()To delete a deployed web service, use service.delete(). 若要刪除已註冊的模型,請使用 model.delete()To delete a registered model, use model.delete().

如需詳細資訊,請參閱WebService. delete ()Model. delete ()的檔。For more information, see the documentation for WebService.delete() and Model.delete().

後續步驟Next steps