您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:生成用于批量评分的 Azure 机器学习管道Tutorial: Build an Azure Machine Learning pipeline for batch scoring

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

在本教程中,你将使用 Azure 机器学习中的某个管道运行批量评分作业。In this tutorial, you use a pipeline in Azure Machine Learning to run a batch scoring job. 本示例使用预先训练的 Inception-V3 卷积神经网络 Tensorflow 模型来对不带标签的图像进行分类。The example uses the pretrained Inception-V3 convolutional neural network Tensorflow model to classify unlabeled images. 生成并发布管道后,你将配置一个 REST 终结点,用于从任何平台上的任何 HTTP 库触发该管道。After you build and publish a pipeline, you configure a REST endpoint that you can use to trigger the pipeline from any HTTP library on any platform.

机器学习管道可以优化工作流以提高其速度、可移植性和可重用性,使你能够将工作重心放在专业技术和机器学习,而不是在基础结构和自动化上。Machine learning pipelines optimize your workflow with speed, portability, and reuse, so you can focus on your expertise - machine learning - instead of on infrastructure and automation. 详细了解机器学习管道Learn more about machine learning pipelines.

在本教程中,请完成以下任务:In this tutorial, you complete the following tasks:

  • 配置工作区并下载示例数据Configure workspace and download sample data
  • 创建用于提取和输出数据的数据对象Create data objects to fetch and output data
  • 下载、准备模型并将其注册到工作区中Download, prepare, and register the model in your workspace
  • 预配计算目标并创建评分脚本Provision compute targets and create a scoring script
  • 生成、运行并发布管道Build, run, and publish a pipeline
  • 为管道启用 REST 终结点Enable a REST endpoint for the pipeline

如果没有 Azure 订阅,请在开始之前创建一个免费帐户。If you don’t have an Azure subscription, create a free account before you begin. 立即试用免费版或付费版 Azure 机器学习Try the free or paid version of Azure Machine Learning today.

必备条件Prerequisites

  • 如果你没有 Azure 机器学习工作区或笔记本虚拟机,请完成设置教程的第 1 部分If you don't already have an Azure Machine Learning workspace or notebook virtual machine, complete Part 1 of the setup tutorial.
  • 完成设置教程后,使用同一笔记本服务器打开 tutorials/machine-learning-pipelines-advanced/tutorial-pipeline-batch-scoring-classification.ipynb 笔记本。When you finish the setup tutorial, use the same notebook server to open the tutorials/machine-learning-pipelines-advanced/tutorial-pipeline-batch-scoring-classification.ipynb notebook.

如果要在自己的本地环境中运行设置教程,可以访问 GitHub 上的教程。If you want to run the setup tutorial in your own local environment, you can access the tutorial on GitHub. 运行 pip install azureml-sdk[notebooks] azureml-pipeline-core azureml-pipeline-steps pandas requests 以获取所需的包。Run pip install azureml-sdk[notebooks] azureml-pipeline-core azureml-pipeline-steps pandas requests to get the required packages.

配置工作区并创建数据存储Configure workspace and create a datastore

从现有的 Azure 机器学习工作区创建工作区对象。Create a workspace object from the existing Azure Machine Learning workspace.

  • 工作区是可接受 Azure 订阅和资源信息的类。A workspace is a class that accepts your Azure subscription and resource information. 工作区还可创建用于监视和跟踪模型运行的云资源。The workspace also creates a cloud resource you can use to monitor and track your model runs.
  • Workspace.from_config() 读取 config.json 文件并将身份验证详细信息加载到名为 ws 的对象中。Workspace.from_config() reads the config.json file and then loads the authentication details into an object named ws. 本教程中的代码使用 ws 对象。The ws object is used in the code throughout this tutorial.
from azureml.core import Workspace
ws = Workspace.from_config()

为示例图像创建数据存储Create a datastore for sample images

pipelinedata 帐户中,从 sampledata 公共 Blob 容器获取 ImageNet 评估公共数据示例。On the pipelinedata account, get the ImageNet evaluation public data sample from the sampledata public blob container. 调用 register_azure_blob_container() 可使数据可用于名为 images_datastore 的工作区。Call register_azure_blob_container() to make the data available to the workspace under the name images_datastore. 然后,将工作区的默认数据存储设置为输出数据存储。Then, set the workspace default datastore as the output datastore. 使用输出数据存储在管道中为输出评分。Use the output datastore to score output in the pipeline.

from azureml.core.datastore import Datastore

batchscore_blob = Datastore.register_azure_blob_container(ws, 
                      datastore_name="images_datastore", 
                      container_name="sampledata", 
                      account_name="pipelinedata", 
                      overwrite=True)

def_data_store = ws.get_default_datastore()

创建数据对象Create data objects

生成管道时,DataReference 对象将从工作区数据存储中读取数据。When you build a pipeline, a DataReference object reads data from the workspace datastore. PipelineData 对象在管道步骤之间传输中间数据。A PipelineData object transfers intermediate data between pipeline steps.

重要

本教程中的批量评分示例只使用一个管道步骤。The batch scoring example in this tutorial uses only one pipeline step. 在包含多个步骤的用例中,典型流包括以下步骤:In use cases that have multiple steps, the typical flow will include these steps:

  1. 使用 DataReference 对象作为提取原始数据的输入,执行某种转换,然后输出 PipelineData 对象。 Use DataReference objects as inputs to fetch raw data, perform some transformation, and then output a PipelineData object.

  2. 使用上一步骤中的 PipelineData 输出对象作为输入对象 。Use the PipelineData output object in the preceding step as an input object. 针对后续步骤重复此过程。Repeat it for subsequent steps.

在此场景中,你将创建与输入图像和分类标签(y-test 值)的数据存储目录相对应的 DataReference 对象。In this scenario, you create DataReference objects that correspond to the datastore directories for both the input images and the classification labels (y-test values). 此外,将为批量评分输出数据创建一个 PipelineData 对象。You also create a PipelineData object for the batch scoring output data.

from azureml.data.data_reference import DataReference
from azureml.pipeline.core import PipelineData

input_images = DataReference(datastore=batchscore_blob, 
                             data_reference_name="input_images",
                             path_on_datastore="batchscoring/images",
                             mode="download"
                            )

label_dir = DataReference(datastore=batchscore_blob, 
                          data_reference_name="input_labels",
                          path_on_datastore="batchscoring/labels",
                          mode="download"                          
                         )

output_dir = PipelineData(name="scores", 
                          datastore=def_data_store, 
                          output_path_on_compute="batchscoring/results")

下载并注册模型Download and register the model

下载预先训练的 Tensorflow 模型用于管道中的批量评分。Download the pretrained Tensorflow model to use it for batch scoring in a pipeline. 首先创建一个用于存储模型的本地目录。First, create a local directory where you store the model. 然后下载并提取该模型。Then, download and extract the model.

import os
import tarfile
import urllib.request

if not os.path.isdir("models"):
    os.mkdir("models")
    
response = urllib.request.urlretrieve("http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz", "model.tar.gz")
tar = tarfile.open("model.tar.gz", "r:gz")
tar.extractall("models")

接下来,将模型注册到工作区,以便能够在管道流程中轻松检索该模型。Next, register the model to your workspace, so you can easily retrieve the model in the pipeline process. register() 静态函数中,model_name 参数是用于在整个 SDK 中查找模型的键。In the register() static function, the model_name parameter is the key you use to locate your model throughout the SDK.

from azureml.core.model import Model
 
model = Model.register(model_path="models/inception_v3.ckpt",
                       model_name="inception",
                       tags={"pretrained": "inception"},
                       description="Imagenet trained tensorflow inception",
                       workspace=ws)

创建并附加远程计算目标Create and attach the remote compute target

机器学习管道无法在本地运行,因此你需要在云资源或远程计算目标上运行这些管道。 Machine learning pipelines can't be run locally, so you run them on cloud resources or remote compute targets. 远程计算目标是可重用的虚拟计算环境,可在其中运行试验和机器学习工作流。A remote compute target is a reusable virtual compute environment where you run experiments and machine learning workflows.

运行以下代码创建支持 GPU 的 AmlCompute 目标,并将其附加到工作区。Run the following code to create a GPU-enabled AmlCompute target, and then attach it to your workspace. 有关计算目标的详细信息,请参阅概念文章For more information about compute targets, see the conceptual article.

from azureml.core.compute import AmlCompute, ComputeTarget
from azureml.exceptions import ComputeTargetException
compute_name = "gpu-cluster"

# checks to see if compute target already exists in workspace, else create it
try:
    compute_target = ComputeTarget(workspace=ws, name=compute_name)
except ComputeTargetException:
    config = AmlCompute.provisioning_configuration(vm_size="STANDARD_NC6",
                                                   vm_priority="lowpriority", 
                                                   min_nodes=0, 
                                                   max_nodes=1)

    compute_target = ComputeTarget.create(workspace=ws, name=compute_name, provisioning_configuration=config)
    compute_target.wait_for_completion(show_output=True, min_node_count=None, timeout_in_minutes=20)

编写评分脚本Write a scoring script

若要执行评分,请创建名为 batch_scoring.py 的批量评分脚本,并将其写入当前目录。To do the scoring, create a batch scoring script called batch_scoring.py, and then write it to the current directory. 该脚本将提取输入图像,应用分类模型,然后将预测结果输出到结果文件中。The script takes input images, applies the classification model, and then outputs the predictions to a results file.

脚本 batch_scoring.py 采用以下参数,这些参数是从稍后在本教程中创建的管道步骤传递的:The batch_scoring.py script takes the following parameters, which are passed from the pipeline step that you create later in this tutorial:

  • --model_name设置用户帐户 :所用模型的名称。--model_name: The name of the model being used.
  • --label_dir设置用户帐户 :保存 labels.txt 文件的目录。--label_dir: The directory that holds the labels.txt file.
  • --dataset_path设置用户帐户 :包含输入图像的目录。--dataset_path: The directory that contains the input images.
  • --output_dir设置用户帐户 :脚本针对数据运行模型后 results-label.txt 文件的输出目录。--output_dir: The output directory for the results-label.txt file after the script runs the model on the data.
  • --batch_size设置用户帐户 :运行模型时使用的批大小。--batch_size: The batch size used in running the model.

管道基础结构使用 ArgumentParser 类将参数传入管道步骤。The pipeline infrastructure uses the ArgumentParser class to pass parameters into pipeline steps. 例如,在以下代码中,为第一个参数 --model_name 指定了属性标识符 model_nameFor example, in the following code, the first argument --model_name is given the property identifier model_name. main() 函数中,使用 Model.get_model_path(args.model_name) 访问此属性。In the main() function, Model.get_model_path(args.model_name) is used to access this property.

%%writefile batch_scoring.py

import os
import argparse
import datetime
import time
import tensorflow as tf
from math import ceil
import numpy as np
import shutil
from tensorflow.contrib.slim.python.slim.nets import inception_v3
from azureml.core.model import Model

slim = tf.contrib.slim

parser = argparse.ArgumentParser(description="Start a tensorflow model serving")
parser.add_argument('--model_name', dest="model_name", required=True)
parser.add_argument('--label_dir', dest="label_dir", required=True)
parser.add_argument('--dataset_path', dest="dataset_path", required=True)
parser.add_argument('--output_dir', dest="output_dir", required=True)
parser.add_argument('--batch_size', dest="batch_size", type=int, required=True)

args = parser.parse_args()

image_size = 299
num_channel = 3

# create output directory if it does not exist
os.makedirs(args.output_dir, exist_ok=True)


def get_class_label_dict(label_file):
    label = []
    proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
    for l in proto_as_ascii_lines:
        label.append(l.rstrip())
    return label


class DataIterator:
    def __init__(self, data_dir):
        self.file_paths = []
        image_list = os.listdir(data_dir)
        self.file_paths = [data_dir + '/' + file_name.rstrip() for file_name in image_list]
        self.labels = [1 for file_name in self.file_paths]

    @property
    def size(self):
        return len(self.labels)

    def input_pipeline(self, batch_size):
        images_tensor = tf.convert_to_tensor(self.file_paths, dtype=tf.string)
        labels_tensor = tf.convert_to_tensor(self.labels, dtype=tf.int64)
        input_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], shuffle=False)
        labels = input_queue[1]
        images_content = tf.read_file(input_queue[0])

        image_reader = tf.image.decode_jpeg(images_content, channels=num_channel, name="jpeg_reader")
        float_caster = tf.cast(image_reader, tf.float32)
        new_size = tf.constant([image_size, image_size], dtype=tf.int32)
        images = tf.image.resize_images(float_caster, new_size)
        images = tf.divide(tf.subtract(images, [0]), [255])

        image_batch, label_batch = tf.train.batch([images, labels], batch_size=batch_size, capacity=5 * batch_size)
        return image_batch


def main(_):
    label_file_name = os.path.join(args.label_dir, "labels.txt")
    label_dict = get_class_label_dict(label_file_name)
    classes_num = len(label_dict)
    test_feeder = DataIterator(data_dir=args.dataset_path)
    total_size = len(test_feeder.labels)
    count = 0

    # get model from model registry
    model_path = Model.get_model_path(args.model_name)

    with tf.Session() as sess:
        test_images = test_feeder.input_pipeline(batch_size=args.batch_size)
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            input_images = tf.placeholder(tf.float32, [args.batch_size, image_size, image_size, num_channel])
            logits, _ = inception_v3.inception_v3(input_images,
                                                  num_classes=classes_num,
                                                  is_training=False)
            probabilities = tf.argmax(logits, 1)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        out_filename = os.path.join(args.output_dir, "result-labels.txt")
        with open(out_filename, "w") as result_file:
            i = 0
            while count < total_size and not coord.should_stop():
                test_images_batch = sess.run(test_images)
                file_names_batch = test_feeder.file_paths[i * args.batch_size:
                                                          min(test_feeder.size, (i + 1) * args.batch_size)]
                results = sess.run(probabilities, feed_dict={input_images: test_images_batch})
                new_add = min(args.batch_size, total_size - count)
                count += new_add
                i += 1
                for j in range(new_add):
                    result_file.write(os.path.basename(file_names_batch[j]) + ": " + label_dict[results[j]] + "\n")
                result_file.flush()
            coord.request_stop()
            coord.join(threads)

        shutil.copy(out_filename, "./outputs/")

if __name__ == "__main__":
    tf.app.run()

提示

本教程中的管道只有一个步骤,它会将输出写入某个文件。The pipeline in this tutorial has only one step, and it writes the output to a file. 对于多步骤管道,你也可以使用 ArgumentParser 来定义要将输出数据写入到的目录,以便将其输入到后续步骤。For multi-step pipelines, you also use ArgumentParser to define a directory to write output data for input to subsequent steps. 有关使用 ArgumentParser 设计模式在多个管道步骤之间传递数据的示例,请参阅笔记本For an example of passing data between multiple pipeline steps by using the ArgumentParser design pattern, see the notebook.

生成并运行管道Build and run the pipeline

在运行管道之前,请创建一个用于定义 Python 环境的对象,并创建 batch_scoring.py 脚本所需的依赖项。Before you run the pipeline, create an object that defines the Python environment and creates the dependencies that your batch_scoring.py script requires. 所需的主要依赖项是 Tensorflow,但你还需要通过 SDK 为后台进程安装 azureml-defaultsThe main dependency required is Tensorflow, but you also install azureml-defaults from the SDK for background processes. 使用依赖项创建 RunConfiguration 对象。Create a RunConfiguration object by using the dependencies. 另外,指定 Docker 和 Docker-GPU 支持。Also, specify Docker and Docker-GPU support.

from azureml.core.runconfig import DEFAULT_GPU_IMAGE
azureml.core.runconfig import RunConfiguration
from azureml.core.conda_dependencies import CondaDependencies

cd = CondaDependencies.create(pip_packages=["tensorflow-gpu==1.13.1", "azureml-defaults"])

amlcompute_run_config = RunConfiguration(conda_dependencies=cd)
amlcompute_run_config.environment.docker.enabled = True
amlcompute_run_config.environment.docker.base_image = DEFAULT_GPU_IMAGE
amlcompute_run_config.environment.spark.precache_packages = False

参数化管道Parameterize the pipeline

定义管道的自定义参数以控制批大小。Define a custom parameter for the pipeline to control the batch size. 通过 REST 终结点发布并公开管道后,也会公开配置的任何参数。After the pipeline is published and exposed via a REST endpoint, any configured parameters are also exposed. 可以在通过 HTTP 请求重新运行管道时,在 JSON 有效负载中指定自定义参数。You can specify custom parameters in the JSON payload when you rerun the pipeline via an HTTP request.

创建 PipelineParameter 对象来启用此行为,并定义名称和默认值。Create a PipelineParameter object to enable this behavior and to define a name and default value.

from azureml.pipeline.core.graph import PipelineParameter
batch_size_param = PipelineParameter(name="param_batch_size", default_value=20)

创建管道步骤Create the pipeline step

管道步骤是一个对象,用于封装运行管道所需的任何内容,其中包括:A pipeline step is an object that encapsulates everything you need to run a pipeline, including:

  • 环境和依赖项设置Environment and dependency settings
  • 要在其上运行管道的计算资源The compute resource to run the pipeline on
  • 输入和输出数据,以及任何自定义参数Input and output data, and any custom parameters
  • 对执行步骤期间要运行的脚本或 SDK 逻辑的引用Reference to a script or SDK logic to run during the step

有多个类继承自父类 PipelineStepMultiple classes inherit from the parent class PipelineStep. 你可以选择适当的类,以使用特定的框架和堆栈生成步骤。You can choose classes to use specific frameworks or stacks to build a step. 在本示例中,你将使用 PythonScriptStep 类来定义使用自定义 Python 脚本的步骤逻辑。In this example, you use the PythonScriptStep class to define your step logic by using a custom Python script. 如果脚本的某个自变量是步骤的输入或步骤的输出,则必须分别在 arguments 数组以及 inputoutput 参数中定义该自变量。 If an argument to your script is either an input to the step or an output of the step, the argument must be defined both in the arguments array and in either the input or the output parameter, respectively.

如果存在多个步骤,outputs 数组中的某个对象引用可用作后续管道步骤的输入。 In scenarios where there is more than one step, an object reference in the outputs array becomes available as an input for a subsequent pipeline step.

from azureml.pipeline.steps import PythonScriptStep

batch_score_step = PythonScriptStep(
    name="batch_scoring",
    script_name="batch_scoring.py",
    arguments=["--dataset_path", input_images, 
               "--model_name", "inception",
               "--label_dir", label_dir, 
               "--output_dir", output_dir, 
               "--batch_size", batch_size_param],
    compute_target=compute_target,
    inputs=[input_images, label_dir],
    outputs=[output_dir],
    runconfig=amlcompute_run_config
)

有关可对不同步骤类型使用的所有类的列表,请参阅步骤包For a list of all the classes you can use for different step types, see the steps package.

运行管道Run the pipeline

现在请运行管道。Now, run the pipeline. 首先,使用工作区引用和创建的管道步骤创建一个 Pipeline 对象。First, create a Pipeline object by using your workspace reference and the pipeline step you created. steps 参数是步骤数组。The steps parameter is an array of steps. 在本例中,批量评分只有一个步骤。In this case, there's only one step for batch scoring. 若要生成包含多个步骤的管道,请将步骤按顺序放入此数组。To build pipelines that have multiple steps, place the steps in order in this array.

接下来,使用 Experiment.submit() 函数提交管道以供执行。Next, use the Experiment.submit() function to submit the pipeline for execution. 还可以指定自定义参数 param_batch_sizeYou also specify the custom parameter param_batch_size. wait_for_completion 函数将在管道生成过程中输出日志。The wait_for_completion function outputs logs during the pipeline build process. 可以使用日志来查看当前进度。You can use the logs to see current progress.

重要

首次管道运行需要大约 15 分钟。 The first pipeline run takes roughly 15 minutes. 必须下载所有依赖项、创建 Docker 映像,并预配和创建 Python 环境。All dependencies must be downloaded, a Docker image is created, and the Python environment is provisioned and created. 再次运行管道所花费的时间会大幅减少,因为会重复使用这些资源,而无需再次创建。Running the pipeline again takes significantly less time because those resources are reused instead of created. 但是,管道的总运行时间取决于脚本的工作负荷,以及每个管道步骤中运行的进程数。However, total run time for the pipeline depends on the workload of your scripts and the processes that are running in each pipeline step.

from azureml.core import Experiment
from azureml.pipeline.core import Pipeline

pipeline = Pipeline(workspace=ws, steps=[batch_score_step])
pipeline_run = Experiment(ws, 'batch_scoring').submit(pipeline, pipeline_parameters={"param_batch_size": 20})
pipeline_run.wait_for_completion(show_output=True)

下载并查看输出Download and review output

运行以下代码下载通过 batch_scoring.py 脚本创建的输出文件。Run the following code to download the output file that's created from the batch_scoring.py script. 然后浏览评分结果。Then, explore the scoring results.

import pandas as pd

step_run = list(pipeline_run.get_children())[0]
step_run.download_file("./outputs/result-labels.txt")

df = pd.read_csv("result-labels.txt", delimiter=":", header=None)
df.columns = ["Filename", "Prediction"]
df.head(10)
文件名Filename 预测Prediction
00 ILSVRC2012_val_00000102.JPEGILSVRC2012_val_00000102.JPEG Rhodesian RidgebackRhodesian Ridgeback
11 ILSVRC2012_val_00000103.JPEGILSVRC2012_val_00000103.JPEG tripodtripod
22 ILSVRC2012_val_00000104.JPEGILSVRC2012_val_00000104.JPEG typewriter keyboardtypewriter keyboard
33 ILSVRC2012_val_00000105.JPEGILSVRC2012_val_00000105.JPEG silky terriersilky terrier
44 ILSVRC2012_val_00000106.JPEGILSVRC2012_val_00000106.JPEG Windsor tieWindsor tie
55 ILSVRC2012_val_00000107.JPEGILSVRC2012_val_00000107.JPEG harvestmanharvestman
66 ILSVRC2012_val_00000108.JPEGILSVRC2012_val_00000108.JPEG violinviolin
77 ILSVRC2012_val_00000109.JPEGILSVRC2012_val_00000109.JPEG loudspeakerloudspeaker
88 ILSVRC2012_val_00000110.JPEGILSVRC2012_val_00000110.JPEG apronapron
99 ILSVRC2012_val_00000111.JPEGILSVRC2012_val_00000111.JPEG American lobsterAmerican lobster

从 REST 终结点发布和运行Publish and run from a REST endpoint

运行以下代码,将管道发布到工作区。Run the following code to publish the pipeline to your workspace. 在 Azure 机器学习工作室的工作区中,可以看到管道的元数据,包括运行历史记录和持续时间。In your workspace in Azure Machine Learning studio, you can see metadata for the pipeline, including run history and durations. 还可以从工作室手动运行管道。You can also run the pipeline manually from the studio.

发布管道会启用一个 REST 终结点,用于从任何平台上的任何 HTTP 库重新运行该管道。Publishing the pipeline enables a REST endpoint that you can use to run the pipeline from any HTTP library on any platform.

published_pipeline = pipeline_run.publish_pipeline(
    name="Inception_v3_scoring", description="Batch scoring using Inception v3 model", version="1.0")

published_pipeline

若要从 REST 终结点运行管道,需要获取 OAuth2 Bearer-type 身份验证标头。To run the pipeline from the REST endpoint, you need an OAuth2 Bearer-type authentication header. 为方便演示,以下示例使用了交互式身份验证,但对于需要自动化身份验证或无头身份验证的大多数生产方案,请使用此笔记本中所述的服务主体身份验证。The following example uses interactive authentication (for illustration purposes), but for most production scenarios that require automated or headless authentication, use service principal authentication as described in this notebook.

服务主体身份验证涉及到在 Azure Active Directory 中创建应用注册。 Service principal authentication involves creating an App Registration in Azure Active Directory. 首先生成客户端机密,然后为服务主体授予对机器学习工作区的角色访问权限。 First, you generate a client secret, and then you grant your service principal role access to your machine learning workspace. 使用 ServicePrincipalAuthentication 类来管理身份验证流。Use the ServicePrincipalAuthentication class to manage your authentication flow.

InteractiveLoginAuthenticationServicePrincipalAuthentication 均继承自 AbstractAuthenticationBoth InteractiveLoginAuthentication and ServicePrincipalAuthentication inherit from AbstractAuthentication. 在这两种情况下,请以相同的方式使用 get_authentication_header() 函数来提取标头:In both cases, use the get_authentication_header() function in the same way to fetch the header:

from azureml.core.authentication import InteractiveLoginAuthentication

interactive_auth = InteractiveLoginAuthentication()
auth_header = interactive_auth.get_authentication_header()

从已发布的管道对象的 endpoint 属性获取 REST URL。Get the REST URL from the endpoint property of the published pipeline object. 也可以在 Azure 机器学习工作室的工作区中找到该 REST URL。You can also find the REST URL in your workspace in Azure Machine Learning studio.

对终结点生成 HTTP POST 请求。Build an HTTP POST request to the endpoint. 在请求中指定身份验证标头。Specify your authentication header in the request. 添加包含试验名称和批大小参数的 JSON 有效负载对象。Add a JSON payload object that has the experiment name and the batch size parameter. 如本教程前面所述,param_batch_size 将传递到 batch_scoring.py 脚本,因为已在步骤配置中将其定义为 PipelineParameter 对象。As noted earlier in the tutorial, param_batch_size is passed through to your batch_scoring.py script because you defined it as a PipelineParameter object in the step configuration.

发出触发运行的请求。Make the request to trigger the run. 包含相应的代码用于访问响应字典中的 Id 密钥以获取运行 ID 的值。Include code to access the Id key from the response dictionary to get the value of the run ID.

import requests

rest_endpoint = published_pipeline.endpoint
response = requests.post(rest_endpoint, 
                         headers=auth_header, 
                         json={"ExperimentName": "batch_scoring",
                               "ParameterAssignments": {"param_batch_size": 50}})
run_id = response.json()["Id"]

使用运行 ID 监视新运行的状态。Use the run ID to monitor the status of the new run. 新的运行需要花费 10-15 分钟来完成。The new run takes another 10-15 min to finish.

新的运行类似于在本教程中前面运行的管道。The new run will look similar to the pipeline you ran earlier in the tutorial. 可以选择不查看完整输出。You can choose not to view the full output.

from azureml.pipeline.core.run import PipelineRun
from azureml.widgets import RunDetails

published_pipeline_run = PipelineRun(ws.experiments["batch_scoring"], run_id)
RunDetails(published_pipeline_run).show()

清理资源Clean up resources

如果你打算运行其他 Azure 机器学习教程,请不要完成本部分。Don't complete this section if you plan to run other Azure Machine Learning tutorials.

停止计算实例Stop the compute instance

如果使用了计算实例或笔记本 VM,请停止未使用的 VM,以降低成本。If you used a compute instance or Notebook VM, stop the VM when you are not using it to reduce cost.

  1. 在工作区中选择“计算”。 In your workspace, select Compute.

  2. 从列表中选择 VM。From the list, select the VM.

  3. 选择“停止” 。Select Stop.

  4. 准备好再次使用服务器时,选择“启动” 。When you're ready to use the server again, select Start.

删除所有内容Delete everything

如果不打算使用已创建的资源,请删除它们,以免产生任何费用:If you don't plan to use the resources you created, delete them, so you don't incur any charges:

  1. 在 Azure 门户的左侧菜单中选择“资源组” 。In the Azure portal, in the left menu, select Resource groups.
  2. 在资源组列表中,选择创建的资源组。In the list of resource groups, select the resource group you created.
  3. 选择“删除资源组” 。Select Delete resource group.
  4. 输入资源组名称。Enter the resource group name. 然后选择“删除” 。Then, select Delete.

还可保留资源组,但请删除单个工作区。You can also keep the resource group but delete a single workspace. 显示工作区属性,然后选择“删除” 。Display the workspace properties, and then select Delete.

后续步骤Next steps

在本机器学习管道教程中,你已完成以下任务:In this machine learning pipelines tutorial, you did the following tasks:

  • 使用环境依赖项生成了一个要在远程 GPU 计算资源上运行的管道。Built a pipeline with environment dependencies to run on a remote GPU compute resource.
  • 使用预先训练的 Tensorflow 模型创建了一个用于运行批量预测的评分脚本。Created a scoring script to run batch predictions by using a pretrained Tensorflow model.
  • 发布了管道,并使其从 REST 终结点运行。Published a pipeline and enabled it to be run from a REST endpoint.

有关演示如何使用机器学习 SDK 生成管道的更多示例,请参阅笔记本存储库For more examples of how to build pipelines by using the machine learning SDK, see the notebook repository.