MCPcopy Create free account
hub / github.com/VCIP-RGBD/DFormer / load_pretrain

Function load_pretrain

utils/load_utils.py:63–80  ·  view source on GitHub ↗
(model, filename, strict=False, revise_keys=[(r"^module\.", "")])

Source from the content-addressed store, hash-verified

61
62
63def load_pretrain(model, filename, strict=False, revise_keys=[(r"^module\.", "")]):
64 checkpoint = torch.load(filename)
65 # OrderedDict is a subclass of dict
66 if not isinstance(checkpoint, dict):
67 raise RuntimeError(f"No state_dict found in checkpoint file {filename}")
68 # get state_dict from checkpoint
69 if "state_dict" in checkpoint:
70 state_dict = checkpoint["state_dict"]
71 elif "model" in checkpoint:
72 state_dict = checkpoint["model"]
73 else:
74 state_dict = checkpoint
75 # strip prefix of state_dict
76 for p, r in revise_keys:
77 state_dict = {re.sub(p, r, k): v for k, v in state_dict.items()}
78 # load state_dict
79 load_state_dict(model, state_dict, strict)
80 return checkpoint

Callers

nothing calls this directly

Calls 1

load_state_dictFunction · 0.85

Tested by

no test coverage detected