メインコンテンツへスキップ
単一の run から、または分散 run と連携して、新しい artifact バージョンを作成できます。必要に応じて、段階的 artifact と呼ばれる既存のバージョンから新しい artifact バージョンを作成することもできます。
artifact 内の一部のファイルに変更を適用する必要があり、元の artifact のサイズが大幅に大きい場合は、段階的 artifact を作成することをおすすめします。

新しいartifactバージョンを最初から作成する

新しいartifactバージョンを作成する方法は 2 つあります。単一の run から作成する方法と、分散 run から作成する方法です。それぞれ次のとおりです。
  • Single run: 1 つの run が、新しいバージョンに必要なデータをすべて提供します。これは最も一般的なケースで、run が必要なデータを完全に再現する場合に最適です。たとえば、保存済みモデルを出力したり、分析用にモデルの予測を表として出力したりする場合です。
  • Distributed runs: 複数の run の集合が、まとめて新しいバージョンに必要なデータをすべて提供します。これは、複数の run が多くの場合並列でデータを生成する分散ジョブに最適です。たとえば、分散方式でモデルを評価し、予測を出力する場合です。
wandb.Artifact API にプロジェクト内に存在しない名を渡すと、W&B は新しいartifactを作成し、v0 alias を割り当てます。同じartifactに対して再度ログすると、W&B は内容のチェックサムを計算します。artifactが変更されていた場合、W&B は新しいバージョン v1 を保存します。 wandb.Artifact API に、プロジェクト内の既存のartifactに一致する名と artifact type を渡すと、W&B は既存のartifactを取得します。取得されたartifactのバージョンは 1 より大きくなります。
artifactワークフローの比較

単一の run

アーティファクト 内のすべてのファイルを生成する 1 つの run で、Artifact の新しいバージョンをログします。これは、1 つの run が アーティファクト 内のすべてのファイルを生成する場合に該当します。 ユースケースに応じて、以下のタブから 1 つ選択し、run の内側または外側で新しい アーティファクト バージョンを作成します。
W&B run 内で アーティファクト バージョンを作成します。
  1. wandb.init() で run を作成します。
  2. wandb.Artifact を使用して、新しい アーティファクト を作成するか既存の アーティファクト を取得します。
  3. .add_file で アーティファクト にファイルを追加します。
  4. .log_artifact で アーティファクト を run にログします。
with wandb.init() as run:
    artifact = wandb.Artifact("artifact_name", "artifact_type")

    # `.add`、`.add_file`、`.add_dir`、`.add_reference` を使用して
    # artifact にファイルとアセットを追加
    artifact.add_file("image1.png")
    run.log_artifact(artifact)

分散 run

複数の run が、コミット前に同じバージョンに対して共同で作業できるようにします。これは、前述の 単一の run モード (1 つの run が新しいバージョンのすべてのデータを提供する方式) とは対照的です。
  1. collection 内の各 run は、同じバージョンで共同作業するために、同じ一意の ID (distributed_id) を認識している必要があります。デフォルトでは、存在する場合、W&B は wandb.init(group=GROUP) で設定した run の groupdistributed_id として使用します。
  2. バージョンを「コミット」し、その状態を永続的に固定する最後の run が必要です。
  3. 共同の artifact への追加には upsert_artifact を使用し、コミットを確定するには finish_artifact を使用します。
次の例を見てみましょう。異なる run (以下では Run 1Run 2Run 3 と表記) が、upsert_artifact を使って同じ artifact にそれぞれ異なる画像ファイルを追加します。

Run 1

with wandb.init() as run:
    artifact = wandb.Artifact("artifact_name", "artifact_type")
    # `.add`、`.add_file`、`.add_dir`、`.add_reference` を使用して
    # アーティファクト にファイルとアセットを追加
    artifact.add_file("image1.png")
    run.upsert_artifact(artifact, distributed_id="my_dist_artifact")

Run 2

with wandb.init() as run:
    artifact = wandb.Artifact("artifact_name", "artifact_type")
    # `.add`、`.add_file`、`.add_dir`、`.add_reference` を使用して
    # アーティファクト にファイルとアセットを追加
    artifact.add_file("image2.png")
    run.upsert_artifact(artifact, distributed_id="my_dist_artifact")

Run 3

Run 1 と Run 2 が完了した後に実行する必要があります。wandb.Run.finish_artifact() を呼び出す run では、アーティファクト にファイルを含めることもできますが、必須ではありません。
with wandb.init() as run:
    artifact = wandb.Artifact("artifact_name", "artifact_type")
    # `.add`、`.add_file`、`.add_dir`、`.add_reference` を使用して
    # artifact にファイルとアセットを追加
    artifact.add_file("image3.png")
    run.finish_artifact(artifact, distributed_id="my_dist_artifact")

Create a new artifact version from an existing version

Add, modify, or remove a subset of files from a previous artifact version without the need to re-index the files that didn’t change. Adding, modifying, or removing a subset of files from a previous artifact version creates a new artifact version known as an incremental artifact.
Incremental artifact versioning
Here are some scenarios for each type of incremental change you might encounter:
  • add: you periodically add a new subset of files to a dataset after collecting a new batch.
  • remove: you discovered several duplicate files and want to remove them from your artifact.
  • update: you corrected annotations for a subset of files and want to replace the old files with the correct ones.
You could create an artifact from scratch to perform the same function as an incremental artifact. However, when you create an artifact from scratch, you will need to have all the contents of your artifact on your local disk. When making an incremental change, you can add, remove, or modify a single file without changing the files from a previous artifact version.
You can create an incremental artifact within a single run or with a set of runs (distributed mode).
Follow the procedure below to incrementally change an artifact:
  1. Obtain the artifact version you want to perform an incremental change on:
saved_artifact = run.use_artifact("my_artifact:latest")
  1. Create a draft with:
draft_artifact = saved_artifact.new_draft()
  1. Perform any incremental changes you want to see in the next version. You can either add, remove, or modify an existing entry.
Select one of the tabs for an example on how to perform each of these changes:
Add a file to an existing artifact version with the add_file method:
draft_artifact.add_file("file_to_add.txt")
You can also add multiple files by adding a directory with the add_dir method.
  1. Lastly, log or save your changes. The following tabs show you how to save your changes inside and outside of a W&B run. Select the tab that is appropriate for your use case:
run.log_artifact(draft_artifact)
Putting it all together, the code examples above look like:
with wandb.init(job_type="modify dataset") as run:
    saved_artifact = run.use_artifact(
        "my_artifact:latest"
    )  # アーティファクトを取得して run に入力する
    draft_artifact = saved_artifact.new_draft()  # ドラフトバージョンを作成する

    # ドラフトバージョン内のファイルの一部を変更する
    draft_artifact.add_file("file_to_add.txt")
    draft_artifact.remove("dir_to_remove/")
    run.log_artifact(
        draft_artifact
    )  # 変更をログして新しいバージョンを作成し、run の出力としてマークする