Skip to content

Commit fa35750

Browse files
SusungHongpatrickvonplatenwilliamberman
authored
Add Self-Attention-Guided (SAG) Stable Diffusion pipeline (huggingface#2193)
* Add Stable Diffusion Sw/ elf-Attention Guidance * Modify __init__.py * Register attention storing processor * Update pipeline_stable_diffusion_sag.py * Editing default value * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update dummy_torch_and_transformers_objects.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update pipeline_stable_diffusion_sag.py * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py Co-authored-by: Patrick von Platen <[email protected]> * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Create test_stable_diffusion_sag.py * Create self_attention_guidance.py * Update pipeline_stable_diffusion_sag.py * Update test_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Rename self_attention_guidance.py to self_attention_guidance.mdx * Update self_attention_guidance.mdx * Update self_attention_guidance.mdx * Update _toctree.yml * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Fixing order * Update pipeline_stable_diffusion_sag.py * fixing import order * fix order * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * Naming change * Noting pred_x0 * Adding some fast tests * Update pipeline_stable_diffusion_sag.py * Update test_stable_diffusion_sag.py * Update test_stable_diffusion_sag.py * Update test_stable_diffusion_sag.py * Update docs/source/en/api/pipelines/stable_diffusion/self_attention_guidance.mdx * implement gaussian_blur * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py * fix tests * Update pipeline_stable_diffusion_sag.py * Update pipeline_stable_diffusion_sag.py --------- Co-authored-by: Patrick von Platen <[email protected]> Co-authored-by: Will Berman <[email protected]>
1 parent fd3d550 commit fa35750

File tree

8 files changed

+1006
-0
lines changed

8 files changed

+1006
-0
lines changed

docs/source/en/_toctree.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
title: InstructPix2Pix
154154
- local: api/pipelines/stable_diffusion/pix2pix_zero
155155
title: Pix2Pix Zero
156+
- local: api/pipelines/stable_diffusion/self_attention_guidance
157+
title: Self-Attention Guidance
156158
title: Stable Diffusion
157159
- local: api/pipelines/stable_diffusion_2
158160
title: Stable Diffusion 2
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
-->
12+
13+
# Self-Attention Guidance (SAG)
14+
15+
## Overview
16+
17+
[Self-Attention Guidance](https://arxiv.org/abs/2210.00939) by Susung Hong et al.
18+
19+
The abstract of the paper is the following:
20+
21+
*Denoising diffusion models (DDMs) have been drawing much attention for their appreciable sample quality and diversity. Despite their remarkable performance, DDMs remain black boxes on which further study is necessary to take a profound step. Motivated by this, we delve into the design of conventional U-shaped diffusion models. More specifically, we investigate the self-attention modules within these models through carefully designed experiments and explore their characteristics. In addition, inspired by the studies that substantiate the effectiveness of the guidance schemes, we present plug-and-play diffusion guidance, namely Self-Attention Guidance (SAG), that can drastically boost the performance of existing diffusion models. Our method, SAG, extracts the intermediate attention map from a diffusion model at every iteration and selects tokens above a certain attention score for masking and blurring to obtain a partially blurred input. Subsequently, we measure the dissimilarity between the predicted noises obtained from feeding the blurred and original input to the diffusion model and leverage it as guidance. With this guidance, we observe apparent improvements in a wide range of diffusion models, e.g., ADM, IDDPM, and Stable Diffusion, and show that the results further improve by combining our method with the conventional guidance scheme. We provide extensive ablation studies to verify our choices.*
22+
23+
Resources:
24+
25+
* [Project Page](https://ku-cvlab.github.io/Self-Attention-Guidance).
26+
* [Paper](https://arxiv.org/abs/2210.00939).
27+
* [Original Code](https://github.com/KU-CVLAB/Self-Attention-Guidance).
28+
* [Demo](https://colab.research.google.com/github/SusungHong/Self-Attention-Guidance/blob/main/SAG_Stable.ipynb).
29+
30+
31+
## Available Pipelines:
32+
33+
| Pipeline | Tasks | Demo
34+
|---|---|:---:|
35+
| [StableDiffusionSAGPipeline](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_sag.py) | *Text-to-Image Generation* | [Colab](https://colab.research.google.com/github/SusungHong/Self-Attention-Guidance/blob/main/SAG_Stable.ipynb) |
36+
37+
## Usage example
38+
39+
```python
40+
import torch
41+
from diffusers import StableDiffusionSAGPipeline
42+
from accelerate.utils import set_seed
43+
44+
pipe = StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
45+
pipe = pipe.to("cuda")
46+
47+
seed = 8978
48+
prompt = "."
49+
guidance_scale = 7.5
50+
num_images_per_prompt = 1
51+
52+
sag_scale = 1.0
53+
54+
set_seed(seed)
55+
images = pipe(
56+
prompt, num_images_per_prompt=num_images_per_prompt, guidance_scale=guidance_scale, sag_scale=sag_scale
57+
).images
58+
images[0].save("example.png")
59+
```
60+
61+
## StableDiffusionSAGPipeline
62+
[[autodoc]] StableDiffusionSAGPipeline
63+
- __call__
64+
- all

src/diffusers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
StableDiffusionPipeline,
120120
StableDiffusionPipelineSafe,
121121
StableDiffusionPix2PixZeroPipeline,
122+
StableDiffusionSAGPipeline,
122123
StableDiffusionUpscalePipeline,
123124
StableUnCLIPImg2ImgPipeline,
124125
StableUnCLIPPipeline,

src/diffusers/pipelines/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
StableDiffusionLatentUpscalePipeline,
5656
StableDiffusionPipeline,
5757
StableDiffusionPix2PixZeroPipeline,
58+
StableDiffusionSAGPipeline,
5859
StableDiffusionUpscalePipeline,
5960
StableUnCLIPImg2ImgPipeline,
6061
StableUnCLIPPipeline,

src/diffusers/pipelines/stable_diffusion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class StableDiffusionPipelineOutput(BaseOutput):
4444
from .pipeline_stable_diffusion_inpaint_legacy import StableDiffusionInpaintPipelineLegacy
4545
from .pipeline_stable_diffusion_instruct_pix2pix import StableDiffusionInstructPix2PixPipeline
4646
from .pipeline_stable_diffusion_latent_upscale import StableDiffusionLatentUpscalePipeline
47+
from .pipeline_stable_diffusion_sag import StableDiffusionSAGPipeline
4748
from .pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
4849
from .pipeline_stable_unclip import StableUnCLIPPipeline
4950
from .pipeline_stable_unclip_img2img import StableUnCLIPImg2ImgPipeline

0 commit comments

Comments
 (0)