การ deploy โมเดล

MLflow เบื้องต้น

Weston Bassler

Senior MLOps Engineer

วงจร ML

ขั้นตอนของวงจร ML

1 datacamp.com
MLflow เบื้องต้น

เวอร์ชันและสถานะของโมเดล

  • เวอร์ชันโมเดล

    • Version +1
  • สถานะโมเดล

    • Staging
    • Production
    • Archived

เวอร์ชันและสถานะของโมเดล

MLflow เบื้องต้น

วิธี deploy โมเดล

โหลดโมเดล

# MLflow flavor
mlflow.FLAVOR.load_model()

เสิร์ฟโมเดล

# MLflow serve command-line
mlflow models serve
MLflow เบื้องต้น

Models URI

รูปแบบ

models:/

เวอร์ชันโมเดล

models:/model_name/version

สถานะโมเดล

models:/model_name/stage
MLflow เบื้องต้น

โหลดโมเดล

# Import flavor
import mlflow.FLAVOR

# Load version mlflow.FLAVOR.load_model("models:/model_name/version")
# Load stage mlflow.FLAVOR.load_model("models:/model_name/stage")
MLflow เบื้องต้น

ตัวอย่างการโหลดโมเดล

# Import flavor
import mlflow.sklearn


# Load Unicorn model in Staging model = mlflow.sklearn.load_model("models:/Unicorn/Staging")
# Print model model
LogisticRegression()
# Inference
model.predict(data)
MLflow เบื้องต้น

การเสิร์ฟโมเดล

# Serve Unicorn model in Production stage
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 endpoint

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 เบื้องต้น

การพยากรณ์ด้วยโมเดล

# Send payload to invocations endpoint
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 เบื้องต้น

มาฝึกกันเถอะ!

MLflow เบื้องต้น

Preparing Video For Download...