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