mlflow.artifacts

MLflow 中用于与 artifact 交互的 API

mlflow.artifacts.download_artifacts(artifact_uri: Optional[str] = None, run_id: Optional[str] = None, artifact_path: Optional[str] = None, dst_path: Optional[str] = None, tracking_uri: Optional[str] = None, registry_uri: Optional[str] = None) str[source]

将 artifact 文件或目录下载到本地目录。

参数
  • artifact_uri

    指向 artifact 的 URI。支持的格式包括

    • runs:/<run_id>/<artifact_path> 示例: runs:/500cf58bee2b40a4a82861cc31a617b1/my_model.pkl

    • models:/<model_name>/<stage> 示例: models:/my_model/Production

    • models:/<model_name>/<version>/path/to/model 示例: models:/my_model/2/path/to/model

    • models:/<model_name>@<alias>/path/to/model 示例: models:/my_model@staging/path/to/model

    • 云存储 URI: s3://<bucket>/<path>gs://<bucket>/<path>

    • 跟踪服务器 artifact URI: http://<host>/mlartifactsmlflow-artifacts://<host>/mlartifacts

    必须指定 artifact_urirun_id 中的一个。

  • run_id – 包含 artifacts 的 MLflow Run 的 ID。必须指定 run_idartifact_uri 中的一个。

  • artifact_path – (与 run_id 结合使用) 如果指定,则为相对于 MLflow Run 根目录的路径,其中包含要下载的 artifacts。

  • dst_path – 要将指定 artifacts 下载到的本地文件系统目标目录的路径。如果目录不存在,则会创建它。如果未指定,则 artifacts 会下载到本地文件系统上一个新命名的目录中,除非 artifacts 已存在于本地文件系统中,在这种情况下将直接返回其本地路径。

  • tracking_uri – 下载 artifacts 时使用的跟踪 URI。

  • registry_uri – 下载 artifacts 时使用的注册表 URI。

返回

本地文件系统上的 artifact 文件或目录的位置。

mlflow.artifacts.list_artifacts(artifact_uri: Optional[str] = None, run_id: Optional[str] = None, artifact_path: Optional[str] = None, tracking_uri: Optional[str] = None) list[FileInfo][source]

列出指定 URI 下的 artifacts。

参数
  • artifact_uri – 指向 artifacts 的 URI,例如 "runs:/500cf58bee2b40a4a82861cc31a617b1/my_model.pkl""models:/my_model/Production""s3://my_bucket/my/file.txt"。必须指定 artifact_urirun_id 中的一个。

  • run_id – 包含 artifacts 的 MLflow Run 的 ID。必须指定 run_idartifact_uri 中的一个。

  • artifact_path – (与 run_id 结合使用) 如果指定,则为相对于 MLflow Run 根目录的路径,其中包含要列出的 artifacts。

  • tracking_uri – 列出 artifacts 时使用的跟踪 URI。

返回

直接在路径下列出的 FileInfo 形式的 artifacts 列表。

mlflow.artifacts.load_dict(artifact_uri: str) dict[str, typing.Any][source]

将 artifact 内容加载为字典。

参数

artifact_uri – artifact 位置。

返回

一个字典。

示例
import mlflow

with mlflow.start_run() as run:
    artifact_uri = run.info.artifact_uri
    mlflow.log_dict({"mlflow-version": "0.28", "n_cores": "10"}, "config.json")
    config_json = mlflow.artifacts.load_dict(artifact_uri + "/config.json")
    print(config_json)
Output
{'mlflow-version': '0.28', 'n_cores': '10'}
mlflow.artifacts.load_image(artifact_uri: str)[source]

将 artifact 内容加载为 PIL.Image.Image 对象

参数

artifact_uri – Artifact 位置。

返回

一个 PIL.Image 对象。

Example
import mlflow
from PIL import Image

with mlflow.start_run() as run:
    image = Image.new("RGB", (100, 100))
    artifact_uri = run.info.artifact_uri
    mlflow.log_image(image, "image.png")
    image = mlflow.artifacts.load_image(artifact_uri + "/image.png")
    print(image)
Output
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=100x100 at 0x11D2FA3D0>
mlflow.artifacts.load_text(artifact_uri: str) str[source]

将 artifact 内容加载为字符串。

参数

artifact_uri – Artifact 位置。

返回

artifact 内容(字符串形式)。

Example
import mlflow

with mlflow.start_run() as run:
    artifact_uri = run.info.artifact_uri
    mlflow.log_text("This is a sentence", "file.txt")
    file_content = mlflow.artifacts.load_text(artifact_uri + "/file.txt")
    print(file_content)
Output
This is a sentence