Skip to content

Commit 2fdd094

Browse files
Wildcard stable diffusion pipeline (#900)
* Initial Wildcard Stable Diffusion Pipeline * Added some additional example usage * style * Added links in README and additional documentation * Initial Wildcard Stable Diffusion Pipeline * Added some additional example usage * style * Added links in README and additional documentation * cleanup readme again * Apply suggestions from code review Co-authored-by: Patrick von Platen <[email protected]>
1 parent 31af4d1 commit 2fdd094

File tree

2 files changed

+476
-0
lines changed

2 files changed

+476
-0
lines changed

examples/community/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ If a community doesn't work as expected, please open an issue and ping the autho
1414
| Stable Diffusion Mega | **One** Stable Diffusion Pipeline with all functionalities of [Text2Image](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py), [Image2Image](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py) and [Inpainting](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py) | [Stable Diffusion Mega](#stable-diffusion-mega) | - | [Patrick von Platen](https://github.com/patrickvonplaten/) |
1515
| Long Prompt Weighting Stable Diffusion | **One** Stable Diffusion Pipeline without tokens length limit, and support parsing weighting in prompt. | [Long Prompt Weighting Stable Diffusion](#long-prompt-weighting-stable-diffusion) | - | [SkyTNT](https://github.com/SkyTNT) |
1616
| Speech to Image | Using automatic-speech-recognition to transcribe text and Stable Diffusion to generate images | [Speech to Image](#speech-to-image) | - | [Mikail Duzenli](https://github.com/MikailINTech)
17+
| Wild Card Stable Diffusion | Stable Diffusion Pipeline that supports prompts that contain wildcard terms (indicated by surrounding double underscores), with values instantiated randomly from a corresponding txt file or a dictionary of possible values | [Wildcard Stable Diffusion](#wildcard-stable-diffusion) | - | [Shyam Sudhakaran](https://github.com/shyamsn97) |
1718

1819
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.
1920
```py
@@ -264,3 +265,60 @@ plt.imshow(output.images[0])
264265
This example produces the following image:
265266

266267
![image](https://user-images.githubusercontent.com/45072645/196901736-77d9c6fc-63ee-4072-90b0-dc8b903d63e3.png)
268+
269+
### Wildcard Stable Diffusion
270+
Following the great examples from https://github.com/jtkelm2/stable-diffusion-webui-1/blob/master/scripts/wildcards.py and https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Custom-Scripts#wildcards, here's a minimal implementation that allows for users to add "wildcards", denoted by `__wildcard__` to prompts that are used as placeholders for randomly sampled values given by either a dictionary or a `.txt` file. For example:
271+
272+
Say we have a prompt:
273+
274+
```
275+
prompt = "__animal__ sitting on a __object__ wearing a __clothing__"
276+
```
277+
278+
We can then define possible values to be sampled for `animal`, `object`, and `clothing`. These can either be from a `.txt` with the same name as the category.
279+
280+
The possible values can also be defined / combined by using a dictionary like: `{"animal":["dog", "cat", mouse"]}`.
281+
282+
The actual pipeline works just like `StableDiffusionPipeline`, except the `__call__` method takes in:
283+
284+
`wildcard_files`: list of file paths for wild card replacement
285+
`wildcard_option_dict`: dict with key as `wildcard` and values as a list of possible replacements
286+
`num_prompt_samples`: number of prompts to sample, uniformly sampling wildcards
287+
288+
A full example:
289+
290+
create `animal.txt`, with contents like:
291+
292+
```
293+
dog
294+
cat
295+
mouse
296+
```
297+
298+
create `object.txt`, with contents like:
299+
300+
```
301+
chair
302+
sofa
303+
bench
304+
```
305+
306+
```python
307+
from diffusers import DiffusionPipeline
308+
import torch
309+
310+
pipe = DiffusionPipeline.from_pretrained(
311+
"CompVis/stable-diffusion-v1-4",
312+
custom_pipeline="wildcard_stable_diffusion",
313+
revision="fp16",
314+
torch_dtype=torch.float16,
315+
)
316+
prompt = "__animal__ sitting on a __object__ wearing a __clothing__"
317+
out = pipe(
318+
prompt,
319+
wildcard_option_dict={
320+
"clothing":["hat", "shirt", "scarf", "beret"]
321+
},
322+
wildcard_files=["object.txt", "animal.txt"],
323+
num_prompt_samples=1
324+
)

0 commit comments

Comments
 (0)