
我定期更新此代碼庫。如果您有任何疑問,請提交問題。
在我們的論文中,我們提出了擴散專注歸因圖(DAAM),這是一種基於跨注意力的方法來解釋穩定擴散。請查看我們的演示:https://huggingface.co/spaces/tetrisd/diffusion-diffusion-antentive-attribution-maps。請參閱我們的文檔,該文檔由GitHub頁面託管,以及我們的COLAB筆記本,該文檔已更新為v0.1.0。
首先,為您的平台安裝Pytorch。然後,使用git clone https://github.com/castorini/daam && pip install -e daam pip install daam 。最後,使用huggingface-cli login登錄以獲取許多穩定的擴散模型 - 您需要在huggingface.co上獲得令牌。
只需在外殼中運行daam-demo ,然後導航到http:// localhost:8080。與擁抱面空間上的演示相同。
DAAM帶有一個簡單的一代腳本,適合想要快速嘗試的人。嘗試跑步
$ mkdir -p daam-test && cd daam-test
$ daam " A dog running across the field. "
$ ls
a.heat_map.png field.heat_map.png generation.pt output.png seed.txt
dog.heat_map.png running.heat_map.png prompt.txt現在,您當前的工作目錄將包含每個單詞的生成圖像作為output.png和DAAM地圖以及一些輔助數據。您可以通過運行daam -h查看daam的更多選項。要使用穩定的擴散XL作為後端,請運行daam --model xl-base-1.0 "Dog jumping" 。
導入並使用DAAM如下:
from daam import trace , set_seed
from diffusers import DiffusionPipeline
from matplotlib import pyplot as plt
import torch
model_id = 'stabilityai/stable-diffusion-xl-base-1.0'
device = 'cuda'
pipe = DiffusionPipeline . from_pretrained ( model_id , use_auth_token = True , torch_dtype = torch . float16 , use_safetensors = True , variant = 'fp16' )
pipe = pipe . to ( device )
prompt = 'A dog runs across the field'
gen = set_seed ( 0 ) # for reproducibility
with torch . no_grad ():
with trace ( pipe ) as tc :
out = pipe ( prompt , num_inference_steps = 50 , generator = gen )
heat_map = tc . compute_global_heat_map ()
heat_map = heat_map . compute_word_heat_map ( 'dog' )
heat_map . plot_overlay ( out . images [ 0 ])
plt . show ()您也可以輕鬆地序列化並逐漸闡明DAAM地圖:
from daam import GenerationExperiment , trace
with trace ( pipe ) as tc :
pipe ( 'A dog and a cat' )
exp = tc . to_experiment ( 'experiment-dir' )
exp . save () # experiment-dir now contains all the data and heat maps
exp = GenerationExperiment . load ( 'experiment-dir' ) # load the experiment我們將繼續添加文檔。同時,請查看GenerationExperiment , GlobalHeatMap和DiffusionHeatMapHooker類別”,以及daam/run/*.py示例腳本。您可以通過http://ralphtang.com/coco-gen.tar.gz從紙上下載可可基因數據集。如果單擊鏈接在瀏覽器上無法使用,請將其複制並粘貼到新的選項卡中,或使用諸如wget之類的CLI實用程序。
DAAM-I2I,DAAM到圖像到圖像歸因的擴展。
Furkan的視頻很容易開始使用DAAM。
1 LittleCoder的代碼演示的視頻和舊版本的DAAM的COLAB筆記本。
@inproceedings{tang2023daam,
title = "What the {DAAM}: Interpreting Stable Diffusion Using Cross Attention",
author = "Tang, Raphael and
Liu, Linqing and
Pandey, Akshat and
Jiang, Zhiying and
Yang, Gefei and
Kumar, Karun and
Stenetorp, Pontus and
Lin, Jimmy and
Ture, Ferhan",
booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
year = "2023",
url = "https://aclanthology.org/2023.acl-long.310",
}