Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: step by step diffusion #4709

Closed
1 task done
Ehplodor opened this issue Nov 14, 2022 · 7 comments
Closed
1 task done

[Feature Request]: step by step diffusion #4709

Ehplodor opened this issue Nov 14, 2022 · 7 comments

Comments

@Ehplodor
Copy link

Is there an existing issue for this?

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

What would your feature do ?

Sometimes, i get the feeling that diffusion is going in the right direction, preview images are good for a few steps, then some steps later the preview is completely unrecognizable. That is quite disturbing. I believe the sheduled step is too high, and that it crushes the current latents. Therefore, I would like to run diffusion step by step, recording latents in the meantime, in order to be able to come back to an earlier version and altering the scheduling (lowering its value dynamically) in order to better control the diffusion process.

Proposed workflow

  1. Go to img2img
  2. activate "1-step diffusion" option
  3. click "run 1-step" ... n times
  4. if needed, adjust diffusion parameters (CFG, denoising strength, ?scheduler?, prompt, ...)
  5. continue until the end or until user decides to stop.

Additional information

Relevant (same subject) discussion "image Generate debugger" proposed by @aifartist : #3396

Issue showing sometimes (last image) the step is not optimal, even undesirable : #3483

Here, sometimes the preview images do not change much and it could be desireable to effectively stop generation earlier : #2472

Further discussion about steps in general : #1113 (comment)

@ClashSAN
Copy link
Collaborator

I think this sort of thing should be built on the "process_one" commit by @macrosoft
a9e9799
I do not know any scripts/extensions using it yet.

@Ehplodor
Copy link
Author

I think this sort of thing should be built on the "process_one" commit by @macrosoft a9e9799 I do not know any scripts/extensions using it yet.

TY @ClashSAN I knew there had been some work in this direction but couldn't find it when searching for relevant info.

@toriato
Copy link

toriato commented Nov 14, 2022

import os

from modules import scripts
from modules.sd_samplers import KDiffusionSampler, sample_to_image

orig_callback_state = KDiffusionSampler.callback_state


def callback_state(self: KDiffusionSampler, d: dict) -> None:
    '''
    callback from k-diffusion sampling methods
    https://github.com/crowsonkb/k-diffusion/blob/60e5042ca0da89c14d1dd59d73883280f8fce991/k_diffusion/sampling.py#L67
    '''
    current_step = d['i']
    latent = d["denoised"]

    image = sample_to_image(latent)
    image.save(os.path.join(scripts.basedir(), f"step_{current_step}.png"))

    return orig_callback_state(self, d)


setattr(KDiffusionSampler, "callback_state", callback_state)


class Script (scripts.Script):
    def title(self):
        return "Step by step"

    def show(self, _):
        return scripts.AlwaysVisible

This code might be helpful to make extension.

@Ehplodor
Copy link
Author

TY. I looked at your example and other scripts as well for more info however I just don't understand most of it. Frankly speaking, I would really like to help but I can't with my present knowledge. Else I would already have made a PR for this feature as well a a few others. All I can do for the moment is, just like most of us that are more "user" than "devs", give some brain-time to propose ideas that could eventually be implemented, or maybe not ;-) Hope that helps, too.

@aifartist
Copy link

aifartist commented Nov 18, 2022 via email

@aifartist
Copy link

aifartist commented Nov 18, 2022 via email

kevinschaul added a commit to kevinschaul/stable-diffusion-webui that referenced this issue Dec 5, 2022
Combines a few different scripts in various GitHub issues with some
useful added features:

- This script is set to `scripts.AlwaysVisible`, meaning it can be run
  in combination with other scripts
- Intermediate images are saved using the same filename structure as the
  final images, in a subdirectory called "intermediates". The custom script
  in the wiki overwrites your intermediate images each time you generate
  new images
- Allows you to save every N images (defaults to 5)
- Allows you to save either denoised or noisy intermediate images

See AUTOMATIC1111#1026
See AUTOMATIC1111#2137
See AUTOMATIC1111#2094
See AUTOMATIC1111#2739
See AUTOMATIC1111#4709
kevinschaul added a commit to kevinschaul/stable-diffusion-webui that referenced this issue Dec 6, 2022
Combines a few different scripts in various GitHub issues with some
useful added features:

- This script is set to `scripts.AlwaysVisible`, meaning it can be run
  in combination with other scripts
- Intermediate images are saved using the same filename structure as the
  final images, in a subdirectory called "intermediates". The custom script
  in the wiki overwrites your intermediate images each time you generate
  new images
- Allows you to save every N images (defaults to 5)
- Allows you to save either denoised or noisy intermediate images

See AUTOMATIC1111#1026
See AUTOMATIC1111#2137
See AUTOMATIC1111#2094
See AUTOMATIC1111#2739
See AUTOMATIC1111#4709
@ClashSAN
Copy link
Collaborator

#5905 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants