hnmr293 / sd-webui-cutoff

Cutoff - Cutting Off Prompt Effect
Other
1.2k stars 85 forks source link

Cutoff - Cutting Off Prompt Effect

cover

Update Info Upper is newer.
026ff95a492a533a4a6e5fb2959c2324258c232c
SDXL support.
527ed922b2c4f8d2620376589dfce0f9f4b622ad
Add support for the newer version of WebUI.
20e87ce264338b824296b7559679ed1bb0bdacd7
Skip empty targets.
03bfe60162ba418e18dbaf8f1b9711fd62195ef3
Add Disable for Negative prompt option. Default is True.
f0990088fed0f5013a659cacedb194313a398860
Accept an empty prompt.

What is this?

This is an extension for stable-diffusion-webui which limits the tokens' influence scope.

SDv1, SDv2 and SDXL are supported.

Usage

  1. Select Enabled checkbox.
  2. Input words which you want to limit scope in Target tokens.
  3. Generate images.

Note

If the generated image was corrupted or something like that, try to change the Weight value or change the interpolation method to SLerp. Interpolation method can be found in Details.

Details section

Disable for Negative prompt.
If enabled, Cutoff will not work for the negative prompt. Default is true.
Cutoff strongly.
See description below. Default is false.
Interpolation method
How "padded" and "original" vectors will be interpolated. Default is Lerp.
Padding token
What token will be padded instead of Target tokens. Default is _ (underbar).

Examples

SDv1

7th_anime_v3_A-fp16 / kl-f8-anime2 / DPM++ 2M Karras / 15 steps / 512x768
Prompt: a cute girl, white shirt with green tie, red shoes, blue hair, yellow eyes, pink skirt
Negative Prompt: (low quality, worst quality:1.4), nsfw
Target tokens: white, green, red, blue, yellow, pink

Sample 1.

sample 1

Sample 2. (use SLerp for interpolation)

sample 2

Sample 3.

sample 3

SDXL

It seems that the Stability AI's base model of SDXL is much improved on token separation. So the effect of cutoff is limited.

(some models) / sdxl_vae / DPM++ 3M SDE / 50 steps / 768x1344
Prompt: full body shot of a cute girl, wearing white shirt with green tie, red shoes, blue hair, yellow eyes, pink skirt
Negative Prompt: (low quality, worst quality:1.4), nsfw, close up
Target tokens: white, green, red, blue, yellow, pink
Cutoff weight: 1.0

Sample 4. (Model = sd_xl_base_1.0)

sample 4

Sample 5. (Model = hassakuXLSfwNsfw_alphaV07)

sample 5

How it works

or see #5.

idea

Japanese

プロンプトをCLIPに通して得られる (77, 768) 次元の埋め込み表現(?正式な用語は分かりません)について、 ごく単純には、77個の行ベクトルはプロンプト中の75個のトークン(+開始トークン+終了トークン)に対応していると考えられる。

※上図は作図上、この説明とは行と列を入れ替えて描いている。

このベクトルには単語単体の意味だけではなく、文章全体の、例えば係り結びなどの情報を集約したものが入っているはずである。

ここで a cute girl, pink hair, red shoes というプロンプトを考える。 普通、こういったプロンプトの意図は

  1. pinkhair だけに係っており shoes には係っていない。
  2. 同様に redhair には係っていない。
  3. a cute girl は全体に係っていて欲しい。hairshoes は女の子に合うものが出て欲しい。

……というもののはずである。

しかしながら、EvViz2 などでトークン間の関係を見ると、そううまくはいっていないことが多い。 つまり、shoes の位置のベクトルに pink の影響が出てしまっていたりする。

一方で上述の通り a cute girl の影響は乗っていて欲しいわけで、どうにかして、特定のトークンの影響を取り除けるようにしたい。

この拡張では、指定されたトークンを padding token に書き換えることでそれを実現している。

たとえば red shoes の部分に対応して a cute girl, _ hair, red shoes というプロンプトを生成する。redshoes に対応する位置のベクトルをここから生成したもので上書きしてやることで、pink の影響を除外している。

これを pink の側から見ると、自分の影響が pink hair の範囲内に制限されているように見える。What is this? の "limits the tokens' influence scope" はそういう意味。

ところで a cute girl の方は、pink hair, red shoes の影響を受けていてもいいし受けなくてもいいような気がする。 そこでこの拡張では、こういうどちらでもいいプロンプトに対して

  1. a cute girl, pink hair, red shoes
  2. a cute girl, _ hair, _ shoes

のどちらを適用するか選べるようにしている。DetailsCutoff strongly がそれで、オフのとき1.を、オンのとき2.を、それぞれ選ぶようになっている。 元絵に近いのが出るのはオフのとき。デフォルトもこちらにしてある。

English

NB. The following text is a translation of the Japanese text above by DeepL.

For the (77, 768) dimensional embedded representation (I don't know the formal terminology), one could simply assume that the 77 row vectors correspond to the 75 tokens (+ start token and end token) in the prompt.

Note: The above figure is drawn with the rows and columns interchanged from this explanation.

This vector should contain not only the meanings of individual words, but also the aggregate information of the whole sentence, for example, the connection between words.

Consider the prompt a cute girl, pink hair, red shoes. Normally, the intent of such a prompt would be

However, when we look at the relationship between tokens in EvViz2 and other tools, we see that it is not always that way. In other words, the position vector of the shoes may be affected by pink.

On the other hand, as mentioned above, we want the influence of a cute girl to be present, so we want to be able to somehow remove the influence of a specific token.

This extension achieves this by rewriting the specified tokens as a padding token.

For example, for the red shoes part, we generate the prompt a cute girl, _ hair, red shoes, and by overwriting the position vectors corresponding to red and shoes with those generated from this prompt, we remove the influence of pink.

From pink's point of view, it appears that its influence is limited to the pink hair's scope.

By the way, a cute girl may or may not be influenced by pink hair and red shoes. So, in this extension, for such a prompt that can be either

  1. a cute girl, pink hair, red shoes
  2. a cute girl, _ hair, _ shoes

The Cutoff strongly in the Details section allows you to select 1 when it is off and 2 when it is on. The one that comes out closer to the original image is "off". The default is also set this way.