メインコンテンツへスキップ
データ バージョン管理 のための W&B Artifacts の作成、更新、ダウンロード、管理を行います。

アーティファクト の作成と ログ

"""
Creates and logs a W&B artifact. First create an
artifact object. Next, add files, directories, or external references to the artifact.
Finally, log the artifact to a W&B run.
"""
import wandb

# Create an artfiact object
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# Add one or more files to the artifact
artifact.add_file(local_path="data/file1.csv")

# Recursively add a directory to the artifact
artifact.add_dir(local_path="data/directory")

# Add external URI reference to the artifact
artifact.add_reference(uri="s3://my-bucket/path/to/data.csv")

with wandb.init(project="<project>") as run:
    # Training and logging code goes here

    # Example of logging an artifact
    run.log_artifact(artifact)

クラウド ストレージ バケット パスへの参照を追加して、外部 アーティファクト を追跡する

"""
Tracks an external artifact by adding a reference to a cloud storage bucket path.
"""

import wandb

# Create an artifact object
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# Add a reference to the bucket path
artifact.add_reference(uri = "uri/to/your/bucket/path")

# Initialize a W&B run
with wandb.init(project="<project>") as run:

  # Log the artifact's metadata
  run.log_artifact(artifact)

run から特定の アーティファクト バージョン を削除する

"""
Delete specific artifact version from a W&B run. Set delete_aliaes to `True` 
if the artifact has an alias attached to it.
"""
import wandb

# Initialize W&B API
api = wandb.Api()

# Get the run by its path. Consists of <entity>/<project>/<run_path>
runs = api.run("<entity>/<project>/<run_path>")

# wandb.Api().Run.logged_artifacts() returns a list of artifact versions
# that consists of artifact name and version <artifact_name>:v<version_number>
for artifact_version in runs.logged_artifacts():
    # Index the last two characters of the artifact version name (str) that
    # consists of the version number
    if artifact_version.name[-2:] == "v"+ "<version_number>":
        artifact_version.delete(delete_aliases=True)

アーティファクト コレクションを削除する

"""
Delete an artifact collection from W&B.
"""
import wandb

# Initialize W&B API
api = wandb.Api()

# Delete an artifact collection by its name and type
# Name format: <entity>/<project>/<run_path>
collection = api.artifact_collection(
    name="<entity>/<project>/<run_path>",
    type_name="<artifact_type>"
)

collection.delete()

アーティファクト から特定のファイルまたはサブフォルダをダウンロードする

"""
Downloads specific files or sub-folders from W&B artifacts. The same
logic applies to external artifacts.
"""
import wandb

with wandb.init(project="<project>") as run:
    # Indicate the artifact to use. Format is "name:alias"
    artifact = run.use_artifact("<artifact_name>:<alias>")

    # Download a specific file or sub-folder
    artifact.download(path_prefix="<file_name>") # downloads only the specified file or folder

ファイルまたは アーティファクト 全体をダウンロードする

"""
Downloads files or entire artifacts from W&B. The same
logic applies to external artifacts.
"""

import wandb    

with wandb.init(project="<project>") as run:
    # Indicate the artifact to use. Format is "name:alias"
    artifact = run.use_artifact("<artifact_name>:<alias>")

    # Downloads file from the artifact at path name
    # If artifact.add_reference() was used, returns the reference URL
    entry = artifact.get_entry("<file_name>")

    # Download the entire artifact
    datadir = artifact.download()

アーティファクト の ログ 時に 1 つ以上の エイリアス を追加する

"""
Add one or more aliases to an artifact when logging it to W&B.
"""
import wandb

# Create an artifact
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# Add files to the artifact
artifact.add_file("<file_path>")

with wandb.init(project="<project>") as run:
    # Log the artifact with aliases
    run.log_artifact(artifact, aliases=["<alias1>", "<alias2>"])

既存の アーティファクト に エイリアス を追加する

"""
Adds an alias to an existing W&B artifact.
"""
import wandb

# Retrieve an existing artifact and add an alias to it
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.aliases = ["<new-alias>"]
artifact.save()

アーティファクト の ログ 時にタグを追加する

"""
Add a tag to an artifact when logging it to W&B.
"""
import wandb

# Create an artifact
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")

# Log the artifact with tags
with wandb.init(project="<project>") as run:
    run.log_artifact(artifact, tags=["<tag1>", "<tag2>"])

既存の アーティファクト にタグを追加する

"""
Adds a tag to an existing W&B artifact.
"""
import wandb

# Retrieve an existing artifact and add a tag to it
artifact = wandb.Api().artifact("entity/project/artifact:version")
artifact.tags = ["new-tag"]
artifact.save()

TTL ポリシーを設定して アーティファクト を作成し、ログ を記録する

"""
Create and log an artifact with a TTL policy in W&B.
"""
import wandb
from datetime import timedelta

# Create an artifact with TTL policy
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
artifact.add_file("<file_path>")
artifact.ttl = timedelta(days=30)  # Set TTL policy

with wandb.init(project="<project>", entity="<entity>") as run:
    # Log the artifact with TTL
    run.log_artifact(artifact)

既存の アーティファクト の TTL ポリシーを更新する

"""
Update the TTL policy of an existing artifact in W&B.
"""
import wandb
from datetime import timedelta

# Initialize the W&B API
api = wandb.Api()

# Retrieve the existing artifact
artifact = api.artifact("<entity/project/artifact:alias>")

# Add or update the TTL policy. Specify the desired duration.
artifact.ttl = timedelta(days=365)  # Delete in one year

# Save the updated artifact
artifact.save()

run 内で既存の アーティファクト の説明を更新する

"""
Update an existing W&B artifact's description within a W&B run.

This code initializes a W&B run, retrieves the specified artifact by name and alias,
updates its description, and saves the changes.
"""
import wandb

with wandb.init(entity="<entity>", project="<project>") as run:
    # Retrieve the artifact by name and alias
    artifact = run.use_artifact(artifact_or_name="<artifact>:<alias>")
    # Update the artifact's description
    artifact.description = "<description>"
    # Save the updated artifact
    artifact.save()

新しい run を作成せずに、既存の アーティファクト の説明、メタデータ、エイリアス を更新する

"""
Given an existing artifact, update its description, metadata, and aliases
without creating a new run.
"""
import wandb

api = wandb.Api()

artifact = api.artifact(name="<entity/project/artifact:alias>")

# Update the description
artifact.description = "My new description"

# Selectively update metadata keys
artifact.metadata["oldKey"] = "new value"

# Replace the metadata entirely
artifact.metadata = {"newKey": "new value"}

# Add an alias
artifact.aliases.append("best")

# Remove an alias
artifact.aliases.remove("latest")

# Completely replace the aliases
artifact.aliases = ["replaced"]

# Persist all artifact modifications
artifact.save()