|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import fnmatch |
| 4 | + |
| 5 | +from safetensors.torch import load_file |
| 6 | + |
| 7 | +from diffusers import Kandinsky3UNet |
| 8 | + |
| 9 | + |
| 10 | +MAPPING = { |
| 11 | + "to_time_embed.1": "time_embedding.linear_1", |
| 12 | + "to_time_embed.3": "time_embedding.linear_2", |
| 13 | + "in_layer": "conv_in", |
| 14 | + "out_layer.0": "conv_norm_out", |
| 15 | + "out_layer.2": "conv_out", |
| 16 | + "down_samples": "down_blocks", |
| 17 | + "up_samples": "up_blocks", |
| 18 | + "projection_lin": "encoder_hid_proj.projection_linear", |
| 19 | + "projection_ln": "encoder_hid_proj.projection_norm", |
| 20 | + "feature_pooling": "add_time_condition", |
| 21 | + "to_query": "to_q", |
| 22 | + "to_key": "to_k", |
| 23 | + "to_value": "to_v", |
| 24 | + "output_layer": "to_out.0", |
| 25 | + "self_attention_block": "attentions.0", |
| 26 | +} |
| 27 | + |
| 28 | +DYNAMIC_MAP = { |
| 29 | + "resnet_attn_blocks.*.0": "resnets_in.*", |
| 30 | + "resnet_attn_blocks.*.1": ("attentions.*", 1), |
| 31 | + "resnet_attn_blocks.*.2": "resnets_out.*", |
| 32 | +} |
| 33 | +# MAPPING = {} |
| 34 | + |
| 35 | + |
| 36 | +def convert_state_dict(unet_state_dict): |
| 37 | + """ |
| 38 | + Convert the state dict of a U-Net model to match the key format expected by Kandinsky3UNet model. |
| 39 | + Args: |
| 40 | + unet_model (torch.nn.Module): The original U-Net model. |
| 41 | + unet_kandi3_model (torch.nn.Module): The Kandinsky3UNet model to match keys with. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + OrderedDict: The converted state dictionary. |
| 45 | + """ |
| 46 | + # Example of renaming logic (this will vary based on your model's architecture) |
| 47 | + converted_state_dict = {} |
| 48 | + for key in unet_state_dict: |
| 49 | + new_key = key |
| 50 | + for pattern, new_pattern in MAPPING.items(): |
| 51 | + new_key = new_key.replace(pattern, new_pattern) |
| 52 | + |
| 53 | + for dyn_pattern, dyn_new_pattern in DYNAMIC_MAP.items(): |
| 54 | + has_matched = False |
| 55 | + if fnmatch.fnmatch(new_key, f"*.{dyn_pattern}.*") and not has_matched: |
| 56 | + star = int(new_key.split(dyn_pattern.split(".")[0])[-1].split(".")[1]) |
| 57 | + |
| 58 | + if isinstance(dyn_new_pattern, tuple): |
| 59 | + new_star = star + dyn_new_pattern[-1] |
| 60 | + dyn_new_pattern = dyn_new_pattern[0] |
| 61 | + else: |
| 62 | + new_star = star |
| 63 | + |
| 64 | + pattern = dyn_pattern.replace("*", str(star)) |
| 65 | + new_pattern = dyn_new_pattern.replace("*", str(new_star)) |
| 66 | + |
| 67 | + new_key = new_key.replace(pattern, new_pattern) |
| 68 | + has_matched = True |
| 69 | + |
| 70 | + converted_state_dict[new_key] = unet_state_dict[key] |
| 71 | + |
| 72 | + return converted_state_dict |
| 73 | + |
| 74 | + |
| 75 | +def main(model_path, output_path): |
| 76 | + # Load your original U-Net model |
| 77 | + unet_state_dict = load_file(model_path) |
| 78 | + |
| 79 | + # Initialize your Kandinsky3UNet model |
| 80 | + config = {} |
| 81 | + |
| 82 | + # Convert the state dict |
| 83 | + converted_state_dict = convert_state_dict(unet_state_dict) |
| 84 | + |
| 85 | + unet = Kandinsky3UNet(config) |
| 86 | + unet.load_state_dict(converted_state_dict) |
| 87 | + |
| 88 | + unet.save_pretrained(output_path) |
| 89 | + print(f"Converted model saved to {output_path}") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + parser = argparse.ArgumentParser(description="Convert U-Net PyTorch model to Kandinsky3UNet format") |
| 94 | + parser.add_argument("--model_path", type=str, required=True, help="Path to the original U-Net PyTorch model") |
| 95 | + parser.add_argument("--output_path", type=str, required=True, help="Path to save the converted model") |
| 96 | + |
| 97 | + args = parser.parse_args() |
| 98 | + main(args.model_path, args.output_path) |
0 commit comments