mlflow.artifacts

用于在 MLflow 中与工件交互的 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) str[source]

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

Parameters
  • artifact_uri

    指向工件的 URI。支持的格式包括:

    • runs:// 示例: runs:/500cf58bee2b40a4a82861cc31a617b1/my_model.pkl

    • models:// 示例: models:/my_model/Production

    • models:///path/to/model 示例: models:/my_model/2/path/to/model

    • models:/@/path/to/model 示例: models:/my_model@staging/path/to/model

    • 云存储 URI: s3:///gs:///

    • 跟踪服务器工件 URI: http:///mlartifactsmlflow-artifacts:///mlartifacts

    必须且只能指定以下之一:artifact_urirun_id

  • run_id – 包含这些工件的 MLflow Run 的 ID。必须指定 run_idartifact_uri 中的恰好一个。

  • artifact_path – (与 run_id 一起使用) 如果指定,为相对于 MLflow Run 的根目录的路径,包含要下载的工件。

  • dst_path – 本地文件系统中用于下载指定工件的目标目录路径。如果该目录不存在,则会创建。如果未指定,则这些工件会下载到本地文件系统中新建的具有唯一名称的目录,除非这些工件已存在于本地文件系统中,在这种情况下会直接返回它们的本地路径。

  • tracking_uri – 在下载工件时要使用的 tracking URI。

Returns

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

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 处的工件。

Parameters
  • artifact_uri – 指向工件的 URI,例如 "runs:/500cf58bee2b40a4a82861cc31a617b1/my_model.pkl", "models:/my_model/Production", 或 "s3://my_bucket/my/file.txt"。 必须且只能指定 artifact_urirun_id 之一。

  • run_id – 包含这些工件的 MLflow Run 的 ID。必须且只能指定 run_idartifact_uri 中的一个。

  • artifact_path – (用于 run_id) 如果指定,表示相对于 MLflow Run 的根目录的路径,包含要列出的工件。

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

Returns

以 FileInfo 形式列出的、直接位于 path 下的工件列表。

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

将工件内容加载为字典。

Parameters

artifact_uri – artifact 存储位置。

Returns

一个字典。

Example
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]

将工件内容作为 PIL.Image.Image 对象加载

Parameters

artifact_uri – 工件位置。

Returns

一个 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]

将工件内容作为字符串加载。

Parameters

artifact_uri – Artifact 存放位置。

Returns

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