본문 바로가기

AI/MLflow

[MLflow] MLflow Model Registry

728x90
반응형

 

 

Model Registry는 MLflow 프로젝트 실행을 통해 나온 결과물인 모델을 저장하는 중앙 집중식 모델 저장 장소다.

MLflow로 모델을 기록했다면, 기록한 모델을 Model Registry에 등록할 수 있고, 등록한 모델은 어디서든 불러올 수 있다.

 

등록 방법은 2가지가 있다.

웹 UI로 등록하기

웹 UI로 간단하게 모델을 등록할 수 있다.

웹 서버에서 실행했던 run을 들어간다.

 

Artifacts 블록에서 Register model 버튼을 클릭하여 모델 이름을 입력해서 모델을 등록한다.

 

 

코드로 등록하기

1. log_model()registered_model_name에 값을 입력하면 된다.

mlflow.keras.log_model(model, "Iris_Classifier_DNN",
                               input_example=train_x,
                               signature=make_signature(),
                               registered_model_name="DNN_Model")

 

 

2. mlflow.register_model()을 사용하는 것이다. 

  • model_url : run_id, artifacts 내에 model이 저장된 경로
  • name : 모델 이름

 

이 두가지 인자를 넘겨줘야한다.

result = mlflow.register_model(
    model_uri=f"runs:/{run.info.run_id}/Iris_Classifier_DNN",
    name="DNN_Model"
)

run_id 뒤에 값은 모델을 저장할 때 사용했던 이름이다.

 

 

3. MlflowClient.create_registered_model()MlflowClient.create_model_version()을 사용하는 것

  • name : 모델 이름
  • source : 모델이 저장된 경로
  • run_id : run_id
from mlflow.tracking import MlflowClient

# Model registry 생성
client = MlflowClient()
client.create_registered_model("DNN_Model2")

# 버전 등록
result = client.create_model_version(
            name="DNN_Model",
            source=mlflow.get_artifact_uri(run.info.run_id).split('artifacts')[
                0]+"artifacts/Iris_Classifier_DNN",
            run_id=f"{run.info.run_id}"
        )

 

tensorflow의 경우 -> get_artifact_uri()로 artifacts의 경로를 가져오고 artifacts를 기준후로 자른 후 앞의 경로와 artifacts/{모델 이름}을 합친다.

 

 

등록된 모델 가져오기

Model Registry에 등록된 모델은 어디서든 불러올 수 있다.

 

모델을 가져올 파이썬 파일을 생성한다.

import mlflow.pyfunc
import pandas as pd

if __name__ == "__main__":
    # 가져올 모델이름
    model_name = "DNN_Model"

    model_version = 10
    model = mlflow.pyfunc.load_model(
        model_uri=f"models:/{model_name}/{model_version}"
    )
    dataset = pd.read_csv("./dataset.csv")
    X = dataset.iloc[20:31, 0:-3]
    a = model.predict(X)
    print(a)

 

 

등록된 모델 서빙

model registry에 저장된 모델들은 바로 서빙이 가능하다.

# Tracking Server 설정
export MLFLOW_TRACKING_URI="http://localhost:5000"
# 등록된 모델 서빙하는 서버
mlflow models serve -m "models:/DNN_Model/10" --port 5001
728x90
반응형

'AI > MLflow' 카테고리의 다른 글

[MLflow] Artifact Store  (0) 2024.02.29
[MLflow] Autologging  (0) 2024.02.28
[MLflow] MLflow Models  (0) 2024.02.28
[MLflow] MLflow Project  (0) 2024.02.27
[MLflow] MLflow Tracking  (0) 2024.02.27