MCPcopy Create free account
hub / github.com/360CVGroup/FancyVideo / load_weights

Function load_weights

fancyvideo/utils/util.py:94–157  ·  view source on GitHub ↗
(
    animation_pipeline,
    # motion module
    motion_module_path         = "",
    motion_module_lora_configs = [],
    # image layers
    dreambooth_model_path = "",
    lora_model_path       = "",
    lora_alpha            = 0.8,
)

Source from the content-addressed store, hash-verified

92 return ddim_latents
93
94def load_weights(
95 animation_pipeline,
96 # motion module
97 motion_module_path = "",
98 motion_module_lora_configs = [],
99 # image layers
100 dreambooth_model_path = "",
101 lora_model_path = "",
102 lora_alpha = 0.8,
103):
104 # 1.1 motion module
105 unet_state_dict = {}
106 if motion_module_path != "":
107 print(f"load motion module from {motion_module_path}")
108 motion_module_state_dict = torch.load(motion_module_path, map_location="cpu")
109 motion_module_state_dict = motion_module_state_dict["state_dict"] if "state_dict" in motion_module_state_dict else motion_module_state_dict
110 unet_state_dict.update({name: param for name, param in motion_module_state_dict.items() if "motion_modules." in name})
111
112 missing, unexpected = animation_pipeline.unet.load_state_dict(unet_state_dict, strict=False)
113 assert len(unexpected) == 0
114 del unet_state_dict
115
116 if dreambooth_model_path != "":
117 print(f"load dreambooth model from {dreambooth_model_path}")
118 if dreambooth_model_path.endswith(".safetensors"):
119 dreambooth_state_dict = {}
120 with safe_open(dreambooth_model_path, framework="pt", device="cpu") as f:
121 for key in f.keys():
122 dreambooth_state_dict[key] = f.get_tensor(key)
123 elif dreambooth_model_path.endswith(".ckpt"):
124 dreambooth_state_dict = torch.load(dreambooth_model_path, map_location="cpu")
125
126 # 1. vae
127 converted_vae_checkpoint = convert_ldm_vae_checkpoint(dreambooth_state_dict, animation_pipeline.vae.config)
128 animation_pipeline.vae.load_state_dict(converted_vae_checkpoint)
129 # 2. unet
130 converted_unet_checkpoint = convert_ldm_unet_checkpoint(dreambooth_state_dict, animation_pipeline.unet.config)
131 animation_pipeline.unet.load_state_dict(converted_unet_checkpoint, strict=False)
132 # 3. text_model
133 animation_pipeline.text_encoder = convert_ldm_clip_checkpoint(dreambooth_state_dict)
134 del dreambooth_state_dict
135
136 if lora_model_path != "":
137 print(f"load lora model from {lora_model_path}")
138 assert lora_model_path.endswith(".safetensors")
139 lora_state_dict = {}
140 with safe_open(lora_model_path, framework="pt", device="cpu") as f:
141 for key in f.keys():
142 lora_state_dict[key] = f.get_tensor(key)
143
144 animation_pipeline = convert_lora(animation_pipeline, lora_state_dict, alpha=lora_alpha)
145 del lora_state_dict
146
147
148 for motion_module_lora_config in motion_module_lora_configs:
149 path, alpha = motion_module_lora_config["path"], motion_module_lora_config["alpha"]
150 print(f"load motion LoRA from {path}")
151

Callers

nothing calls this directly

Calls 5

convert_loraFunction · 0.90

Tested by

no test coverage detected