Download a model version
Use the W&B Python SDK to download a model artifact that you linked to the Model Registry. Downloading a model is particularly useful if you want to load or consume a model in a future to evaluate a model's performance, make predictions with a dataset, or use ship the model to production.
You are responsible for providing additional Python functions, API calls to reconstruct, deserialize your model into a form that you can work with.
W&B suggests that you document information on how to load models into memory with model cards. For more information, see the Document machine learning models page.
Replace values within <>
with your own:
import wandb
# Initialize a run
run = wandb.init(project="<project>", entity="<entity>")
# Access and download model. Returns path to downloaded artifact
downloaded_model_path = run.use_model(name="<your-model-name>")
Reference a model version with one of following formats listed:
latest
- Uselatest
alias to specify the model version that is most recently linked.v#
- Usev0
,v1
,v2
, and so on to fetch a specific version in the Registered Modelalias
- Specify the custom alias that you and your team assigned to your model version
See use_model
in the API Reference guide for more information on possible parameters and return type.
Example: Download and use a logged model
For example, in the proceeding code snippet a user called the use_model
API. They specified the name of the model artifact they want to fetch and they also provided a version/alias. They then stored the path that returned from the API to the downloaded_model_path
variable.
import wandb
entity = "luka"
project = "NLP_Experiments"
alias = "latest" # semantic nickname or identifier for the model version
model_artifact_name = "fine-tuned-model"
# Initialize a run
run = wandb.init()
# Access and download model. Returns path to downloaded artifact
downloaded_model_path = run.use_model(name=f"{entity/project/model_artifact_name}:{alias}")