MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / copy_model_state

Function copy_model_state

monai/networks/utils.py:542–624  ·  view source on GitHub ↗

Compute a module state_dict, of which the keys are the same as `dst`. The values of `dst` are overwritten by the ones from `src` whenever their keys match. The method provides additional `dst_prefix` for the `dst` key when matching them. `mapping` can be a `{"src_key": "dst_key"}` dict,

(
    dst: torch.nn.Module | Mapping,
    src: torch.nn.Module | Mapping,
    dst_prefix="",
    mapping=None,
    exclude_vars=None,
    inplace=True,
    filter_func=None,
)

Source from the content-addressed store, hash-verified

540
541
542def copy_model_state(
543 dst: torch.nn.Module | Mapping,
544 src: torch.nn.Module | Mapping,
545 dst_prefix="",
546 mapping=None,
547 exclude_vars=None,
548 inplace=True,
549 filter_func=None,
550):
551 """
552 Compute a module state_dict, of which the keys are the same as `dst`. The values of `dst` are overwritten
553 by the ones from `src` whenever their keys match. The method provides additional `dst_prefix` for
554 the `dst` key when matching them. `mapping` can be a `{"src_key": "dst_key"}` dict, indicating
555 `dst[dst_prefix + dst_key] = src[src_key]`.
556 This function is mainly to return a model state dict
557 for loading the `src` model state into the `dst` model, `src` and `dst` can have different dict keys, but
558 their corresponding values normally have the same shape.
559
560 Args:
561 dst: a pytorch module or state dict to be updated.
562 src: a pytorch module or state dict used to get the values used for the update.
563 dst_prefix: `dst` key prefix, so that `dst[dst_prefix + src_key]`
564 will be assigned to the value of `src[src_key]`.
565 mapping: a `{"src_key": "dst_key"}` dict, indicating that `dst[dst_prefix + dst_key]`
566 to be assigned to the value of `src[src_key]`.
567 exclude_vars: a regular expression to match the `dst` variable names,
568 so that their values are not overwritten by `src`.
569 inplace: whether to set the `dst` module with the updated `state_dict` via `load_state_dict`.
570 This option is only available when `dst` is a `torch.nn.Module`.
571 filter_func: a filter function used to filter the weights to be loaded.
572 See 'filter_swinunetr' in "monai.networks.nets.swin_unetr.py".
573
574 Examples:
575 .. code-block:: python
576
577 from monai.networks.nets import BasicUNet
578 from monai.networks.utils import copy_model_state
579
580 model_a = BasicUNet(in_channels=1, out_channels=4)
581 model_b = BasicUNet(in_channels=1, out_channels=2)
582 model_a_b, changed, unchanged = copy_model_state(
583 model_a, model_b, exclude_vars="conv_0.conv_0", inplace=False)
584 # dst model updated: 76 of 82 variables.
585 model_a.load_state_dict(model_a_b)
586 # <All keys matched successfully>
587
588 Returns: an OrderedDict of the updated `dst` state, the changed, and unchanged keys.
589
590 """
591 src_dict = get_state_dict(src)
592 dst_dict = OrderedDict(get_state_dict(dst))
593
594 to_skip = {s_key for s_key in src_dict if exclude_vars and re.compile(exclude_vars).search(s_key)}
595
596 # update dst with items from src
597 all_keys, updated_keys = list(dst_dict), list()
598 for s, val in src_dict.items():
599 dst_key = f"{dst_prefix}{s}"

Callers 12

trainMethod · 0.90
evaluateMethod · 0.90
load_from_mmarFunction · 0.90
__call__Method · 0.90
loadFunction · 0.90
_exportFunction · 0.90
test_set_stateMethod · 0.90
test_set_full_stateMethod · 0.90
test_set_exclude_varsMethod · 0.90
test_set_map_acrossMethod · 0.90
test_set_prefixMethod · 0.90
test_filter_swinunetrMethod · 0.90

Calls 4

get_state_dictFunction · 0.85
infoMethod · 0.80
appendMethod · 0.45
load_state_dictMethod · 0.45

Tested by 6

test_set_stateMethod · 0.72
test_set_full_stateMethod · 0.72
test_set_exclude_varsMethod · 0.72
test_set_map_acrossMethod · 0.72
test_set_prefixMethod · 0.72
test_filter_swinunetrMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…