aibedo.interface.reload_model_from_config_and_ckpt

aibedo.interface.reload_model_from_config_and_ckpt(config: omegaconf.dictconfig.DictConfig, model_path: str, load_datamodule: bool = False)[source]

Load a model as defined by config.model and reload its weights from model_path.

Parameters
  • config (DictConfig) – The config to use to reload the model

  • model_path (str) – The path to the model checkpoint (its weights)

  • load_datamodule (bool) – Whether to return the datamodule too. Default: False

Returns

BaseModel – The reloaded model if load_datamodule is False, otherwise a tuple of (reloaded-model, datamodule)

Examples:

# If you used wandb to save the model, you can use the following to reload it
from aibedo.utilities.wandb_api import load_hydra_config_from_wandb

run_path = ENTITY/PROJECT/RUN_ID   # wandb run id (you can find it on the wandb URL after runs/, e.g. 1f5ehvll)
config = load_hydra_config_from_wandb(run_path, override_kwargs=['datamodule.num_workers=4', 'trainer.gpus=-1'])

model, datamodule = reload_model_from_config_and_ckpt(config, model_path, load_datamodule=True)

# Test the reloaded model
trainer = hydra.utils.instantiate(config.trainer, _recursive_=False)
trainer.test(model=model, datamodule=datamodule)