mindspore.load_checkpoint
- mindspore.load_checkpoint(ckpt_file_name, net=None, strict_load=False, filter_prefix=None, dec_key=None, dec_mode='AES-GCM', specify_prefix=None, choice_func=None)[source]
Load checkpoint info from a specified file.
Note
specify_prefix and filter_prefix do not affect each other.
If none of the parameters are loaded from checkpoint file, it will throw ValueError.
specify_prefix and filter_prefix are in the process of being deprecated, choice_func is recommended instead. And using either of those two args will override choice_func at the same time.
- Parameters
ckpt_file_name (str) – Checkpoint file name.
net (Cell) – The network where the parameters will be loaded. Default:
None
.strict_load (bool) – Whether to strict load the parameter into net. If
False
, it will load parameter into net when parameter name’s suffix in checkpoint file is the same as the parameter in the network. When the types are inconsistent perform type conversion on the parameters of the same type, such as float32 to float16. Default:False
.filter_prefix (Union[str, list[str], tuple[str]]) – Deprecated(see choice_func). Parameters starting with the filter_prefix will not be loaded. Default:
None
.dec_key (Union[None, bytes]) – Byte type key used for decryption. If the value is
None
, the decryption is not required. Default:None
.dec_mode (str) – This parameter is valid only when dec_key is not set to
None
. Specifies the decryption mode, currently supports"AES-GCM"
and"AES-CBC"
and"SM4-CBC"
. Default:"AES-GCM"
.specify_prefix (Union[str, list[str], tuple[str]]) – Deprecated(see choice_func). Parameters starting with the specify_prefix will be loaded. Default:
None
.choice_func (Union[None, function]) – Input value of the function is a Parameter name of type string, and the return value is a bool. If returns
True
, the Parameter that matches the custom condition will be loaded. If returnsFalse
, the Parameter that matches the custom condition will be removed. Default:None
.
- Returns
Dict, key is parameter name, value is a Parameter or string. When the append_dict parameter of
mindspore.save_checkpoint()
and the append_info parameter ofmindspore.train.CheckpointConfig
are used to save the checkpoint, append_dict and append_info are dict types, and their value are string, then the return value obtained by loading checkpoint is string, and in other cases the return value is Parameter.- Raises
ValueError – Checkpoint file’s format is incorrect.
ValueError – Parameter’s dict is None after load checkpoint file.
TypeError – The type of specify_prefix or filter_prefix is incorrect.
Examples
>>> import mindspore as ms >>> >>> ckpt_file_name = "./checkpoint/LeNet5-1_32.ckpt" >>> param_dict = ms.load_checkpoint(ckpt_file_name, ... choice_func=lambda x: x.startswith("conv") and not x.startswith("conv1")) >>> print(param_dict["conv2.weight"]) Parameter (name=conv2.weight, shape=(16, 6, 5, 5), dtype=Float32, requires_grad=True) >>> def func(param_name): ... whether_load = False ... if param_name.startswith("conv"): ... whether_load = True ... if param_name.startswith("conv1"): ... whether_load = False ... return whether_load >>> param_dict1 = ms.load_checkpoint(ckpt_file_name, choice_func=func) >>> print(param_dict1["conv2.weight"]) Parameter (name=conv2.weight, shape=(16, 6, 5, 5), dtype=Float32, requires_grad=True) >>> def func(param_name): ... whether_load = False ... if param_name.startswith("conv1"): ... whether_load = True ... return whether_load >>> param_dict2 = ms.load_checkpoint(ckpt_file_name, choice_func=func) >>> print(param_dict2) {'conv1.weight': Parameter (name=conv1.weight, shape=(6, 1, 5, 5), dtype=Float32, requires_grad=True)}
- Tutorial Examples: