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

Split mask blur into X and Y components, patch Outpainting MK2 accordingly #10295

Merged

Conversation

Splendide-Imaginarius
Copy link
Contributor

@Splendide-Imaginarius Splendide-Imaginarius commented May 11, 2023

Describe what this pull request is trying to achieve.

The mask blur in img2img was applied equally in both X and Y dimensions. This caused a bug in the Outpainting MK2 script, where noise would be introduced near borders other than the outpainted border. This PR splits out the mask blur into X and Y dimensions, which fixes the bad behavior.

Additional notes and description of your changes

PIL doesn't support applying a Gaussian blur in a single dimension, so I replaced PIL's Gaussian blur with that of OpenCV. I picked a kernel size that should be easily large enough to avoid quality issues; it may be a tad overkill but that shouldn't hurt anything.

I suspect that the bug will remain if you instruct MK2 to outpaint in more than one dimension at once, but that's not a regression, and AFAIK most users only outpaint in one direction at a time.

I didn't expose the new functionality to anything in the UI other than Outpainting MK2, but it should be easy for someone to do so if it's desired. (I couldn't think of a reason anyone would want this other than to fix the outpainting bug, but maybe I wasn't creative enough.)

Environment this was tested in

  • OS: Windows
  • Browser: Firefox
  • Graphics card: CPU

Screenshots or videos of your changes

Sample input image: https://github.com/Splendide-Imaginarius/stable-diffusion-webui/blob/mk2-blur-mask-docs/samples/original.png

Sample left-outpainted image without this PR: https://github.com/Splendide-Imaginarius/stable-diffusion-webui/blob/mk2-blur-mask-docs/samples/outpainted-broken.png

Sample left-outpainted image with this PR: https://github.com/Splendide-Imaginarius/stable-diffusion-webui/blob/mk2-blur-mask-docs/samples/outpainted-fixed.png

Note the noise along the bottom border of the outpainted image without this PR, and note that with this PR, the bottom border is the same as that of the input image.

@AUTOMATIC1111
Copy link
Owner

This is a UI change and I don't even know what it fixes. Does user want to specify both X and Y blur normally?

@Splendide-Imaginarius
Copy link
Contributor Author

This is a UI change and I don't even know what it fixes.

As stated above, it fixes a bug where outpainting to the left or right causes noise to appear at the bottom border (not near the outpainted region); is the bug not apparent from my screenshots?

Does user want to specify both X and Y blur normally?

I have no idea whether users want to do that, but if they do, it should be an easy modification on top of this PR. My motivation was just to fix the outpainting bug.

@AUTOMATIC1111
Copy link
Owner

Never mind, my bad, I didn't notice screenshots and misunderstood what this changes.

StableDiffusionProcessingImg2Img should still accept mask_blur named arg to not fail with scripts that use it (and still work properly if it's specified) and file access flags (100644 → 100755) should not be modified.

@Splendide-Imaginarius
Copy link
Contributor Author

OK, no worries, I'll re-add the mask_blur argument for compatibility. May take me a few days to update the PR. Thanks for the review.

Prequisite to fixing Outpainting MK2 mask blur bug.
Fixes unexpected noise in non-outpainted borders when using MK2 script.
@Splendide-Imaginarius
Copy link
Contributor Author

PR updated to address review, sorry this took so long.

@AUTOMATIC1111 AUTOMATIC1111 merged commit f9606b8 into AUTOMATIC1111:dev Jun 9, 2023
@dmzkrsk
Copy link

dmzkrsk commented Jul 19, 2023

Somehow, this commit broke tile/upscaling: Coyote-A/ultimate-upscale-for-automatic1111#111

@dmzkrsk
Copy link

dmzkrsk commented Jul 19, 2023

Maybe, if some old code passes only mask_blur, but not mask_blur_x, mask_blur_y, they are initialized 4 and 4 and mask_blur always gets overwritten by x/y default values?

@Splendide-Imaginarius
Copy link
Contributor Author

Interesting, thanks for the heads up @dmzkrsk. I'll take a look and see if I maybe broke the defaults somehow.

Comment on lines -1164 to +1168
self.mask_blur = mask_blur
if mask_blur is not None:
mask_blur_x = mask_blur
mask_blur_y = mask_blur
self.mask_blur_x = mask_blur_x
self.mask_blur_y = mask_blur_y
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmzkrsk As best I can tell, this section of code should prevent the failure mode you described. Do you see anything obviously wrong here?

@catboxanon
Copy link
Collaborator

catboxanon commented Aug 11, 2023

@Splendide-Imaginarius FYI, this broke the ControlNet extension too, see my PR linked above. It was unknown for about 3 months. I did see you created a new branch here though -- do you still plan to open a PR for that here?

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

Successfully merging this pull request may close these issues.

4 participants