(
checkpoint_path: str,
original_config_file: str,
attention_type: str,
image_size: int = 512,
extract_ema: bool = False,
num_in_channels: int = None,
device: str = None,
)
| 436 | |
| 437 | |
| 438 | def convert_gligen_to_diffusers( |
| 439 | checkpoint_path: str, |
| 440 | original_config_file: str, |
| 441 | attention_type: str, |
| 442 | image_size: int = 512, |
| 443 | extract_ema: bool = False, |
| 444 | num_in_channels: int = None, |
| 445 | device: str = None, |
| 446 | ): |
| 447 | if device is None: |
| 448 | device = "cuda" if torch.cuda.is_available() else "cpu" |
| 449 | checkpoint = torch.load(checkpoint_path, map_location=device) |
| 450 | else: |
| 451 | checkpoint = torch.load(checkpoint_path, map_location=device) |
| 452 | |
| 453 | if "global_step" in checkpoint: |
| 454 | checkpoint["global_step"] |
| 455 | else: |
| 456 | print("global_step key not found in model") |
| 457 | |
| 458 | original_config = yaml.safe_load(original_config_file) |
| 459 | |
| 460 | if num_in_channels is not None: |
| 461 | original_config["model"]["params"]["in_channels"] = num_in_channels |
| 462 | |
| 463 | num_train_timesteps = original_config["diffusion"]["params"]["timesteps"] |
| 464 | beta_start = original_config["diffusion"]["params"]["linear_start"] |
| 465 | beta_end = original_config["diffusion"]["params"]["linear_end"] |
| 466 | |
| 467 | scheduler = DDIMScheduler( |
| 468 | beta_end=beta_end, |
| 469 | beta_schedule="scaled_linear", |
| 470 | beta_start=beta_start, |
| 471 | num_train_timesteps=num_train_timesteps, |
| 472 | steps_offset=1, |
| 473 | clip_sample=False, |
| 474 | set_alpha_to_one=False, |
| 475 | prediction_type="epsilon", |
| 476 | ) |
| 477 | |
| 478 | # Convert the UNet2DConditionalModel model |
| 479 | unet_config = create_unet_config(original_config, image_size, attention_type) |
| 480 | unet = UNet2DConditionModel(**unet_config) |
| 481 | |
| 482 | converted_unet_checkpoint = convert_gligen_unet_checkpoint( |
| 483 | checkpoint, unet_config, path=checkpoint_path, extract_ema=extract_ema |
| 484 | ) |
| 485 | |
| 486 | unet.load_state_dict(converted_unet_checkpoint) |
| 487 | |
| 488 | # Convert the VAE model |
| 489 | vae_config = create_vae_config(original_config, image_size) |
| 490 | converted_vae_checkpoint = convert_gligen_vae_checkpoint(checkpoint, vae_config) |
| 491 | |
| 492 | vae = AutoencoderKL(**vae_config) |
| 493 | vae.load_state_dict(converted_vae_checkpoint) |
| 494 | |
| 495 | # Convert the text model |
no test coverage detected
searching dependent graphs…