モデルのデプロイ

MLflow 入門

Weston Bassler

Senior MLOps Engineer

ML ライフサイクル

ML ライフサイクルのステップ

1 datacamp.com
MLflow 入門

モデルのバージョンとステージ

  • モデルのバージョン

    • バージョン +1
  • モデルのステージ

    • Staging
    • Production
    • Archived

モデルのバージョンとステージ

MLflow 入門

モデルのデプロイ方法

モデルを読み込む

# MLflow フレーバー
mlflow.FLAVOR.load_model()

モデルを提供

# MLflow の serve コマンド
mlflow models serve
MLflow 入門

Models URI

規約

models:/

モデルのバージョン

models:/model_name/version

モデルのステージ

models:/model_name/stage
MLflow 入門

モデルの読み込み

# フレーバーをインポート
import mlflow.FLAVOR

# バージョンを読み込む mlflow.FLAVOR.load_model("models:/model_name/version")
# ステージを読み込む mlflow.FLAVOR.load_model("models:/model_name/stage")
MLflow 入門

読み込み例

# フレーバーをインポート
import mlflow.sklearn


# Staging の Unicorn モデルを読み込み model = mlflow.sklearn.load_model("models:/Unicorn/Staging")
# モデルを表示 model
LogisticRegression()
# 推論
model.predict(data)
MLflow 入門

モデルの提供(Serving)

# Production ステージの Unicorn モデルを提供
mlflow models serve -m "models:/Unicorn/Production"
2023/03/26 15:07:00 INFO mlflow.models.flavor_backend_registry: 
Selected backend for flavor 'python_function'
2023/03/26 15:07:00 INFO mlflow.pyfunc.backend: === Running command 'exec gunicorn 
--timeout=60 -b 127.0.0.1:5000 -w 1 ${GUNICORN_CMD_ARGS} -- 
mlflow.pyfunc.scoring_server.wsgi:app'
[2023-03-26 15:07:00 -0400] [86409] [INFO] Starting gunicorn 20.1.0
[2023-03-26 15:07:00 -0400] [86409] [INFO] Listening at: http://127.0.0.1:5000
[2023-03-26 15:07:00 -0400] [86409] [INFO] Using worker: sync
[2023-03-26 15:07:00 -0400] [86410] [INFO] Booting worker with pid: 86410
MLflow 入門

Invocations エンドポイント

API

http://localhost:5000/invocations

  • フォーマット
    • CSV
    • JSON
1 Flaticon.com
MLflow 入門

【CSV 形式】

pandas_df.to_csv()

【JSON 形式】

{
  "dataframe_split": {
      "columns": ["R&D Spend", "Administration", "Marketing Spend", "State"],
      "data": [["165349.20", 136897.80, 471784.10, 1]]
  }
}
MLflow 入門

モデル予測

# ペイロードを invocations に送信
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json' -d
{
  "dataframe_split": {
      "columns": ["R&D Spend", "Administration", "Marketing Spend", "State"],
      "data": [["165349.20", 136897.80, 471784.10, 1]]
  }
}
[[104055.1842384]]
MLflow 入門

Vamos praticar!

MLflow 入門

Preparing Video For Download...