There are a few different ways to load a ZenML Model in code:
Load the active model in a pipeline
You can also use the active model to get the model metadata, or the associated artifacts directly as described in the starter guide:
from zenml import step, pipeline, get_step_context, pipeline, Model@pipeline(model=Model(name="my_model"))defmy_pipeline(): ...@stepdefmy_step():# Get model from active step context mv =get_step_context().model# Get metadataprint(mv.run_metadata["metadata_key"].value)# Directly fetch an artifact that is attached to the model output = mv.get_artifact("my_dataset", "my_version") output.run_metadata["accuracy"].value
Load any model via the Client
Alternatively, you can use the Client:
from zenml import stepfrom zenml.client import Clientfrom zenml.enums import ModelStages@stepdefmodel_evaluator_step() ...# Get staging model version try: staging_zenml_model =Client().get_model_version( model_name_or_id="<INSERT_MODEL_NAME>", model_version_name_or_number_or_id=ModelStages.STAGING, )exceptKeyError: staging_zenml_model =None ...