アーティファクトを作成してログする
"""
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
# アーティファクト オブジェクトを作成します
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# 1 つ以上のファイルをアーティファクトに追加します
artifact.add_file(local_path="data/file1.csv")
# ディレクトリをアーティファクトに再帰的に追加します
artifact.add_dir(local_path="data/directory")
# 外部 URI 参照をアーティファクトに追加します
artifact.add_reference(uri="s3://my-bucket/path/to/data.csv")
with wandb.init(project="<project>") as run:
# ここにトレーニングとログのコードを記述します
# アーティファクトをログする例
run.log_artifact(artifact)
クラウドストレージバケットのパスへの参照を追加して外部アーティファクトをトラッキングする
"""
Tracks an external artifact by adding a reference to a cloud storage bucket path.
"""
import wandb
# アーティファクト オブジェクトを作成します
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# バケットパスへの参照を追加します
artifact.add_reference(uri = "uri/to/your/bucket/path")
# W&B run を初期化します
with wandb.init(project="<project>") as run:
# アーティファクトのメタデータをログします
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
# W&B API を初期化します
api = wandb.Api()
# パスを指定して run を取得します。パスの形式は <entity>/<project>/<run_path> です
runs = api.run("<entity>/<project>/<run_path>")
# wandb.Api().Run.logged_artifacts() は、アーティファクト名とバージョンを
# <artifact_name>:v<version_number> の形式で表すアーティファクトのバージョンのリストを返します
for artifact_version in runs.logged_artifacts():
# アーティファクトのバージョン名 (str) の末尾 2 文字を指定して、
# バージョン番号を確認します
if artifact_version.name[-2:] == "v"+ "<version_number>":
artifact_version.delete(delete_aliases=True)
アーティファクト コレクションを削除する
"""
Delete an artifact collection from W&B.
"""
import wandb
# W&B APIを初期化します
api = wandb.Api()
# 名とタイプを指定してアーティファクト コレクションを削除します
# 名の形式: <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:
# 使用するアーティファクトを指定します。形式は "name:alias" です
artifact = run.use_artifact("<artifact_name>:<alias>")
# 特定のファイルまたはサブフォルダーをダウンロードします
artifact.download(path_prefix="<file_name>") # 指定したファイルまたはフォルダーのみをダウンロードします
ファイルまたはアーティファクト全体をダウンロードする
"""
Downloads files or entire artifacts from W&B. The same
logic applies to external artifacts.
"""
import wandb
with wandb.init(project="<project>") as run:
# 使用するアーティファクトを指定します。Format is "name:alias"
artifact = run.use_artifact("<artifact_name>:<alias>")
# 指定したパス名のファイルをアーティファクトからダウンロードします
# artifact.add_reference() を使用した場合は、参照 URL が返されます
entry = artifact.get_entry("<file_name>")
# アーティファクト全体をダウンロードします
datadir = artifact.download()
アーティファクトをログする際に 1 つ以上のエイリアスを追加する
"""
Add one or more aliases to an artifact when logging it to W&B.
"""
import wandb
# アーティファクトを作成します
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# アーティファクトにファイルを追加します
artifact.add_file("<file_path>")
with wandb.init(project="<project>") as run:
# エイリアスを付けてアーティファクトをログします
run.log_artifact(artifact, aliases=["<alias1>", "<alias2>"])
既存のアーティファクトにエイリアスを追加する
"""
Adds an alias to an existing W&B artifact.
"""
import wandb
# 既存のアーティファクトを取得し、エイリアスを追加します
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
# アーティファクトを作成します
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
# タグを付けてアーティファクトをログします
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
# 既存のアーティファクトを取得し、タグを追加します
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
# TTL ポリシーを使用してアーティファクトを作成します
artifact = wandb.Artifact(name="<artifact_name>", type="<artifact_type>")
artifact.add_file("<file_path>")
artifact.ttl = timedelta(days=30) # TTL ポリシーを設定します
with wandb.init(project="<project>", entity="<entity>") as run:
# TTL を使用してアーティファクトをログします
run.log_artifact(artifact)
既存のアーティファクトの TTL ポリシーを更新する
"""
Update the TTL policy of an existing artifact in W&B.
"""
import wandb
from datetime import timedelta
# W&B API を初期化します
api = wandb.Api()
# 既存のアーティファクトを取得します
artifact = api.artifact("<entity/project/artifact:alias>")
# TTL ポリシーを追加または更新します。任意の保持期間を指定してください。
artifact.ttl = timedelta(days=365) # 1 年後に削除されます
# 更新したアーティファクトを保存します
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:
# 名前とエイリアスでアーティファクトを取得
artifact = run.use_artifact(artifact_or_name="<artifact>:<alias>")
# アーティファクトの説明を更新
artifact.description = "<description>"
# 更新したアーティファクトを保存
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>")
# 説明を更新します
artifact.description = "My new description"
# メタデータのキーを選択的に更新します
artifact.metadata["oldKey"] = "new value"
# メタデータ全体を置き換えます
artifact.metadata = {"newKey": "new value"}
# エイリアスを追加します
artifact.aliases.append("best")
# エイリアスを削除します
artifact.aliases.remove("latest")
# エイリアスをすべて置き換えます
artifact.aliases = ["replaced"]
# アーティファクトへのすべての変更を保存します
artifact.save()