MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / compose

Function compose

src/composenW.py:68–265  ·  view source on GitHub ↗
(paths, category, outpath, pretrained_model_path, regularization_prompt, prompts, save_path, device='cuda')

Source from the content-addressed store, hash-verified

66
67
68def compose(paths, category, outpath, pretrained_model_path, regularization_prompt, prompts, save_path, device='cuda'):
69 model, config = get_model(pretrained_model_path)
70 model.eval()
71 model.requires_grad = False
72
73 layers = []
74 layers_modified = []
75
76 def getlayers(model, root_name=''):
77 for name, module in model.named_children():
78 if module.__class__.__name__ == 'SpatialTransformer':
79 layers_modified.append(root_name + '.' + name + '.transformer_blocks.0.attn2.to_k')
80 layers_modified.append(root_name + '.' + name + '.transformer_blocks.0.attn2.to_v')
81 else:
82 if list(module.children()) == []:
83 layers.append(root_name + '.' + name)
84 else:
85 getlayers(module, root_name + '.' + name)
86
87 getlayers(model.model.diffusion_model)
88
89 for i in range(len(layers_modified)):
90 layers_modified[i] = 'model.diffusion_model' + layers_modified[i] + '.weight'
91
92 def get_text_embedding(prompts):
93 with torch.no_grad():
94 uc = []
95 for text in prompts:
96 tokens = tokenizer(text,
97 truncation=True,
98 max_length=77,
99 return_length=True,
100 return_overflowing_tokens=False,
101 padding="max_length",
102 return_tensors="pt")
103
104 tokens = tokens["input_ids"]
105 end = torch.nonzero(tokens == 49407)[:, 1].min()
106 if 'photo of a' in text[:15]:
107 print(text)
108 uc.append((model.get_learned_conditioning(1 * [text])[:, 4:end+1]).reshape(-1, 768))
109 else:
110 uc.append((model.get_learned_conditioning(1 * [text])[:, 1:end+1]).reshape(-1, 768))
111
112 return torch.cat(uc, 0)
113
114 tokenizer = model.cond_stage_model.tokenizer
115 embeds = []
116 count = 1
117
118 model2_sts = []
119 modifier_tokens = []
120 categories = []
121 config.model.params.cond_stage_config.params = {}
122 config.model.params.cond_stage_config.params.modifier_token = None
123 for path1, cat1 in zip(paths.split('+'), category.split('+')):
124 model2_st = torch.load(path1)
125 if 'embed' in model2_st['state_dict']:

Callers 1

composenW.pyFile · 0.70

Calls 4

get_modelFunction · 0.85
getlayersFunction · 0.85
get_text_embeddingFunction · 0.70
gdupdateWexactFunction · 0.70

Tested by

no test coverage detected