stable-diffusion-webui icon indicating copy to clipboard operation
stable-diffusion-webui copied to clipboard

[Feature Request]: Allow step reduction in hires fix

Open lihaoyun6 opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues and checked the recent builds/commits

What would your feature do ?

In the img2img, usually the webui will do less with less denoising (compared to user setting). Unless we enable "With img2img, do exactly the amount of steps the slider specifies".

This is good because you don't need to waste too many iterations on the weaker Denoising strength. But this function will not work in hires fix.

If we set the Sampling steps to 20 and the Denoising strength to 0.4. It should iterate 20+(20*0.4)=28 times. But webui will do 20+20=40 times...

Proposed workflow

I found this function in modules/processing.py line 941, just change this line from:

samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning)

to:

samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps if opts.img2img_fix_steps else None, image_conditioning=image_conditioning)

Added an additional judgment condition, it will perfectly comply with the "With img2img, do exactly the amount of steps the slider specifies" setting item.

Then we need to fix the progress bar at around line 851...

Additional information

No response

lihaoyun6 avatar Apr 18 '23 18:04 lihaoyun6

The tooltip even explicitly says it should reduce the number of steps, which makes this a bug:

Determines how little respect the algorithm should have for image's content. At 0, nothing will change, and at 1 you'll get an unrelated image. With values below 1.0, processing will take less steps than the Sampling Steps slider specifies.

feffy380 avatar Jan 19 '25 08:01 feffy380

@feffy380

I found a better way to to calculate a number of Hires Steps:

$HS = \lceil \frac{s \cdot d}{\log_{10}{s}} \rceil$

$HS :$ Hires Steps $s :$ Sampling Steps
$d :$ Denoising Strength

For example, set Sampling Steps to 30 with Denoising Strength of 0.7, you will get:

$\lceil \frac{30 \cdot 0.7}{\log_{10}{30}} \rceil = 15$

Compared to $\lceil s \cdot d \rceil = 21$, the image is already good enough at 15 steps.


And I made a one-line script to automatically calculate the Hires Steps.
All you have to do is bookmark it and click it before generating.
It will calculate and fill in the steps into the WebUI.

javascript: (function() {const c1 = document.getElementById("txt2img_steps");const i1 = c1.querySelector('input[type="number"]');const c2 = document.getElementById("txt2img_denoising_strength");const i2 = c2.querySelector('input[type="number"]');const c3 = document.getElementById("txt2img_hires_steps");const i3 = c3.querySelector('input[type="number"]');i3.value = Math.ceil((i2.value * i1.value) / Math.log10(i1.value));i3.dispatchEvent(new Event("input"))})()

lihaoyun6 avatar Jan 19 '25 11:01 lihaoyun6