| 109 | import wandb |
| 110 | |
| 111 | def 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 | |
| 135 | def main(): |