이번에는Azure ML[1] Azure Machine Learning에서 머신러닝 모델 학습하기 에서 등록한 모델을 online endpoint로 배포해본다.
Azure ML[1] Azure Machine Learning에서 머신러닝 모델 학습하기 에서처럼 workspace를 다룰 수 있는 MLClient객체를 생성합니다.
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
# authenticate
credential = DefaultAzureCredential()
# Azure 리소스 그룹 생성
# Azure Machine Learning Service 워크스페이스 생성
# Get a handle to the workspace
ml_client = MLClient(
credential=credenial,
subscription_id="b9#8##4-2###-4a##-####-7##e6##5a###", # subscription_id - 구독id는 Azure Portal -> Subscription 항목에서 확인 가능
resource_group_name="resource group name",
workspace_name="workspace name",
)
Azure ML[1] Azure Machine Learning에서 머신러닝 모델 학습하기 을 모두 완료하였다면 이미 workspace에 모델이 등록되어 있다.
그렇지 않은 경우 다음과 같은 코드 등록시켜준다.
from azure.ai.ml.entities import Model
from azure.ai.ml.constants import AssetTypes
mlflow_model = Model(
path="./deploy/credit_defaults_model/", # path to the model files, if you've stored them locally.
type=AssetTypes.MLFLOW_MODEL,
# Asset types are used to identify the type of an asset.
# An asset can be a file, folder, mlflow model, triton model, mltable or custom model.
name="credit_defaults_model",
description="MLflow Model created from local files.",
)
# Register the model
ml_client.models.create_or_update(mlflow_model)
compute나 environment를 등록할 때 처럼 Model 객체를 만든 후 MLClient.models.create_or_update() 메소드로 등록해줍니다.
AML studio의 workspace - Models에서 확인할 수 있습니다.

코드로는 다음과 같이 확인 할 수 있습니다.
registered_model_name = "credit_defaults_model"
# 이 코드로 등록되어있는 모델들의 리스트에서 가장 최신의 버전을 알아낸다
latest_model_version = max(
[int(m.version) for m in ml_client.models.list(name=registered_model_name)]
)
print(latest_model_version) # 최신버전
모델의 학습이 끝나면 배포를 해서 사람들이 inference에 사용할 수 있도록 해야겠죠, AML은 이를 위해 endpoints를 생성하고 그것에 deployment를 추가할 수 있게 도와줍니다.
다음 그림을 보면서 이해해봅시다.

https://learn.microsoft.com/en-us/azure/machine-learning/concept-endpoints?view=azureml-api-2
주어진 사진을 보고 차의 색과 종류를 예측하는 application이라고 합시다. 특정 credentials를 갖는 사용자가 URL에 HTTP request를 생성합니다. 그리고 request의 일부로 사진을 제공합니다. 그러면 사용자는 그에 대한 응답으로 차의 종류와 색 정보를 받게됩니다. 여기서 URL이 endpoint로서 작동하게 됩니다.