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

Function save_state

monai/networks/utils.py:627–658  ·  view source on GitHub ↗

Save the state dict of input source data with PyTorch `save`. It can save `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`. And automatically convert the data parallel module to regular module. For example:: save_state(net, path) save_state(net

(src: torch.nn.Module | dict, path: PathLike, **kwargs)

Source from the content-addressed store, hash-verified

625
626
627def save_state(src: torch.nn.Module | dict, path: PathLike, **kwargs):
628 """
629 Save the state dict of input source data with PyTorch `save`.
630 It can save `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`.
631 And automatically convert the data parallel module to regular module.
632 For example::
633
634 save_state(net, path)
635 save_state(net.state_dict(), path)
636 save_state({"net": net, "opt": opt}, path)
637 net_dp = torch.nn.DataParallel(net)
638 save_state(net_dp, path)
639
640 Refer to: https://pytorch.org/ignite/v0.4.8/generated/ignite.handlers.DiskSaver.html.
641
642 Args:
643 src: input data to save, can be `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`.
644 path: target file path to save the input object.
645 kwargs: other args for the `save_obj` except for the `obj` and `path`.
646 default `func` is `torch.save()`, details of the args:
647 https://pytorch.org/docs/stable/generated/torch.save.html.
648
649 """
650
651 ckpt: dict = {}
652 if isinstance(src, dict):
653 for k, v in src.items():
654 ckpt[k] = get_state_dict(v)
655 else:
656 ckpt = get_state_dict(src)
657
658 save_obj(obj=ckpt, path=path, **kwargs)
659
660
661def convert_to_onnx(

Callers 7

init_bundleFunction · 0.90
test_onnx_exportMethod · 0.90
test_fileMethod · 0.90
test_trt_exportMethod · 0.90
test_onnx_trt_exportMethod · 0.90
test_exportMethod · 0.90
test_default_valueMethod · 0.90

Calls 2

save_objFunction · 0.90
get_state_dictFunction · 0.85

Tested by 6

test_onnx_exportMethod · 0.72
test_fileMethod · 0.72
test_trt_exportMethod · 0.72
test_onnx_trt_exportMethod · 0.72
test_exportMethod · 0.72
test_default_valueMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…