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

Class CheckpointLoader

monai/handlers/checkpoint_loader.py:31–156  ·  view source on GitHub ↗

CheckpointLoader acts as an Ignite handler to load checkpoint data from file. It can load variables for network, optimizer, lr_scheduler, etc. If saving checkpoint after `torch.nn.DataParallel`, need to save `model.module` instead as PyTorch recommended and then use this loader to l

Source from the content-addressed store, hash-verified

29
30
31class CheckpointLoader:
32 """
33 CheckpointLoader acts as an Ignite handler to load checkpoint data from file.
34 It can load variables for network, optimizer, lr_scheduler, etc.
35 If saving checkpoint after `torch.nn.DataParallel`, need to save `model.module` instead
36 as PyTorch recommended and then use this loader to load the model.
37
38 Usage example::
39
40 trainer = SupervisedTrainer(...)
41 save_dict = {
42 "trainer": trainer,
43 "net": network,
44 "opt": optimizer,
45 "lr": lr_scheduler,
46 }
47
48 map_location = "cuda:0"
49 # checkpoint needs to have same save_dict for this to work
50 handler = CheckpointLoader(load_path="/test/checkpoint.pt", load_dict=save_dict, map_location=map_location, strict=True)
51 handler(trainer)
52 # Trainer now has the same state as stored, including the number of epochs and iterations completed
53 # so you can resume an interrupted training at the place where it left
54
55 Args:
56 load_path: the file path of checkpoint, it should be a PyTorch `pth` file.
57 load_dict: target objects that load checkpoint to. examples::
58
59 {'network': net, 'optimizer': optimizer, 'lr_scheduler': lr_scheduler}
60
61 name: identifier of logging.logger to use, if None, defaulting to ``engine.logger``.
62 map_location: when loading the module for distributed training/evaluation,
63 need to provide an appropriate map_location argument to prevent a process
64 to step into others’ devices. If map_location is missing, torch.load will
65 first load the module to CPU and then copy each parameter to where it was
66 saved, which would result in all processes on the same machine using the
67 same set of devices.
68 strict: whether to strictly enforce that the keys and data shape in the `state_dict` of every item
69 of `load_dict` match the `state_dict` of the corresponding items of checkpoint, default to `True`.
70 strict_shape: whether to enforce the data shape of the matched layers in the checkpoint,
71 `if `False`, it will skip the layers that have different data shape with checkpoint content,
72 and ignore the `strict` arg. this can be useful advanced feature for transfer learning.
73 users should totally understand which layers will have different shape. default to `True`.
74
75 Note: if `strict_shape=False`, will only load checkpoint for `torch.nn.Module` and skip other
76 items in the `load_dict`. For example, if the shape of some layers in current model can't
77 match the checkpoint, the `parameter_group` of current optimizer may also can't match the
78 checkpoint, so skip loading checkpoint for optimizer.
79
80 For more details about loading checkpoint, please refer to:
81 https://pytorch.org/ignite/v0.4.5/generated/ignite.handlers.checkpoint.Checkpoint.html
82 #ignite.handlers.checkpoint.Checkpoint.load_objects.
83 https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.load_state_dict.
84
85 """
86
87 def __init__(
88 self,

Calls

no outgoing calls

Tested by 8

run_inference_testFunction · 0.72
test_load_state_dictMethod · 0.72
test_strict_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…