stable-diffusion-NPW/README.md

5.7 KiB

Negative Prompt Weight

This is a simple extension for the Stable Diffusion Web UI, which allows users to adjust the overall weight of the negative prompt, allowing you to increase or decrease its effect in a new way. Oh, and it writes the value to PNGinfo, honors it during 'send to txt2img' etc., and supports XYZ Plot.

Updates

  • 2023-04-04 - Better UX: Brought back the slider, albeit a special slider, with a custom number input with unlimited range and proper decimal display, a reset button, and snappy responses made possible by using Gradio's custom JS feature. For this, I had to force the extension's accordion to be always open.

  • 2023-04-04 - Not Even a Slider: Replaced the slider with a number input so any number can be set, cuz then you can do the crazy stuff! It also helps with the responsiveness of the UI feedback because the change/release listeners on the version of Gradio that sd-webui uses right now are useless.

  • 2023-04-04 - UI Feedback: I kept leaving NPW on a low value and forgetting about it, then went nuts trying to figure out why my prompts were not working. So I added some UI feedback both in the console (a warning will be printed to console at runtime if NPW is not 1) and in the webUI (a color outline for the negative prompt token counter).

  • 2023-04-03 - Long Prompts: Fixed the initial limitation caused by me having forgotten prompts can be longer than 77 tokens.

What Does It Do?

Here's a demonstration of how it can continously reduce the effect of the negative prompt from what you normally get (on the right, with weight 1.0) to nothing, as if the negative prompt was empty (on the left, with weight 0.0):

Another example plot showing the effect of different weights Prompt: portrait of zimby anton fadeev cyborg propaganda poster
Params: Steps: 30, Sampler: DPM++ SDE Karras, CFG scale: 7.5, Seed: 918, Size: 512x640, Model: deliberate_v2
Negative Prompt: Male

Another example plot showing the effect of different weights Prompt: portrait of zimby anton fadeev cyborg propaganda poster
Params: Steps: 30, Sampler: DPM++ SDE Karras, CFG scale: 7.5, Seed: 918, Size: 512x640, Model: deliberate_v2
Negative Prompt: Female

Why Use This?

This method was originally intended for decreasing the effect of the negative prompt, which is very hard or at times impossible to do with the currently available methods like Better Prompting™, Attention/Emphasis (using the '(prompt:weight)' syntax), Prompt Editing (using the [prompt1:prompt2:when] syntax), etc. But you can also use it with values higher than 1 and it will boost your negative prompt in its own style (you might need to lower your CFG scale a bit if you do that).

Here is the first example compared to using the '(negative prompts: weight)' syntax (i.e. bottom row is (negative prompt:0),(negative prompt:0.25),etc.:

portrait of zimby anton fadeev cyborg propaganda poster-24-male

Please have a look at the examples in the comparisons section if you want to know how it's different from using '(prompt:weight)' and check out the discussion here if you need more context.

Installation

Open SD WebUI > Go to Extensions tab > Go to Available > Press the big button > Find 'Negative Prompt Weight' in the list > Click Install

Or manually clone this repo into your extensions folder:

git clone "https://github.com/muerrilla/stable-diffusion-NPW" extensions/stable-diffusion-NPW

Usage

After installing, you can locate the new parameter "Negative Prompt Weight" in the extentions area of txt2img and img2img tabs.

Screenshot of the slider provided by the extension in UI

More Comparisons and Stuff

Here are some comparisons between NPW and Attention/Emphasis. So, top row is using NPW and bottom row is using the (Negative Prompt: weight) syntax with the same weights.

Prompts: a close up portrait of a cyberpunk [knight|lobster], [lobster| ] armour, cyberpunk!, fantasy, elegant, digital painting, artstation, concept art, matte, sharp focus, art by josan gonzalez

Params: Steps: 30, Sampler: DPM++ 2M Karras, CFG scale: 10, Seed: 6, Size: 512x640, Model: deliberate_v2

a close up portrait of a cyberpunk knight-2-red Negative Prompt: red

a close up portrait of a cyberpunk knight-25-samurai pink cg Negative Prompt: samurai pink cg

a close up portrait of a cyberpunk knight-42 Negative Prompt: custom TI embedding

How It's Done

At runtime a new learned conditioning tensor empty_uncond is made from an empty prompt. Then at every step, inside the denoiser callback, the scheduled uncond tensor of the denoiser (which is based on whatever prompt hijinks were passed to the parser) is lerped with the empty_uncond to weaken it's effect. The lerp function can instead be given a parameter bigger than 1, and it will boost the effect of the negative prompt like the CFG scale does for the positive.