MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / convert_state_dict

Function convert_state_dict

diffusers/scripts/convert_kandinsky3_unet.py:36–72  ·  view source on GitHub ↗

Convert the state dict of a U-Net model to match the key format expected by Kandinsky3UNet model. Args: unet_model (torch.nn.Module): The original U-Net model. unet_kandi3_model (torch.nn.Module): The Kandinsky3UNet model to match keys with. Returns: OrderedDict

(unet_state_dict)

Source from the content-addressed store, hash-verified

34
35
36def 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
75def main(model_path, output_path):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected