Construct an artifact
Construct a W&B Artifact in three steps:- Create an artifact Python object with
wandb.Artifact() - Add one or more files to the artifact
- Save your artifact to the W&B server
Create an artifact Python object with wandb.Artifact()
Initialize the wandb.Artifact() class to create an artifact object. Specify the following parameters:
- Name: The name of your artifact. The name should be unique, descriptive, and memorable.
- Type: The type of artifact. The type should be simple, descriptive, and correspond to a single step of your machine learning pipeline. Common artifact types include
'dataset'or'model'.
W&B uses the “name” and “type” you provide to create a directed acyclic graph in the W&B App. See the Explore and traverse artifact graphs for more information.
wandb.Artifact Class definition in the Python SDK Reference Guide.
Copy and paste the following code snippet to create an artifact object. Replace the <name> and <type> placeholders with your own values:
Add one or more files to the artifact
Add files, directories, external URI references (such as Amazon S3) and more to your artifact object. To add a single file, use the artifact object’sArtifact.add_file() method:
Artifact.add_dir() method:
Save your artifact to the W&B server
Save your artifact to the W&B server. Use the run object’swandb.Run.log_artifact() method to save the artifact.
Add files to an artifact
The following sections demonstrate how to add different types of objects to an artifact. Assume you have a directory with the following structure as you read through the examples:Add a single file
Usewandb.Artifact.add_file() to add a single local file to an artifact. Provide the local path to the file as the local_path parameter:
'hello.txt' in your working local directory.
name parameter to rename the file within the artifact object itself. Continuing the previous example:
| API Call | Resulting artifact |
|---|---|
artifact.new_file('hello.txt') | hello.txt |
artifact.add_file('model.h5') | model.h5 |
artifact.add_file('checkpoints/model.h5') | model.h5 |
artifact.add_file('model.h5', name='models/mymodel.h5') | models/mymodel.h5 |
Add multiple files
Use thewandb.Artifact.add_dir() method to add multiple files from a local directory to an artifact. Provide the local path to the directory as the local_path parameter.
| API Call | Resulting artifact |
|---|---|
artifact.add_dir('images') |
|
artifact.add_dir('images', name='images') |
|
Add a URI reference
Artifacts track checksums and other information for reproducibility if the URI has a scheme that the W&B library supports. Add an external URI reference to an artifact with thewandb.Artifact.add_reference() method. Replace the 'uri' string with your own URI. Optionally pass the desired path within the artifact for the name parameter.
http(s)://: A path to a file accessible over HTTP. The artifact will track checksums in the form of etags and size metadata if the HTTP server supports theETagandContent-Lengthresponse headers.s3://: A path to an object or object prefix in S3. The artifact will track checksums and versioning information (if the bucket has object versioning enabled) for the referenced objects. Object prefixes are expanded to include the objects under the prefix, up to a maximum of 10,000 objects.gs://: A path to an object or object prefix in GCS. The artifact will track checksums and versioning information (if the bucket has object versioning enabled) for the referenced objects. Object prefixes are expanded to include the objects under the prefix, up to a maximum of 10,000 objects.
| API call | Resulting artifact contents |
|---|---|
artifact.add_reference('s3://my-bucket/model.h5') | model.h5 |
artifact.add_reference('s3://my-bucket/checkpoints/model.h5') | model.h5 |
artifact.add_reference('s3://my-bucket/model.h5', name='models/mymodel.h5') | models/mymodel.h5 |
artifact.add_reference('s3://my-bucket/images') |
|
artifact.add_reference('s3://my-bucket/images', name='images') |
|
Add files to artifacts from parallel runs
For large datasets or distributed training, multiple parallel runs might need to contribute to a single artifact.Find path for logged artifacts and other metadata
The following code snippet shows how to use the W&B Public API to list the files in a run, including their names and URLs. Replace the<entity/project/run-id> placeholder with your own values: