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, registry_uri: Optional[str] = None) str[source]

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

参数
  • artifact_uri

    指向工件的 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>

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

    必须指定 artifact_urirun_id 中的一个。

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

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

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

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

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

返回

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

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

参数
  • 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 – 列出工件时使用的跟踪 URI。

返回

路径下直接列出的 FileInfo 形式的工件列表。

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

将工件内容加载为字典。

参数

artifact_uri – 工件位置。

返回

一个字典。

示例
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 对象

参数

artifact_uri – 工件位置。

返回

一个 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_uri – 工件位置。

返回

工件的内容(字符串形式)。

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