MCPcopy Index your code
hub / github.com/CompVis/diff2flow / load_model_from_config

Function load_model_from_config

diff2flow/helpers.py:30–53  ·  view source on GitHub ↗
(config, ckpt, verbose=False, ignore_keys=[])

Source from the content-addressed store, hash-verified

28
29
30def load_model_from_config(config, ckpt, verbose=False, ignore_keys=[]):
31 print(f"Loading model from {ckpt}")
32 pl_sd = torch.load(ckpt, map_location="cpu")
33 if "global_step" in pl_sd:
34 print(f"Global Step: {pl_sd['global_step']}")
35 sd = pl_sd["state_dict"] if 'state_dict' in pl_sd else pl_sd
36 keys = list(sd.keys())
37 for k in keys:
38 for ik in ignore_keys:
39 if ik and k.startswith(ik):
40 print("Deleting key {} from state_dict.".format(k))
41 del sd[k]
42 model = instantiate_from_config(config.model)
43 m, u = model.load_state_dict(sd, strict=False)
44 if len(m) > 0 and verbose:
45 print("missing keys:")
46 print(m)
47 if len(u) > 0 and verbose:
48 print("unexpected keys:")
49 print(u)
50 print(f'Missing {len(m)} keys and unexpecting {len(u)} keys')
51 # model.cuda()
52 # model.eval()
53 return model
54
55
56def freeze(model: nn.Module):

Callers

nothing calls this directly

Calls 1

instantiate_from_configFunction · 0.85

Tested by

no test coverage detected