MCPcopy Create free account
hub / github.com/adobe-research/custom-diffusion / load_model_from_config

Function load_model_from_config

sample.py:111–132  ·  view source on GitHub ↗
(config, ckpt, verbose=False)

Source from the content-addressed store, hash-verified

109import wandb
110
111def load_model_from_config(config, ckpt, verbose=False):
112 print(f"Loading model from {ckpt}")
113 pl_sd = torch.load(ckpt, map_location="cpu")
114 if "global_step" in pl_sd:
115 print(f"Global Step: {pl_sd['global_step']}")
116 sd = pl_sd["state_dict"]
117 model = instantiate_from_config(config.model)
118
119 token_weights = sd["cond_stage_model.transformer.text_model.embeddings.token_embedding.weight"]
120 del sd["cond_stage_model.transformer.text_model.embeddings.token_embedding.weight"]
121 m, u = model.load_state_dict(sd, strict=False)
122 model.cond_stage_model.transformer.text_model.embeddings.token_embedding.weight.data[:token_weights.shape[0]] = token_weights
123 if len(m) > 0 and verbose:
124 print("missing keys:")
125 print(m)
126 if len(u) > 0 and verbose:
127 print("unexpected keys:")
128 print(u)
129
130 model.cuda()
131 model.eval()
132 return model
133
134
135def main():

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected