File size: 2,021 Bytes
19b7fe7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
license: 
- apache-2.0
language:
- en
tags:
- Diffusion Models
- Stable Diffusion
- Perturbed-Attention Guidance
- PAG
---
# Perturbed-Attention Guidance for SD 1.5 (i2i)

The original Perturbed-Attention Guidance for unconditional models and SD1.5 by [Hyoungwon Cho](https://huggingface.co/hyoungwoncho) is availiable at [hyoungwoncho/sd_perturbed_attention_guidance](https://huggingface.co/hyoungwoncho/sd_perturbed_attention_guidance)

[Project](https://ku-cvlab.github.io/Perturbed-Attention-Guidance/) / [arXiv](https://arxiv.org/abs/2403.17377) / [GitHub](https://github.com/KU-CVLAB/Perturbed-Attention-Guidance)

This repository is just a simple implementation of the Perturbed-Attention Guidance (PAG) on Stable Diffusion 1.5 (SD 1.5) for the 🧨 diffusers library to "image-to-image".


## Quickstart

Loading Custom Pipeline:

```py
from diffusers import StableDiffusionImg2ImgPipeline
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    custom_pipeline="jyoung105/sd15_perturbed_attention_guidance_i2i",
    torch_dtype=torch.float16
)
device="cuda"
pipe = pipe.to(device)
```

Unconditional sampling with PAG:
![image/jpeg](example_1.jpg)

```py
output = pipe(
        "",
        image=init_image,
        strength=0.6,
        num_inference_steps=50,
        guidance_scale=0.0,
        pag_scale=5.0,
        pag_applied_layers_index=['m0']
    ).images
```

Sampling with PAG and CFG:
![image/jpeg](example_2.jpg)
```py
output = pipe(
        "A man with hoodie on is looking at sky, photo",
        image=init_image,
        strength=0.6,
        num_inference_steps=50,
        guidance_scale=0.0,
        pag_scale=5.0,
        pag_applied_layers_index=['m0']
    ).images
```

## Parameters

`guidance_scale` : guidance scale of CFG (ex: `7.5`)

`pag_scale` : guidance scale of PAG (ex: `4.0`)

`pag_applied_layers`: layer to apply perturbation (ex: ['mid'])

`pag_applied_layers_index` : index of the layers to apply perturbation (ex: ['m0', 'm1'])