MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / load

Function load

monai/bundle/scripts.py:628–768  ·  view source on GitHub ↗

Load model weights or TorchScript module of a bundle. Args: name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`. for example: "spleen_ct_segmentation", "prostate_mri_anatomy" in model-zoo: https://github.com/Proje

(
    name: str,
    model: torch.nn.Module | None = None,
    version: str | None = None,
    workflow_type: str = "train",
    model_file: str | None = None,
    load_ts_module: bool = False,
    bundle_dir: PathLike | None = None,
    source: str = DEFAULT_DOWNLOAD_SOURCE,
    repo: str | None = None,
    remove_prefix: str | None = "monai_",
    progress: bool = True,
    device: str | None = None,
    key_in_ckpt: str | None = None,
    config_files: Sequence[str] = (),
    workflow_name: str | BundleWorkflow | None = None,
    args_file: str | None = None,
    copy_model_args: dict | None = None,
    net_override: dict | None = None,
)

Source from the content-addressed store, hash-verified

626
627
628def load(
629 name: str,
630 model: torch.nn.Module | None = None,
631 version: str | None = None,
632 workflow_type: str = "train",
633 model_file: str | None = None,
634 load_ts_module: bool = False,
635 bundle_dir: PathLike | None = None,
636 source: str = DEFAULT_DOWNLOAD_SOURCE,
637 repo: str | None = None,
638 remove_prefix: str | None = "monai_",
639 progress: bool = True,
640 device: str | None = None,
641 key_in_ckpt: str | None = None,
642 config_files: Sequence[str] = (),
643 workflow_name: str | BundleWorkflow | None = None,
644 args_file: str | None = None,
645 copy_model_args: dict | None = None,
646 net_override: dict | None = None,
647) -> object | tuple[torch.nn.Module, dict, dict] | Any:
648 """
649 Load model weights or TorchScript module of a bundle.
650
651 Args:
652 name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`.
653 for example:
654 "spleen_ct_segmentation", "prostate_mri_anatomy" in model-zoo:
655 https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1.
656 "monai_brats_mri_segmentation" in ngc:
657 https://catalog.ngc.nvidia.com/models?filters=&orderBy=scoreDESC&query=monai.
658 "mednist_gan" in monaihosting:
659 https://api.ngc.nvidia.com/v2/models/nvidia/monaihosting/mednist_gan/versions/0.2.0/files/mednist_gan_v0.2.0.zip
660 model: a pytorch module to be updated. Default to None, using the "network_def" in the bundle.
661 version: version name of the target bundle to download, like: "0.1.0". If `None`, will download
662 the latest version. If `source` is "huggingface_hub", this argument is a Git revision id.
663 workflow_type: specifies the workflow type: "train" or "training" for a training workflow,
664 or "infer", "inference", "eval", "evaluation" for a inference workflow,
665 other unsupported string will raise a ValueError.
666 default to `train` for training workflow.
667 model_file: the relative path of the model weights or TorchScript module within bundle.
668 If `None`, "models/model.pt" or "models/model.ts" will be used.
669 load_ts_module: a flag to specify if loading the TorchScript module.
670 bundle_dir: directory the weights/TorchScript module will be loaded from.
671 Default is `bundle` subfolder under `torch.hub.get_dir()`.
672 source: storage location name. This argument is used when `model_file` is not existing locally and need to be
673 downloaded first.
674 In default, the value is achieved from the environment variable BUNDLE_DOWNLOAD_SRC, and
675 it should be "ngc", "monaihosting", "github", or "huggingface_hub".
676 repo: repo name. This argument is used when `url` is `None` and `source` is "github" or "huggingface_hub".
677 If `source` is "github", it should be in the form of "repo_owner/repo_name/release_tag".
678 If `source` is "huggingface_hub", it should be in the form of "repo_owner/repo_name".
679 remove_prefix: This argument is used when `source` is "ngc". Currently, all ngc bundles
680 have the ``monai_`` prefix, which is not existing in their model zoo contrasts. In order to
681 maintain the consistency between these three sources, remove prefix is necessary.
682 Therefore, if specified, downloaded folder name will remove the prefix.
683 progress: whether to display a progress bar when downloading.
684 device: target device of returned weights or module, if `None`, prefer to "cuda" if existing.
685 key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model

Callers 7

test_load_weightsMethod · 0.90
test_load_ts_moduleMethod · 0.90
loadMethod · 0.85
from_pretrainedMethod · 0.85
load_moduleFunction · 0.85

Calls 9

load_net_with_metadataFunction · 0.90
get_state_dictFunction · 0.90
copy_model_stateFunction · 0.90
_process_bundle_dirFunction · 0.85
_add_ngc_prefixFunction · 0.85
_remove_ngc_prefixFunction · 0.85
downloadFunction · 0.85
create_workflowFunction · 0.85
loadMethod · 0.80

Tested by 4

test_load_weightsMethod · 0.72
test_load_ts_moduleMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…