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

> 複数のプロジェクトからArtifactsをダウンロードして使用します。

# Artifactsをダウンロードして使用する

W\&B Server にすでに保存されている artifact をダウンロードして使用するか、必要に応じて重複排除のために アーティファクト オブジェクト を作成して渡します。

<Note>
  Models **Viewer** seat のチームメンバーは Artifacts をダウンロードできません。
</Note>

<div id="download-and-use-an-artifact-stored-on-wb">
  ### W\&B に保存されている artifact をダウンロードして使用する
</div>

W\&B Run の実行中でも実行外でも、W\&B に保存されている artifact をダウンロードして使用できます。W\&B にすでに保存されているデータをエクスポート (または更新) するには、Public API ([`wandb.Api`](/ja/models/ref/python/public-api/api)) を使用します。

<Tabs>
  <Tab title="run 中">
    まず、W\&B Python SDK を import します。次に、W\&B [Run](/ja/models/ref/python/experiments/run) を作成します。

    ```python theme={null}
    import wandb

    with wandb.init(project="<example>", job_type="<job-type>") as run:
        # 次のステップを参照してください
    ```

    [`wandb.Run.use_artifact()`](/ja/models/ref/python/experiments/run#use_artifact) method を使用して、使いたい artifact を指定します。この method は run オブジェクトを返します。次の コードスニペット では、`'latest'` alias が付いた `'bike-dataset'` という artifact を指定しています。

    ```python theme={null}
    # 使用する artifact を指定します。形式は "name:alias" です
    artifact = run.use_artifact("bike-dataset:latest")
    ```

    返された object を使って、artifact の内容全体をダウンロードします。

    ```python theme={null}
    # artifact 全体をダウンロードします
    datadir = artifact.download()
    ```

    必要に応じて、`root` パラメーターにパスを渡して、artifact の内容を特定のディレクトリにダウンロードすることもできます。

    [`wandb.Artifact.get_entry()`](/ja/models/ref/python/experiments/artifact#get_entry) method を使用すると、ファイルの一部だけをダウンロードできます。

    ```python theme={null}
    # 特定のファイルをダウンロードします
    entry = artifact.get_entry(name)
    ```

    以上をまとめると、完全なコード例は次のようになります。

    ```python theme={null}
    import wandb    

    with wandb.init(project="<example>", job_type="<job-type>") as run:
        # 使用する artifact を指定します。形式は "name:alias" です
        artifact = run.use_artifact("bike-dataset:latest")

        # artifact 全体をダウンロードします
        datadir = artifact.download()

        # 特定のファイルをダウンロードします
        entry = artifact.get_entry("bike.png")
    ```

    これにより、パス `name` にあるファイルだけを取得します。返されるのは `Entry` オブジェクトで、次の methods があります。

    * `Entry.download`: パス `name` にあるファイルを artifact からダウンロードします
    * `Entry.ref`: `add_reference` によってそのエントリが参照として保存されている場合は、URI を返します
  </Tab>

  <Tab title="run の外部">
    まず、W\&B SDK を import します。次に、Public API Class から アーティファクト オブジェクト を作成します。その artifact に関連付けられた entity、プロジェクト、artifact、alias を指定します。

    ```python theme={null}
    import wandb

    api = wandb.Api()
    artifact = api.artifact("entity/project/artifact:alias")
    ```

    返された object を使って、artifact の内容をダウンロードします。

    ```python theme={null}
    artifact.download()
    ```

    必要に応じて、`root` パラメーターにパスを渡して、artifact の内容を特定のディレクトリにダウンロードすることもできます。詳細は、[Python SDK Reference Guide](/ja/models/ref/python/experiments/artifact#download) を参照してください。
  </Tab>

  <Tab title="W&B CLI">
    W\&B Server から artifact をダウンロードするには、`wandb artifact get` command を使用します。

    ```
    $ wandb artifact get project/artifact:alias --root mnist/
    ```
  </Tab>
</Tabs>

<div id="partially-download-an-artifact">
  ### artifact を部分的にダウンロードする
</div>

プレフィックスを指定して、artifact の一部だけを必要に応じてダウンロードできます。単一のファイルまたはサブフォルダーの内容をダウンロードするには、`path_prefix=` (`wandb.Artifact.download(path_prefix=)`) パラメーターを使用します。

```python theme={null}
with wandb.init(project="<example>", job_type="<job-type>") as run:
    # 使用する artifact を指定します。形式は "name:alias"
    artifact = run.use_artifact("bike-dataset:latest")

    # 特定のファイルまたはサブフォルダーをダウンロードする
    artifact.download(path_prefix="bike.png") # bike.png のみをダウンロードする
```

また、特定のディレクトリからファイルをダウンロードすることもできます。その場合は、`path_prefix=` パラメーターにディレクトリを指定します。前のコードスニペットに続けて、次のように記述します。

```python theme={null}
# images/bikes ディレクトリ内のファイルをダウンロードする
artifact.download(path_prefix="images/bikes/") 
```

<div id="use-an-artifact-from-a-different-project">
  ### 別のプロジェクトのartifactを使用する
</div>

artifactを参照するには、そのartifactの名をプロジェクト名とあわせて指定します。artifactの名にentity名も指定すると、entityをまたいでartifactを参照することもできます。

以下のコード例は、現在のW\&B runへの入力として別のプロジェクトのartifactをクエリする方法を示しています。

```python theme={null}
with wandb.init(project="<example>", job_type="<job-type>") as run:
    # 別のプロジェクトのartifactをW&BにクエリしてこのrunへのInputとしてマークする
    #
    artifact = run.use_artifact("my-project/artifact:alias")

    # 別のentityのartifactを使用してこのrunへのInputとしてマークする
    #
    artifact = run.use_artifact("my-entity/my-project/artifact:alias")

```

<div id="construct-and-use-an-artifact-simultaneously">
  ### artifact を作成して同時に使用する
</div>

artifact を作成すると同時に使用できます。artifact オブジェクトを作成し、`use_artifact` に渡します。これにより、まだ存在していない場合は W\&B に artifact が作成されます。[`wandb.Run.use_artifact()`](/ja/models/ref/python/experiments/run#use_artifact) API は冪等であるため、何度呼び出しても問題ありません。

```python theme={null}
import wandb

with wandb.init(project="<example>", job_type="<job-type>") as run:
    artifact = wandb.Artifact("reference model")
    artifact.add_file("model.h5")
    run.use_artifact(artifact)
```

artifact の作成について詳しくは、[artifact を作成する](/ja/models/artifacts/construct-an-artifact/)を参照してください。
