sd-webui-controlnet
sd-webui-controlnet copied to clipboard
Fix shuffle preprocessor seed bug & allow modifying seed with subseed
As discussed in https://github.com/Mikubill/sd-webui-controlnet/pull/742, the shuffle processor seed should be reproducible but also controllable by the user. The implementation in https://github.com/Mikubill/sd-webui-controlnet/commit/fa994bb2ffed7428f07afd423f215609c5794204 isn't ideal because when using txt2img, this means that only one variation is able to be created for a generated image.
This PR changes this to also account for the subseed (what the web UI refers to as the Variation seed). This means that a user could use this variation seed, with the Variation strength set to 0 (so it doesn't affect the base generated image), but it will affect the Shuffle preprocessor to use a different permutation. This will also work with batch sizes out of the box.
A use case for this would be using the XYZ plot script to show how the Shuffle cnet strength affects the image for multiple different variation seeds as increasing strengths, but all starting from the same generated image.
Might be something to reconsider the implementation for later once paint-with-words and GLIGEN support are added. https://github.com/Mikubill/sd-webui-controlnet/discussions/519#discussioncomment-5225020
Edit: See comment below. This now also fixes a bug in the previous implementation.
Example of use, txt2img. I used Van Gogh's Starry Night as the input image for the Shuffle cnet. Now the progression can be seen how the cnet affects the image, even when the input images remain the same, and all that changes is the Shuffle preprocessor result due to the different variation seed.

As an aside, this is definitely the best style transfer method I've used yet, it's much better than the T2I adapter. You were 100% right that all other methods before this were limited. Amazing work!
Looks like when clicking the reuse seed button, it ends up not reproducing the same preprocessor result, looking into this
Okay, this PR now actually fixes a bug in the impl. p.seed and p.subseed always return -1 when they are not explicitly set. But, p.all_seeds contains the seed(s), so now we simply take the first value. This will also make batches reproducible, however it does mean taking an image from the batch other than the 1st one won't be reproducible, unless it's part of the same batch. IIRC, the Dynamic Prompts author had ran into this issue as well and solved it by using the process_batch hook from the scripts module instead of process. This wouldn't be a huge issue if batch size and position were recorded still but unfortunately that was removed in a commit a few months back.
I've also went ahead and changed the modulo op to a bitwise op and increased it to the max value np.random.seed allows (2**32 - 1). This is also the max value the web UI actually utilizes so now there will be a random shuffle permutation for all possible seeds.
Clicking the reuse seed button now gives the expected result. I've also set the subseed value used equal to the primary seed value when the subseed is not set (is equal to -1), as clicking the reuse seed button for the subseed inputs the primary seed. Mouthful of a sentence but I hope that makes sense.
Subseed seems to be needed to be set explicitly if you want to run an XYZ plot for the same primary seed. I haven't yet looked into why exactly that is but that seems like less of an issue.
I sill have this problem, even with the latest webui + CN. Any ideas why? Even setting a variation seed (with variation set to 0) does not maintain the shuffle preprocessor stability. Thanks!
I sill have this problem, even with the latest webui + CN. Any ideas why? Even setting a variation seed (with variation set to 0) does not maintain the shuffle preprocessor stability. Thanks!
I just tested that the result for shuffle can be perfectly reproduced in lastest A1111 + ControlNet extension. Make sure to turn on debug log --controlnet-loglevel DEBUG and look for following log:
2024-04-23 19:44:07,826 - ControlNet - INFO - Loading model from cache: control_v11e_sd15_shuffle [526bfdae]
2024-04-23 19:44:07,826 - ControlNet - DEBUG - is_upscale_script=False
2024-04-23 19:44:07,828 - ControlNet - DEBUG - Use numpy seed 222.
2024-04-23 19:44:07,828 - ControlNet - INFO - Using preprocessor: shuffle
The numpy seed should be the same between runs to ensure shuffle preprocessor produce the same result.