Share via


使用自訂 Docker 映像將模型定型

適用於:Python SDK azureml 第 1 版

在本文中,您將了解如何在使用 Azure Machine Learning 來將模型定型時使用自訂 Docker 映像。 您會使用本文中的範例指令碼,藉由建立卷積神經網路來分類寵物影像。

Azure Machine Learning 會提供預設的 Docker 基礎映像。 您也可以使用 Azure Machine Learning 環境來指定不同的基礎映像,例如您維護的其中一個 Azure Machine Learning 基礎映像或您自己的自訂映像。 自訂的基礎映像可讓您在執行定型作業時,仔細地管理相依性,並更加嚴密地控制元件版本。

必要條件

在下列任一環境中執行程式碼:

設定訓練實驗

在本節中,您會藉由初始化工作區、定義環境和設定計算目標,來設定訓練實驗。

初始化工作區

Azure Machine Learning 工作區是服務的最上層資源。 其可讓您集中處理您建立的所有成品。 在 Python SDK 中,您可以藉由建立 Workspace 物件來存取工作區成品。

透過為了滿足必要條件而建立的 config.json 檔案來建立 Workspace 物件。

from azureml.core import Workspace

ws = Workspace.from_config()

定義環境

建立 Environment 物件。

from azureml.core import Environment

fastai_env = Environment("fastai2")

下列程式碼中指定的基礎映像可支援 fast.ai 程式庫,而能夠實現分散式深度學習功能。 如需詳細資訊,請參閱 fast.ai Docker Hub 存放庫

當您使用自訂的 Docker 映像時,您可能已經正確設定好 Python 環境。 如果是這樣,請將 user_managed_dependencies 旗標設為 True,以使用自訂映像的內建 Python 環境。 依預設,Azure Machine Learning 會使用您指定的相依性來建置 Conda 環境。 服務會在該環境中執行指令碼,而不會使用您安裝在基礎映像上的任何 Python 程式庫。

fastai_env.docker.base_image = "fastdotai/fastai2:latest"
fastai_env.python.user_managed_dependencies = True

使用私人容器登錄 (選擇性)

若要使用位於工作區以外的私人容器登錄中的映像,請使用 docker.base_image_registry 來指定存放庫的位址以及使用者名稱和密碼:

# Set the container registry information.
fastai_env.docker.base_image_registry.address = "myregistry.azurecr.io"
fastai_env.docker.base_image_registry.username = "username"
fastai_env.docker.base_image_registry.password = "password"

使用自訂 Dockerfile (選擇性)

您也可以使用自訂 Dockerfile。 如果您需要安裝非 Python 套件作為相依性,請使用此方法。 請記得將基礎映像設定為 None

# Specify Docker steps as a string. 
dockerfile = r"""
FROM mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:20210615.v1
RUN echo "Hello from custom container!"
"""

# Set the base image to None, because the image is defined by Dockerfile.
fastai_env.docker.base_image = None
fastai_env.docker.base_dockerfile = dockerfile

# Alternatively, load the string from a file.
fastai_env.docker.base_image = None
fastai_env.docker.base_dockerfile = "./Dockerfile"

重要

Azure Machine Learning 僅支援會提供下列軟體的 Docker 映像:

  • Ubuntu 18.04 或更新版本。
  • Conda 4.7.# 或更新版本。
  • Python 3.7+。
  • 在任何用於定型的容器映像中,都需要可於 /bin/sh 取得的 POSIX 符合規範殼層。

如需如何建立和管理 Azure Machine Learning 環境的詳細資訊,請參閱建立和使用軟體環境

建立或附加計算目標

您必須建立計算目標以便將模型定型。 在本教學課程中,您會建立 AmlCompute 作為定型計算資源。

建立 AmlCompute 需要幾分鐘的時間。 如果您的工作區中已有 AmlCompute 資源,此程式碼會略過建立程序。

和其他 Azure 服務一樣,與 Azure Machine Learning 服務相關聯的特定資源 (例如 AmlCompute) 會有一些限制。 如需詳細資訊,請參閱預設限制和如何要求更高的配額

from azureml.core.compute import ComputeTarget, AmlCompute
from azureml.core.compute_target import ComputeTargetException

# Choose a name for your cluster.
cluster_name = "gpu-cluster"

try:
    compute_target = ComputeTarget(workspace=ws, name=cluster_name)
    print('Found existing compute target.')
except ComputeTargetException:
    print('Creating a new compute target...')
    compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_NC6',
                                                           max_nodes=4)

    # Create the cluster.
    compute_target = ComputeTarget.create(ws, cluster_name, compute_config)

    compute_target.wait_for_completion(show_output=True)

# Use get_status() to get a detailed status for the current AmlCompute.
print(compute_target.get_status().serialize())

重要

針對計算上的任何映像建置,都請使用 CPU SKU。

設定定型作業

在本教學課程中,請使用 GitHub 上的定型指令碼 train.py。 實際上,您可以採用任何自訂定型指令碼,依原樣與 Azure Machine Learning 搭配執行。

請建立 ScriptRunConfig 資源,以設定您的作業,從而讓作業在所需的計算目標上執行。

from azureml.core import ScriptRunConfig

src = ScriptRunConfig(source_directory='fastai-example',
                      script='train.py',
                      compute_target=compute_target,
                      environment=fastai_env)

提交您的定型作業

當您使用 ScriptRunConfig 物件來提交定型執行時,submit 方法會傳回類型為 ScriptRun 的物件。 所傳回的 ScriptRun 物件可讓您以程式設計方式存取定型執行的相關資訊。

from azureml.core import Experiment

run = Experiment(ws,'Tutorial-fastai').submit(src)
run.wait_for_completion(show_output=True)

警告

Azure Machine Learning 藉由複製整個來源目錄來執行定型指令碼。 如果您不想上傳敏感性資料,請使用 .ignore 檔案,或不要將敏感性資料放入來源目錄中。 請改用資料存放區來存取資料。

下一步

在本文中,您已使用自訂 Docker 映像來將模型定型。 若要深入了解 Azure Machine Learning,請參閱下列其他文章: