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 형식이 할당됩니다.
az account set --subscription <subscription>
az configure --defaults workspace=<workspace> group=<resource-group> location=<location>
작업 영역은 Azure Machine Learning의 최상위 리소스로, Azure Machine Learning을 사용할 때 만든 모든 아티팩트를 사용할 수 있는 중앙 집중식 환경을 제공합니다. 이 섹션에서는 배포 작업을 수행할 작업 영역에 연결합니다.
필요한 라이브러리 가져오기:
from azure.ai.ml import MLClient, Input
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes
from azure.identity import DefaultAzureCredential
$schema: https://azuremlschemas.azureedge.net/latest/model.schema.json
name: local-file-example
path: mlflow-model/model.pkl
description: Model created from local file.
여기에는 두 가지 옵션이 있습니다. MLflow 실행 URI 형식을 사용하거나 azureml job URI 형식을 사용할 수 있습니다.
MLflow
이 옵션은 MLflow 실행 URI 형식에 이미 익숙한 MLflow 사용자에게 가장 적합합니다. 이 옵션을 사용하면 기본 아티팩트 위치(모든 MLflow 로깅 모델 및 아티팩트가 있는 위치)의 아티팩트에서 모델을 만들 수 있습니다. 이렇게 하면 등록된 모델과 모델이 생성된 실행 간에 계보가 설정됩니다.
az ml model create --name my-model --version 1 --path runs:/<run-id>/model/ --type mlflow_model
azureml job
이 옵션은 아티팩트의 모델을 작업의 출력에 등록하는 데 도움이 되는 azureml job 참조 URI 형식입니다. 이 형식은 기존 azureml 데이터 저장소 참조 URI 형식에 맞춰지고 (기본 아티팩트 위치뿐 아니라) 작업의 명명된 출력에서 아티팩트 참조를 지원합니다. 또한 MLflow를 사용하여 학습 스크립트 내에서 모델을 직접 등록하지 않은 경우 등록된 모델과 학습된 작업 간에 계보를 설정할 수 있습니다.
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)
지원되는 다음 URI 형식 중 하나를 사용하여 클라우드 경로에서 모델을 만들 수 있습니다.
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes
cloud_model = Model(
path=file_model.path,
# The above line basically provides a path in the format "azureml://subscriptions/XXXXXXXXXXXXXXXX/resourceGroups/XXXXXXXXXXX/workspaces/XXXXXXXXXXX/datastores/workspaceblobstore/paths/model.pkl"
# Users could also use,"azureml://datastores/workspaceblobstore/paths/model.pkl" as a shorthand to the same location
name="cloud-path-example",
type=AssetTypes.CUSTOM_MODEL,
description="Model created from cloud path.",
)
ml_client.models.create_or_update(cloud_model)
예제에서는 azureml://datastores/${{datastore-name}}/paths/${{path_on_datastore}} 구문을 사용하여 datastore에서의 경로를 가리키는 약식 azureml 스키마를 사용합니다.
여기에는 두 가지 옵션이 있습니다. MLflow 실행 URI 형식을 사용하거나 azureml job URI 형식을 사용할 수 있습니다.
MLflow
이 옵션은 MLflow 실행 URI 형식에 이미 익숙한 MLflow 사용자에게 가장 적합합니다. 이 옵션을 사용하면 기본 아티팩트 위치(모든 MLflow 로깅 모델 및 아티팩트가 있는 위치)의 아티팩트에서 모델을 만들 수 있습니다. 이렇게 하면 등록된 모델과 모델이 생성된 실행 간에 계보가 설정됩니다.
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import ModelType
run_model = Model(
path="runs:/<run-id>/model/"
name="run-model-example",
description="Model created from run.",
type=ModelType.MLFLOW
)
ml_client.models.create_or_update(run_model)
azureml job
이 옵션은 아티팩트의 모델을 작업의 출력에 등록하는 데 도움이 되는 azureml job 참조 URI 형식입니다. 이 형식은 기존 azureml 데이터 저장소 참조 URI 형식에 맞춰지고 (기본 아티팩트 위치뿐 아니라) 작업의 명명된 출력에서 아티팩트 참조를 지원합니다. 또한 MLflow를 사용하여 학습 스크립트 내에서 모델을 직접 등록하지 않은 경우 등록된 모델과 학습된 작업 간에 계보를 설정할 수 있습니다.
type: 모델이 mlflow_model, custom_model 또는 triton_model 중 어느 것인지를 나타냅니다.
path - 데이터가 있는 위치입니다. 경로는 지원되는 경로 섹션에 설명된 경로 중 하나일 수 있습니다.
from azure.ai.ml import command
from azure.ai.ml.entities import Model
from azure.ai.ml import Input
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml import MLClient
# Possible Asset Types for Data:
# AssetTypes.MLFLOW_MODEL
# AssetTypes.CUSTOM_MODEL
# AssetTypes.TRITON_MODEL
# Possible Paths for Model:
# Local path: mlflow-model/model.pkl
# Azure Machine Learning 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>
my_job_inputs = {
"input_model": Input(type=AssetTypes.MLFLOW_MODEL, path="mlflowmodel")
}
job = command(
code="./src", # local path where the code is stored
command="ls ${{inputs.input_model}}",
inputs=my_job_inputs,
environment="AzureML-sklearn-0.24-ubuntu18.04-py37-cpu:9",
compute="cpu-cluster",
)
# submit the command
returned_job = ml_client.jobs.create_or_update(job)
# get a URL for the status of the job
returned_job.services["Studio"].endpoint
from azure.ai.ml import command
from azure.ai.ml.entities import Model
from azure.ai.ml import Input, Output
from azure.ai.ml.constants import AssetTypes
# Possible Asset Types for Model:
# AssetTypes.MLFLOW_MODEL
# AssetTypes.CUSTOM_MODEL
# AssetTypes.TRITON_MODEL
# Possible Paths for Model:
# Local path: mlflow-model/model.pkl
# Azure Machine Learning 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>
my_job_inputs = {
"input_model": Input(type=AssetTypes.MLFLOW_MODEL, path="mlflow-model"),
"input_data": Input(type=AssetTypes.URI_FILE, path="./mlflow-model/input_example.json"),
}
my_job_outputs = {
"output_folder": Output(type=AssetTypes.CUSTOM_MODEL)
}
job = command(
code="./src", # local path where the code is stored
command="python load_write_model.py --input_model ${{inputs.input_model}} --output_folder ${{outputs.output_folder}}",
inputs=my_job_inputs,
outputs=my_job_outputs,
environment="AzureML-sklearn-0.24-ubuntu18.04-py37-cpu:9",
compute="cpu-cluster",
)
# submit the command
returned_job = ml_client.create_or_update(job)
# get a URL for the status of the job
returned_job.services["Studio"].endpoint