Skip to content

Commit 169fc4a

Browse files
Add Prompt2Prompt pipeline (#4563)
* Initial commit P2P * Replaced CrossAttention, added test skeleton * bug fixes * Updated docstring * Removed unused function * Created tests * improved tests - made fast inference tests faster - corrected image shape assertions * Corrected expected output shape in tests * small fix: test inputs * Update tests - used conditional unet2d - set expected image slices - edit_kwargs are now not popped, so pipe can be run multiple times * Fixed bug in int tests * Fixed tests * Linting * Create prompt2prompt.md * Added to docs toc * Ran make fix-copies * Fixed code blocks in docs * Using same interface as StableDiffusionPipeline * Fixed small test bug * Added all options SDPipeline.__call_ has * Fixed docstring; made __call__ like in SD * Linting * Added test for multiple prompts * Improved docs * Incorporated feedback * Reverted formatting on unrelated files * Moved prompt2prompt to community - Moved prompt2prompt pipeline from main to community - Deleted tests - Moved documentation to community and shorted it * Update src/diffusers/utils/dummy_torch_and_transformers_objects.py Co-authored-by: Patrick von Platen <[email protected]> --------- Co-authored-by: Patrick von Platen <[email protected]>
1 parent 566bdf4 commit 169fc4a

File tree

2 files changed

+946
-0
lines changed

2 files changed

+946
-0
lines changed

Diff for: examples/community/README.md

+87
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ If a community doesn't work as expected, please open an issue and ping the autho
4343
Stable Diffusion XL Long Weighted Prompt Pipeline | A pipeline support unlimited length of prompt and negative prompt, use A1111 style of prompt weighting | [Stable Diffusion XL Long Weighted Prompt Pipeline](#stable-diffusion-xl-long-weighted-prompt-pipeline) | - | [Andrew Zhu](https://xhinker.medium.com/) |
4444
FABRIC - Stable Diffusion with feedback Pipeline | pipeline supports feedback from liked and disliked images | [Stable Diffusion Fabric Pipline](#stable-diffusion-fabric-pipeline) | - | [Shauray Singh](https://shauray8.github.io/about_shauray/) |
4545
sketch inpaint - Inpainting with non-inpaint Stable Diffusion | sketch inpaint much like in automatic1111 | [Masked Im2Im Stable Diffusion Pipeline](#stable-diffusion-masked-im2im) | - | [Anatoly Belikov](https://github.com/noskill) |
46+
prompt-to-prompt | change parts of a prompt and retain image structure (see [paper page](https://prompt-to-prompt.github.io/)) | [Prompt2Prompt Pipeline](#prompt2prompt-pipeline) | - | [Umer H. Adil](https://twitter.com/UmerHAdil) |
4647

4748

4849
To load a custom pipeline you just need to pass the `custom_pipeline` argument to `DiffusionPipeline`, as one of the files in `diffusers/examples/community`. Feel free to send a PR with your own pipelines, we will merge them quickly.
@@ -2060,3 +2061,89 @@ result:
20602061

20612062
<img src=https://github.com/noskill/diffusers/assets/733626/23a0a71d-51db-471e-926a-107ac62512a8 width="25%" >
20622063

2064+
2065+
### Prompt2Prompt Pipeline
2066+
2067+
Prompt2Prompt allows the following edits:
2068+
- ReplaceEdit (change words in prompt)
2069+
- ReplaceEdit with local blend (change words in prompt, keep image part unrelated to changes constant)
2070+
- RefineEdit (add words to prompt)
2071+
- RefineEdit with local blend (add words to prompt, keep image part unrelated to changes constant)
2072+
- ReweightEdit (modulate importance of words)
2073+
2074+
Here's a full example for `ReplaceEdit``:
2075+
2076+
```python
2077+
import torch
2078+
import numpy as np
2079+
import matplotlib.pyplot as plt
2080+
from diffusers.pipelines import Prompt2PromptPipeline
2081+
2082+
pipe = Prompt2PromptPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to("cuda")
2083+
2084+
prompts = ["A turtle playing with a ball",
2085+
"A monkey playing with a ball"]
2086+
2087+
cross_attention_kwargs = {
2088+
"edit_type": "replace",
2089+
"cross_replace_steps": 0.4,
2090+
"self_replace_steps": 0.4
2091+
}
2092+
2093+
outputs = pipe(prompt=prompts, height=512, width=512, num_inference_steps=50, cross_attention_kwargs=cross_attention_kwargs)
2094+
```
2095+
2096+
And abbreviated examples for the other edits:
2097+
2098+
`ReplaceEdit with local blend`
2099+
```python
2100+
prompts = ["A turtle playing with a ball",
2101+
"A monkey playing with a ball"]
2102+
2103+
cross_attention_kwargs = {
2104+
"edit_type": "replace",
2105+
"cross_replace_steps": 0.4,
2106+
"self_replace_steps": 0.4,
2107+
"local_blend_words": ["turtle", "monkey"]
2108+
}
2109+
```
2110+
2111+
`RefineEdit`
2112+
```python
2113+
prompts = ["A turtle",
2114+
"A turtle in a forest"]
2115+
2116+
cross_attention_kwargs = {
2117+
"edit_type": "refine",
2118+
"cross_replace_steps": 0.4,
2119+
"self_replace_steps": 0.4,
2120+
}
2121+
```
2122+
2123+
`RefineEdit with local blend`
2124+
```python
2125+
prompts = ["A turtle",
2126+
"A turtle in a forest"]
2127+
2128+
cross_attention_kwargs = {
2129+
"edit_type": "refine",
2130+
"cross_replace_steps": 0.4,
2131+
"self_replace_steps": 0.4,
2132+
"local_blend_words": ["in", "a" , "forest"]
2133+
}
2134+
```
2135+
2136+
`ReweightEdit`
2137+
```python
2138+
prompts = ["A smiling turtle"] * 2
2139+
2140+
edit_kcross_attention_kwargswargs = {
2141+
"edit_type": "reweight",
2142+
"cross_replace_steps": 0.4,
2143+
"self_replace_steps": 0.4,
2144+
"equalizer_words": ["smiling"],
2145+
"equalizer_strengths": [5]
2146+
}
2147+
```
2148+
2149+
Side note: See [this GitHub gist](https://gist.github.com/UmerHA/b65bb5fb9626c9c73f3ade2869e36164) if you want to visualize the attention maps.

0 commit comments

Comments
 (0)