Logging artifacts

MLflow · Essentials · 10 min

Plots, confusion matrices, feature lists and sample predictions explain a run far better than metrics alone.

Artifacts are files attached to a run and stored in the run's artifact location (a local folder, S3, GCS, Azure Blob, or proxied through a tracking server).

Ways to log them:

  • mlflow.log_artifact("report.html") uploads a single file; log_artifacts("plots/") uploads a directory. artifact_path="eval" places them in a subfolder.
  • mlflow.log_dict(d, "config.json") and mlflow.log_text(s, "notes.md") write objects directly without temporary files.
  • mlflow.log_figure(fig, "roc.png") saves a matplotlib or plotly figure.
  • mlflow.log_image(array_or_pil, key="samples", step=epoch) logs images over steps.
  • mlflow.log_table(data=df_or_dict, artifact_file="preds.json") stores tables you can view and compare in the UI.

Reading back: mlflow.artifacts.download_artifacts(run_id=..., artifact_path="config.json") returns a local path, and mlflow.artifacts.load_dict or load_text load from an artifact URI directly. MlflowClient().list_artifacts(run_id) lists files.

Keep artifacts purposeful: large datasets usually belong in a data store, with only a pointer or hash logged as a tag or via mlflow.log_input with a dataset object. Model files are logged with log_model, not as raw artifacts, so they get a signature and flavor.

Example

import json
import mlflow

with mlflow.start_run() as run:
    mlflow.log_dict({"features": ["age", "tenure"], "target": "churn"}, "config.json")
    mlflow.log_text("Baseline with two features.", "notes/summary.txt")
    mlflow.log_table(data={"id": [1, 2], "pred": [0.2, 0.9]}, artifact_file="preds.json")
    with open("threshold.json", "w") as f:
        json.dump({"threshold": 0.5}, f)
    mlflow.log_artifact("threshold.json", artifact_path="eval")

from mlflow import MlflowClient
print(sorted(a.path for a in MlflowClient().list_artifacts(run.info.run_id)))
# ['config.json', 'eval', 'notes', 'preds.json']

cfg = mlflow.artifacts.load_dict(f"runs:/{run.info.run_id}/config.json")
print(cfg["target"])    # churn

Remember

  • Artifacts are files stored with a run (local, cloud or proxied storage)
  • log_dict, log_text, log_figure, log_table avoid temp files
  • Load back with mlflow.artifacts helpers and runs:/<id>/path URIs

Watch out for

  • Logging huge raw datasets as artifacts on every run, which bloats storage; log a reference instead.

Check yourself

  1. Which call stores a matplotlib figure with a run without saving it to disk first?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: mlflow.org/docs/latest/ml/tracking/tracking-api/