> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-sandboxes-integrations-placement.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> W&B でデータセットArtifactを作成、トラッキング、使用します。

# チュートリアル: データセットArtifactを作成、トラッキング、使用する

このチュートリアルでは、データセットArtifactを作成、トラッキング、使用する方法を説明します。

<div id="1-log-into-wb">
  ## 1. W\&B にログインする
</div>

W\&B ライブラリをインポートして、W\&B にログインします。まだお済みでない場合は、無料の W\&B アカウントにサインアップする必要があります。

```python theme={null}
import wandb

wandb.login()
```

<div id="2-initialize-a-run">
  ## 2. run を初期化する
</div>

[`wandb.init()`](/ja/models/ref/python/functions/init) を使用して run を初期化します。これにより、データを Sync・ログするバックグラウンドプロセスが起動します。プロジェクト名とジョブタイプを指定します:

```python theme={null}
# W&B の run を作成します。ここでは、このサンプルがデータセット artifact の作成方法を示すため、
# ジョブタイプとして 'dataset' を指定しています。
with wandb.init(project="artifacts-example", job_type="upload-dataset") as run:
    # ここにコードを記述します
```

<div id="3-create-an-artifact-object">
  ## 3. artifact オブジェクトを作成する
</div>

[`wandb.Artifact()`](/ja/models/ref/python/experiments/artifact) を使用して artifact オブジェクトを作成します。`name` パラメーターには artifact の名を、`type` パラメーターにはファイルタイプを表す説明を指定します。

たとえば、次のコードスニペットは、`‘dataset’` ラベルを持つ `‘bicycle-dataset’` という artifact を作成する方法を示しています。

```python theme={null}
artifact = wandb.Artifact(name="bicycle-dataset", type="dataset")
```

artifact の作成方法の詳細については、[Artifacts を作成する](./construct-an-artifact)を参照してください。

<div id="4-add-the-dataset-to-the-artifact">
  ## 4. データセットをartifactに追加する
</div>

artifactにファイルを追加します。一般的なファイルタイプには、モデルやデータセットがあります。次の例では、ローカル環境に保存されている `dataset.h5` という名前のデータセットをartifactに追加します。

```python theme={null}
# artifactのコンテンツにファイルを追加する
artifact.add_file(local_path="dataset.h5")
```

前のコードスニペットのファイル名 `dataset.h5` を、artifact に追加するファイルのパスに置き換えます。

<div id="5-log-the-dataset">
  ## 5. データセットをログする
</div>

W\&B の run オブジェクトの `wandb.Run.log_artifact()` method を使用して、artifact のバージョンを保存すると同時に、その artifact を [run の出力](/ja/models/artifacts/explore-and-traverse-an-artifact-graph) として宣言します。

```python theme={null}
# artifact のバージョンを W&B に保存し、
# この run の出力としてマークする
run.log_artifact(artifact)
```

artifact をログすると、デフォルトで `'latest'` の[エイリアス](/ja/models/artifacts/create-a-custom-alias)が作成されます。artifact のエイリアスとバージョンの詳細については、それぞれ[カスタムエイリアスを作成する](./create-a-custom-alias)と[新しい artifact バージョンを作成する](./create-a-new-artifact-version)を参照してください。

以上を踏まえると、ここまでのスクリプトは次のようになります。

```python theme={null}
import wandb

wandb.login()

with wandb.init(project="artifacts-example", job_type="upload-dataset") as run:
    artifact = wandb.Artifact(name="bicycle-dataset", type="dataset")
    artifact.add_file(local_path="dataset.h5")
    run.log_artifact(artifact)
```

<div id="6-download-and-use-the-artifact">
  ## 6. artifact をダウンロードして使用する
</div>

次のコード例では、ログして W\&B サーバーに保存した artifact を使用する手順を示します。

1. まず、**`wandb.init()`** で新しい run オブジェクトを初期化します。
2. 次に、run オブジェクトの [`wandb.Run.use_artifact()`](/ja/models/ref/python/experiments/run#use_artifact) method を使用して、使用する artifact を W\&B に指定します。これにより、artifact オブジェクトが返されます。
3. 最後に、artifact の [`wandb.Artifact.download()`](/ja/models/ref/python/experiments/artifact#download) method を使用して、artifact の内容をダウンロードします。

```python theme={null}
# W&B の Run を作成します。ここでは 'type' に 'training' を指定します
# この run をトレーニングのトラッキングに使用するためです。
with wandb.init(project="artifacts-example", job_type="training") as run:

  # W&B に artifact をクエリし、この run への入力としてマークします
  artifact = run.use_artifact("bicycle-dataset:latest")

  # artifact の内容をダウンロードします
  artifact_dir = artifact.download()
```

あるいは、Run の外で W\&B にすでに保存されているデータをエクスポートしたり、更新したりするには、Public API (`wandb.Api`) を使用できます。詳細は [外部ファイルをトラッキングする](./track-external-files) を参照してください。
