Upload README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,109 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# What the DAAM: Interpreting Stable Diffusion Using Cross Attention
|
2 |
+
|
3 |
+
[](https://huggingface.co/spaces/tetrisd/Diffusion-Attentive-Attribution-Maps) [](https://gist.github.com/daemon/639de6fea584d7df1a62f04a2ea0cdad) [](https://pypi.org/project/daam) [](https://pepy.tech/project/daam)
|
4 |
+
|
5 |
+

|
6 |
+
|
7 |
+
### Updated to support Stable Diffusion XL (SDXL) and Diffusers 0.21.1!
|
8 |
+
|
9 |
+
I regularly update this codebase. Please submit an issue if you have any questions.
|
10 |
+
|
11 |
+
In [our paper](https://aclanthology.org/2023.acl-long.310), we propose diffusion attentive attribution maps (DAAM), a cross attention-based approach for interpreting Stable Diffusion.
|
12 |
+
Check out our demo: https://huggingface.co/spaces/tetrisd/Diffusion-Attentive-Attribution-Maps.
|
13 |
+
See our [documentation](https://castorini.github.io/daam/), hosted by GitHub pages, and [our Colab notebook](https://colab.research.google.com/drive/1miGauqa07uHnDoe81NmbmtTtnupmlipv?usp=sharing), updated for v0.1.0.
|
14 |
+
|
15 |
+
## Getting Started
|
16 |
+
First, install [PyTorch](https://pytorch.org) for your platform.
|
17 |
+
Then, install DAAM with `pip install daam`, unless you want an editable version of the library, in which case do `git clone https://github.com/castorini/daam && pip install -e daam`.
|
18 |
+
Finally, login using `huggingface-cli login` to get many stable diffusion models -- you'll need to get a token at [HuggingFace.co](https://huggingface.co/).
|
19 |
+
|
20 |
+
### Running the Website Demo
|
21 |
+
Simply run `daam-demo` in a shell and navigate to http://localhost:8080.
|
22 |
+
The same demo as the one on HuggingFace Spaces will show up.
|
23 |
+
|
24 |
+
### Using DAAM as a CLI Utility
|
25 |
+
DAAM comes with a simple generation script for people who want to quickly try it out.
|
26 |
+
Try running
|
27 |
+
```bash
|
28 |
+
$ mkdir -p daam-test && cd daam-test
|
29 |
+
$ daam "A dog running across the field."
|
30 |
+
$ ls
|
31 |
+
a.heat_map.png field.heat_map.png generation.pt output.png seed.txt
|
32 |
+
dog.heat_map.png running.heat_map.png prompt.txt
|
33 |
+
```
|
34 |
+
Your current working directory will now contain the generated image as `output.png` and a DAAM map for every word, as well as some auxiliary data.
|
35 |
+
You can see more options for `daam` by running `daam -h`.
|
36 |
+
To use Stable Diffusion XL as the backend, run `daam --model xl-base-1.0 "Dog jumping"`.
|
37 |
+
|
38 |
+
### Using DAAM as a Library
|
39 |
+
|
40 |
+
Import and use DAAM as follows:
|
41 |
+
|
42 |
+
```python
|
43 |
+
from daam import trace, set_seed
|
44 |
+
from diffusers import DiffusionPipeline
|
45 |
+
from matplotlib import pyplot as plt
|
46 |
+
import torch
|
47 |
+
|
48 |
+
|
49 |
+
model_id = 'stabilityai/stable-diffusion-xl-base-1.0'
|
50 |
+
device = 'cuda'
|
51 |
+
|
52 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, use_auth_token=True, torch_dtype=torch.float16, use_safetensors=True, variant='fp16')
|
53 |
+
pipe = pipe.to(device)
|
54 |
+
|
55 |
+
prompt = 'A dog runs across the field'
|
56 |
+
gen = set_seed(0) # for reproducibility
|
57 |
+
|
58 |
+
with torch.no_grad():
|
59 |
+
with trace(pipe) as tc:
|
60 |
+
out = pipe(prompt, num_inference_steps=50, generator=gen)
|
61 |
+
heat_map = tc.compute_global_heat_map()
|
62 |
+
heat_map = heat_map.compute_word_heat_map('dog')
|
63 |
+
heat_map.plot_overlay(out.images[0])
|
64 |
+
plt.show()
|
65 |
+
```
|
66 |
+
|
67 |
+
You can also serialize and deserialize the DAAM maps pretty easily:
|
68 |
+
|
69 |
+
```python
|
70 |
+
from daam import GenerationExperiment, trace
|
71 |
+
|
72 |
+
with trace(pipe) as tc:
|
73 |
+
pipe('A dog and a cat')
|
74 |
+
exp = tc.to_experiment('experiment-dir')
|
75 |
+
exp.save() # experiment-dir now contains all the data and heat maps
|
76 |
+
|
77 |
+
exp = GenerationExperiment.load('experiment-dir') # load the experiment
|
78 |
+
```
|
79 |
+
|
80 |
+
We'll continue adding docs.
|
81 |
+
In the meantime, check out the `GenerationExperiment`, `GlobalHeatMap`, and `DiffusionHeatMapHooker` classes, as well as the `daam/run/*.py` example scripts.
|
82 |
+
You can download the COCO-Gen dataset from the paper at http://ralphtang.com/coco-gen.tar.gz.
|
83 |
+
If clicking the link doesn't work on your browser, copy and paste it in a new tab, or use a CLI utility such as `wget`.
|
84 |
+
|
85 |
+
## See Also
|
86 |
+
- [DAAM-i2i](https://github.com/RishiDarkDevil/daam-i2i), an extension of DAAM to image-to-image attribution.
|
87 |
+
|
88 |
+
- [Furkan's video](https://www.youtube.com/watch?v=XiKyEKJrTLQ) on easily getting started with DAAM.
|
89 |
+
|
90 |
+
- [1littlecoder's video](https://www.youtube.com/watch?v=J2WtkA1Xfew) for a code demonstration and Colab notebook of an older version of DAAM.
|
91 |
+
|
92 |
+
## Citation
|
93 |
+
```
|
94 |
+
@inproceedings{tang2023daam,
|
95 |
+
title = "What the {DAAM}: Interpreting Stable Diffusion Using Cross Attention",
|
96 |
+
author = "Tang, Raphael and
|
97 |
+
Liu, Linqing and
|
98 |
+
Pandey, Akshat and
|
99 |
+
Jiang, Zhiying and
|
100 |
+
Yang, Gefei and
|
101 |
+
Kumar, Karun and
|
102 |
+
Stenetorp, Pontus and
|
103 |
+
Lin, Jimmy and
|
104 |
+
Ture, Ferhan",
|
105 |
+
booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
|
106 |
+
year = "2023",
|
107 |
+
url = "https://aclanthology.org/2023.acl-long.310",
|
108 |
+
}
|
109 |
+
```
|