CheckpointLoader#
- class CheckpointLoader(keywords='', replacement=None, replacements=None, strict=False)[source]#
Bases:
HookBaseLoad model weights and, when requested, resume optimizer/train state.
In
before_train, before the trainer enters the loop, delegates toload_weight_and_resume(), which loads weights fromcfg.weight(warm-start) or resumes a full run fromcfg.resume. State-dict keys are rewritten by the configured replacement rules before loading, which is how a checkpoint trained under one module layout is mapped onto another (e.g. wrapping a backbone undermodule.model.backbone). Registered asCheckpointLoader.- Parameters:
keywords (str) – Single substring to match in checkpoint keys when using the scalar form. Defaults to
""(matches everything / no-op).replacement (str, optional) – String to substitute for
keywordsin matched keys. WhenNone, falls back tokeywords(i.e. no rewrite). Defaults toNone.replacements (dict, optional) – A
{keyword: replacement}mapping to apply several key rewrites in a singleload_state_dictcall, so missing/unexpected keys are reported truthfully. Overlapping keywords are resolved by longest match, so dict order is irrelevant. Mutually exclusive in practice with the scalar form. Must be a dict or aTypeErroris raised. Defaults toNone.strict (bool) – Whether to require an exact key match when loading model weights (passed through to
load_state_dict). Defaults toFalse.
Note
Warm-starting (
cfg.weight) loads model weights only; full resume (cfg.resume) additionally restores optimizer, scheduler, and step state. After loading, judge success by inspecting the reported missing / unexpected keys rather than assuming silence means success.Example
Add to
cfg.hooks; once inbefore_trainit loadscfg.weight(warm-start) orcfg.resume(full resume), rewriting state-dict keys by the given rules beforeload_state_dict:hooks = [ dict(type="CheckpointLoader", replacements={ "module.backbone": "module.model.backbone", "module.decoder": "module.model.decoder", }), ] # → before the loop, loads cfg.weight/cfg.resume after renaming # "module.backbone.*" -> "module.model.backbone.*" (etc.); inspect the # reported missing/unexpected keys to confirm the load took