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

Function show_cross_attention

sample.py:257–295  ·  view source on GitHub ↗
(prompts, tokenizer, save_path, base_count, attention_store: AttentionStore, res: int, from_where: List[str], select: int = 0)

Source from the content-addressed store, hash-verified

255 return out_all_step
256
257def show_cross_attention(prompts, tokenizer, save_path, base_count, attention_store: AttentionStore, res: int, from_where: List[str], select: int = 0):
258 tokens = tokenizer.encode(prompts[select])
259 decoder = tokenizer.decode
260
261 attention_maps = aggregate_attention_all(prompts, attention_store, res, from_where, True, select, len(tokens))
262 images = []
263 for i in range(len(tokens)):
264 image = attention_maps[:, :, i]
265 image = 255 * image / image.max()
266 image[image < 0] = 0 # process the overflow
267 image = image.unsqueeze(-1).expand(*image.shape, 3)
268 image = image.numpy().astype(np.uint8)
269 image = np.array(Image.fromarray(image).resize((256, 256)))
270 # tmp_image = Image.fromarray(image)
271 # tmp_image.save(os.path.join(save_path, '{}_{}.png'.format(base_count, decoder(int(tokens[i])))))
272 text_under_image_string = decoder(int(tokens[i]))
273 if text_under_image_string == '<new1>':
274 text_under_image_string = 'V1*'
275 elif text_under_image_string == '<new2>':
276 text_under_image_string = 'V2*'
277 image = text_under_image(image, text_under_image_string)
278 # image = text_under_image(image, decoder(int(tokens[i])))
279
280 images.append(image)
281 view_images(np.stack(images, axis=0), save_path, base_count)
282
283 if attention_store.all_step_attention_store:
284 attention_maps_all_step = aggregate_attention_all_step(prompts, attention_store, res, from_where, True, select, len(tokens))
285 for step, attention_maps in attention_maps_all_step.items():
286 images = []
287 for i in range(len(tokens)):
288 image = attention_maps[:, :, i]
289 image = 255 * image / image.max()
290 image = image.unsqueeze(-1).expand(*image.shape, 3)
291 image = image.numpy().astype(np.uint8)
292 image = np.array(Image.fromarray(image).resize((256, 256)))
293 image = text_under_image(image, decoder(int(tokens[i])))
294 images.append(image)
295 view_images(np.stack(images, axis=0), save_path, base_count, step=step)
296
297
298def main():

Callers 1

mainFunction · 0.85

Calls 5

aggregate_attention_allFunction · 0.85
text_under_imageFunction · 0.70
view_imagesFunction · 0.70
encodeMethod · 0.45

Tested by

no test coverage detected