Download and use artifacts
Download and use an artifact that is already stored on the Weights & Biases server or construct an artifact object and pass it in to be deduplicated as necessary.
Team members with view-only seats cannot download artifacts.
Download and use an artifact stored on Weights & Biasesโ
Download and use an artifact that is stored in Weights & Biases either inside or outside of a W&B Run. Use the Public API (wandb.Api
) to export (or update data) already saved in Weights & Biases. For more information, see the Weights & Biases Public API Reference guide.
- During a run
- Outside of a run
- wandb CLI
First, import the W&B Python SDK. Next, create a W&B Run:
import wandb
run = wandb.init(project="<example>", job_type="<job-type>")
Indicate the artifact you want to use with the use_artifact
method. This returns a run object. In the proceeding code snippet we specify an artifact called 'bike-dataset'
with alias 'latest'
:
artifact = run.use_artifact('bike-dataset:latest')
Use the object returned to download all the contents of the artifact:
datadir = artifact.download()
You can optionally pass a path to the root parameter to download the contents of the artifact to a specific directory. For more information, see the Python SDK Reference Guide.
Use the get_path
method to download only subset of files:
path = artifact.get_path(name)
This fetches only the file at the path name
. It returns an Entry
object with the following methods:
Entry.download
: Downloads file from the artifact at pathname
Entry.ref
: If the entry was stored as a reference usingadd_reference
, returns the URI
References that have schemes that Weights & Biases knows how to handle can be downloaded just like artifact files. For more information, see Track external files.
import wandb
api = wandb.Api()
artifact = api.artifact('entity/project/artifact:alias')
Use the object returned to download the contents of the artifact:
artifact.download()
You can optionally pass a path the root
parameter to download the contents of the artifact to a specific directory. For more information, see the API Reference Guide.
Use the wandb artifact get
command to download an artifact from the Weights & Biases server.
$ wandb artifact get project/artifact:alias --root mnist/
Use an artifact from a different projectโ
Specify the name of artifact along with its project name to reference an artifact. You can also reference artifacts across entities by specifying the name of the artifact with its entity name.
The following code example demonstrates how to query an artifact from another project as input to our current W&B run.
import wandb
run = wandb.init(project="<example>", job_type="<job-type>")
# Query W&B for an artifact from another project and mark it
# as an input to this run.
artifact = run.use_artifact('my-project/artifact:alias')
# Use an artifact from another entity and mark it as an input
# to this run.
artifact = run.use_artifact('my-entity/my-project/artifact:alias')
Construct and use an artifact simultaneouslyโ
Simultaneously construct and use an artifact. Create an artifact object and pass it to use_artifact. This will create an artifact in Weights & Biases if it does not exist yet. The use_artifact
API is idempotent, so you can call it as many times as you like.
import wandb
artifact = wandb.Artifact('reference model')
artifact.add_file('model.h5')
run.use_artifact(artifact)
For more information about constructing an artifact, see Construct an artifact.