Azure Machine Learning でモデルを操作する

適用対象:Azure CLI ml extension v2 (現行)Python SDK azure-ai-ml v2 (現行)

Azure Machine Learning では、さまざまな種類のモデルを利用できます。 この記事では、Azure Machine Learning を使用して、カスタム、MLflow、Triton などのさまざまなモデルの種類を操作する方法について説明します。 また、さまざまな場所からモデルを登録する方法と、Azure Machine Learning SDK、ユーザー インターフェイス (UI)、Azure Machine Learning CLI を使用してモデルを管理する方法についても説明します。

ヒント

SDK/CLI v1 を使用する、作成されたモデル資産がある場合でも、SDK/CLI v2 で使用できます。 完全な下位互換性が用意されています。 V1 SDK に登録されているすべてのモデルに型 custom が割り当てられます。

前提条件

  • Azure サブスクリプション。 Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。 無料版または有料版の Azure Machine Learning をお試しください。
  • Azure Machine Learning ワークスペース。
  • Azure Machine Learning SDK v2 for Python
  • Azure Machine Learning CLI v2

さらに、次を行う必要があります。

サポートされているパス

登録するモデルを指定するときに、データまたはジョブの場所を指し示す path パラメーターを指定する必要があります。 下の表は、Azure Machine Learning でサポートされているさまざまなデータの場所と path パラメーターの例を示しています。

Location
ローカル コンピューター上のパス mlflow-model/model.pkl
Azure Machine Learning データストアのパス azureml://datastores/<datastore-name>/paths/<path_on_datastore>
Azure Machine Learning ジョブからのパス azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
MLflow ジョブからのパス runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
Azure Machine Learning ワークスペースのモデル資産からのパス azureml:<model-name>:<version>
Azure Machine Learning レジストリのモデル資産からのパス azureml://registries/<registry-name>/models/<model-name>/versions/<version>

サポートされているモード

モデルの入力/出力を伴うジョブを実行する場合、たとえば、モデルを読み取り専用でマウントするか、コンピューティング先にダウンロードするかなどの "モード" を指定できます。 下の表は、さまざまな種類/モード/入力/出力の組み合わせの使用可能なモードを示しています。

[入力または出力] upload download ro_mount rw_mount direct
custom ファイル 入力
custom フォルダー 入力
mlflow 入力
custom ファイル 出力
custom フォルダー 出力
mlflow 出力

Jupyter ノートブックで経過をたどる

Jupyter Notebook でこのサンプルの経過をたどることができます。 azureml-examples リポジトリで、ノートブック model.ipynb を開きます。

モデル レジストリでのモデルの作成

モデルの登録によって、モデルを Azure クラウドに格納し、ワークスペースでバージョン管理できます。 モデルのレジストリは、トレーニングしたモデルの整理と追跡に役立ちます。

このセクションのコード スニペットは、次の方法を扱います。

  • CLI を使用して、Machine Learning でモデルを資産として登録します。
  • SDK を使用して、Machine Learning でモデルを資産として登録します。
  • UI を使用して、Machine Learning でモデルを資産として登録します。

これらのスニペットでは、custommlflow が使用されます。

  • custom は、Azure Machine Learning で現在サポートされていないカスタム標準でトレーニングされたモデルのファイルまたはフォルダーを参照する型です。
  • mlflow は、mlflow でトレーニングされたモデルを参照する型です。 MLflow トレーニング済みモデルは、MLmodel ファイル、model ファイル、conda dependencies ファイル、requirements.txt ファイルを含むフォルダー内にあります。

ワークスペースに接続する

まず、これから作業する Azure Machine Learning ワークスペースに接続しましょう。

az account set --subscription <subscription>
az configure --defaults workspace=<workspace> group=<resource-group> location=<location>

CLI を使用して、Machine Learning でモデルを資産として登録する

次のタブを使用して、モデルが配置されている場所を選択します。

$schema: https://azuremlschemas.azureedge.net/latest/model.schema.json
name: local-file-example
path: mlflow-model/model.pkl
description: Model created from local file.
az ml model create -f <file-name>.yml

完全な例については、モデル YAML を参照してください。

SDK を使用して、Machine Learning でモデルを資産として登録する

次のタブを使用して、モデルが配置されている場所を選択します。

from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes

file_model = Model(
    path="mlflow-model/model.pkl",
    type=AssetTypes.CUSTOM_MODEL,
    name="local-file-example",
    description="Model created from local file.",
)
ml_client.models.create_or_update(file_model)

UI を使用して、Machine Learning でモデルを資産として登録する

Machine Learning でモデルを作成するには、UI から [モデル] ページを開きます。 [モデルの登録] を選択し、モデルが配置されている場所を選択します。 必須フィールドに入力し、[登録] を選択します。

モデルを登録する UI のスクリーンショット。


モデルを管理する

SDK と CLI (v2) では、Azure Machine Learning モデル資産のライフサイクルを管理することもできます。

List

次にワークスペース内のすべてのモデルを一覧表示します。

az ml model list

指定された名前のすべてのモデル バージョンを一覧表示します。

az ml model list --name run-model-example

表示

特定のモデルの詳細を取得します。

az ml model show --name run-model-example --version 1

更新

次の特定のモデルの変更可能なプロパティを更新します。

az ml model update --name  run-model-example --version 1 --set description="This is an updated description." --set tags.stage="Prod"

重要

モデルの場合、更新できるのは descriptiontags のみです。 その他のすべてのプロパティは変更できません。これらのプロパティを変更する必要がある場合は、新しいバージョンのモデルを作成する必要があります。

アーカイブ

モデルをアーカイブすると、既定でリスト クエリ (az ml model list) から非表示になります。 アーカイブされたモデルは、ワークフローで参照し、使用し続けることができます。 モデルのすべてのバージョンまたは特定のバージョンのみをアーカイブできます。

バージョンを指定しない場合、特定の名前のモデルのすべてのバージョンがアーカイブされます。 アーカイブされたモデル コンテナーの下に新しいモデル バージョンを作成すると、その新しいバージョンも自動的にアーカイブ済みとして設定されます。

次のモデルのすべてのバージョンをアーカイブします。

az ml model archive --name run-model-example

次の特定のモデルのバージョンをアーカイブします。

az ml model archive --name run-model-example --version 1

トレーニングにモデルを使用する

SDK と CLI (v2) では、トレーニング ジョブのモデルを入力または出力として使用することもできます。

ジョブの入力としてモデルを使用する

ジョブ仕様 YAML ファイル (<file-name>.yml) を作成します。 ジョブの inputs セクションで次を指定します。

  1. type: モデルが mlflow_modelcustom_model、または triton_model のどれか。
  2. データが置かれている場所の path。「サポートされているパス」セクションで説明されているパスのいずれかを指定できます。
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json

# Possible Paths for models:
# AzureML Datastore: azureml://datastores/<datastore-name>/paths/<path_on_datastore>
# MLflow run: runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
# Job: azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
# Model Asset: azureml:<my_model>:<version>

command: |
  ls ${{inputs.my_model}}
inputs:
  my_model:
    type: mlflow_model # List of all model types here: https://learn.microsoft.com/azure/machine-learning/reference-yaml-model#yaml-syntax
    path: ../../assets/model/mlflow-model
environment: azureml://registries/azureml/environments/sklearn-1.0/labels/latest

次に、CLI で以下を実行します

az ml job create -f <file-name>.yml

完全な例については、モデル GitHub レポジトリを参照してください。

ジョブの出力としてモデルを使用する

ジョブでは、出力 を使用してクラウドベースのストレージにモデルを書き込むことができます。

ジョブ仕様 YAML ファイル (<file-name>.yml) を作成し、outputs セクションにデータの書き込み先の型とパスを入力します。

$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json

# Possible Paths for Model:
# Local path: mlflow-model/model.pkl
# AzureML Datastore: azureml://datastores/<datastore-name>/paths/<path_on_datastore>
# MLflow run: runs:/<run-id>/<path-to-model-relative-to-the-root-of-the-artifact-location>
# Job: azureml://jobs/<job-name>/outputs/<output-name>/paths/<path-to-model-relative-to-the-named-output-location>
# Model Asset: azureml:<my_model>:<version>

code: src
command: >-
  python hello-model-as-output.py 
  --input_model ${{inputs.input_model}} 
  --custom_model_output ${{outputs.output_folder}}
inputs:
  input_model: 
    type: mlflow_model # mlflow_model,custom_model, triton_model
    path: ../../assets/model/mlflow-model
outputs:
  output_folder: 
    type: custom_model # mlflow_model,custom_model, triton_model
environment: azureml://registries/azureml/environments/sklearn-1.0/labels/latest

次に、CLI を使用してジョブを作成します。

az ml job create --file <file-name>.yml

完全な例については、モデル GitHub レポジトリを参照してください。

次の手順