|
| 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 | +# Weighting prompts |
| 14 | + |
| 15 | +Text-guided diffusion models generate images based on a given text prompt. The text prompt |
| 16 | +can include multiple concepts that the model should generate and it's often desirable to weight |
| 17 | +certain parts of the prompt more or less. |
| 18 | + |
| 19 | +Diffusion models work by conditioning the cross attention layers of the diffusion model with contextualized text embeddings (see the [Stable Diffusion Guide for more information](../stable-diffusion)). |
| 20 | +Thus a simple way to emphasize (or de-emphasize) certain parts of the prompt is by increasing or reducing the scale of the text embedding vector that corresponds to the relevant part of the prompt. |
| 21 | +This is called "prompt-weighting" and has been a highly demanded feature by the community (see issue [here](https://github.com/huggingface/diffusers/issues/2431)). |
| 22 | + |
| 23 | +## How to do prompt-weighting in Diffusers |
| 24 | + |
| 25 | +We believe the role of `diffusers` is to be a toolbox that provides essential features that enable other projects, such as [InvokeAI](https://github.com/invoke-ai/InvokeAI) or [diffuzers](https://github.com/abhishekkrthakur/diffuzers), to build powerful UIs. In order to support arbitrary methods to manipulate prompts, `diffusers` exposes a [`prompt_embeds`](https://huggingface.co/docs/diffusers/v0.14.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.__call__.prompt_embeds) function argument to many pipelines such as [`StableDiffusionPipeline`], allowing to directly pass the "prompt-weighted"/scaled text embeddings to the pipeline. |
| 26 | + |
| 27 | +The [compel library](https://github.com/damian0815/compel) provides an easy way to emphasize or de-emphasize portions of the prompt for you. We strongly recommend it instead of preparing the embeddings yourself. |
| 28 | + |
| 29 | +Let's look at a simple example. Imagine you want to generate an image of `"a red cat playing with a ball"` as |
| 30 | +follows: |
| 31 | + |
| 32 | +```py |
| 33 | +from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler |
| 34 | + |
| 35 | +pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4") |
| 36 | +pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) |
| 37 | + |
| 38 | +prompt = "a red cat playing with a ball" |
| 39 | + |
| 40 | +generator = torch.Generator(device="cpu").manual_seed(33) |
| 41 | + |
| 42 | +image = pipe(prompt, generator=generator, num_inference_steps=20).images[0] |
| 43 | +image |
| 44 | +``` |
| 45 | + |
| 46 | +This gives you: |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +As you can see, there is no "ball" in the image. Let's emphasize this part! |
| 51 | + |
| 52 | +For this we should install the `compel` library: |
| 53 | + |
| 54 | +``` |
| 55 | +pip install compel |
| 56 | +``` |
| 57 | + |
| 58 | +and then create a `Compel` object: |
| 59 | + |
| 60 | +```py |
| 61 | +from compel import Compel |
| 62 | + |
| 63 | +compel_proc = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder) |
| 64 | +``` |
| 65 | + |
| 66 | +Now we emphasize the part "ball" with the `"++"` syntax: |
| 67 | + |
| 68 | +```py |
| 69 | +prompt = "a red cat playing with a ball++" |
| 70 | +``` |
| 71 | + |
| 72 | +and instead of passing this to the pipeline directly, we have to process it using `compel_proc`: |
| 73 | + |
| 74 | +```py |
| 75 | +prompt_embeds = compel_proc(prompt) |
| 76 | +``` |
| 77 | + |
| 78 | +Now we can pass `prompt_embeds` directly to the pipeline: |
| 79 | + |
| 80 | +```py |
| 81 | +generator = torch.Generator(device="cpu").manual_seed(33) |
| 82 | + |
| 83 | +images = pipe(prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=20).images[0] |
| 84 | +image |
| 85 | +``` |
| 86 | + |
| 87 | +We now get the following image which has a "ball"! |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +Similarly, we de-emphasize parts of the sentence by using the `--` suffix for words, feel free to give it |
| 92 | +a try! |
| 93 | + |
| 94 | +If your favorite pipeline does not have a `prompt_embeds` input, please make sure to open an issue, the |
| 95 | +diffusers team tries to be as responsive as possible. |
| 96 | + |
| 97 | +Also, please check out the documentation of the [compel](https://github.com/damian0815/compel) library for |
| 98 | +more information. |
0 commit comments