espnet2.torch_utils.load_pretrained_model.load_pretrained_model
Less than 1 minute
espnet2.torch_utils.load_pretrained_model.load_pretrained_model
espnet2.torch_utils.load_pretrained_model.load_pretrained_model(init_param: str, model: Module, ignore_init_mismatch: bool, map_location: str = 'cpu')
Load a model state and set it to the model.
This function loads a pretrained model’s state dictionary from a specified file and applies it to the provided model. It supports various configurations for selecting source and destination keys, as well as excluding certain keys.
- Parameters:
- init_param – A string formatted as <file_path>:<src_key>:<dst_key>:<exclude_Keys>.
- file_path: Path to the pretrained model file.
- src_key: The key in the source state dictionary to load.
- dst_key: The key in the model to which the state should be loaded.
- exclude_Keys: A comma-separated list of keys to exclude from loading.
- model – The model instance (torch.nn.Module) to which the state will be applied.
- ignore_init_mismatch – A boolean indicating whether to ignore mismatches during initialization.
- map_location – The location to map the model parameters (default is “cpu”).
- init_param – A string formatted as <file_path>:<src_key>:<dst_key>:<exclude_Keys>.
Examples
>>> load_pretrained_model("somewhere/model.pth", model)
>>> load_pretrained_model("somewhere/model.pth:decoder:decoder", model)
>>> load_pretrained_model("somewhere/model.pth:decoder:decoder:", model)
>>> load_pretrained_model(
... "somewhere/model.pth:decoder:decoder:decoder.embed", model
... )
>>> load_pretrained_model("somewhere/decoder.pth::decoder", model)