본문 바로가기

AI/MLflow

[MLflow] MLflow Models

728x90
반응형

 

 

mlflow로 모델을 저장하는 방법은 2가지가 있다.

  • mlflow.모델 프레임워크.save_model()
    • ex. mlflow.sklearn.save_model(model,"my_model")
    • 모델을 지정된 경로에 저장
    • 모델, 데이터, 파일, 다른 리소스를 포함하는 MLflow의 저장 단위

 

 

  • mlflow.모델 프레임워크.log_model()
    • ex. mlflow.sklearn.log_model(model,"my_model")
    • MLflow의 모델 레지스트리에 로깅
    • 모델을 로깅, 모델 버전을 관리, 모델 메타데이터를 추적
    • 모델 레지스트리는 다양한 모델 버전을 관리하고 추적할 수 있는 중앙 집중식 저장소

 

두 함수 모두 공통적으로 "MLmodel"이라는 파일이 나오는데 모델에 대한 메타 데이터가 담긴 파일이다.

artifact_path: Iris_Classifier_DNN
flavors:
  python_function:
    data: data
    env:
      conda: conda.yaml
      virtualenv: python_env.yaml
    loader_module: mlflow.tensorflow
    python_version: 3.9.0
  tensorflow:
    code: null
    data: data
    keras_version: 2.13.0
    model_type: keras
    save_format: tf
mlflow_version: 2.10.0
model_size_bytes: 120142
model_uuid: 3ddeb6c12df84eb69f6a1e02aab450c0
run_id: 0d03c2ffb3ef466985f87d386ddc19d0
saved_input_example_info:
  artifact_path: input_example.json
  format: tf-serving
  type: ndarray
signature:
  inputs: '[{"type": "tensor", "tensor-spec": {"dtype": "float64", "shape": [-1, 4]}}]'
  outputs: '[{"type": "tensor", "tensor-spec": {"dtype": "float32", "shape": [-1,
    3]}}]'
  params: null
utc_time_created: '2024-02-27 12:08:49.089119'

 

 

Signature

infer_signature를 사용해서 모델의 입출력 정보를 전달할 수 있다.

from mlflow.models.signature import infer_signature

signature = infer_signature(train_x, model.predict(val_x))

mlflow.keras.log_model(
            model, "Iris_Classifier_DNN", signature=signature)

 

직접 signature를 만들어 전달 할 수도 있다.

def make_signature() -> ModelSignature:
    input_schema = Schema([
        TensorSpec(np.dtype(
            'float64'), [-1, 4], "sepal length (cm),sepal width (cm),petal length (cm),petal width (cm)"),
    ])
    output_schema = Schema([
        TensorSpec(np.dtype('float32'), (-1, 3), "output")])
    return ModelSignature(inputs=input_schema, outputs=output_schema)
    
mlflow.keras.log_model(model, "Iris_Classifier_DNN", signature=make_signature())

# 텐서가 아닐 경우
# input_schema = Schema([
#       ColSpec("double", "sepal length (cm)"),
#       ColSpec("double", "sepal width (cm)"),
#       ColSpec("double", "petal length (cm)"),
#       ColSpec("double", "petal width (cm)"),
#   ])
#   output_schema = Schema([ColSpec("long")])
# signature = ModelSignature(inputs=input_schema, outputs=output_schema)

 

이렇게 signature를 추가하면 MLmodel 파일에는 signature 정보가 추가된다.

 

 

입출력 예시 추가

입출력 예시를 추가하는 방법은 log_model(), save_model()input_example 인자 값에 값을 전달하면 된다.

# 또는 직접 example 데이터를 정의해서 넣을 수 있음, Dataframe 등...
mlflow.keras.log_model(
            model, "Iris_Classifier_DNN", input_example=train_x, signature=make_signature())

 

코드를 실행하면 MLmodel이 있는 디렉토리에 input_example.json 파일이 생성된다.

 

728x90
반응형

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

[MLflow] Autologging  (0) 2024.02.28
[MLflow] MLflow Model Registry  (0) 2024.02.28
[MLflow] MLflow Project  (0) 2024.02.27
[MLflow] MLflow Tracking  (0) 2024.02.27
[MLflow] 개요  (0) 2024.02.27