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

fix: getting rid of black borders in inpainting and img2img modes #29

Merged
merged 2 commits into from
Sep 7, 2024

Conversation

zsxkib
Copy link
Contributor

@zsxkib zsxkib commented Sep 7, 2024

fixing black borders in inpainting and img2img

found a bug causing black rectangles in inpainting and img2img. it happened because we weren't resizing images consistently. here's how i fixed it:

what i did

  • switched to Image.resize() instead of our old cropping or thumbnail() stuff
  • now i resize input images to the next multiple of 16, always rounding up
  • this way, we get the same dimensions every time without losing info or adding extra space

why i picked resizing

@andreas asked if we could crop to smaller multiples of 16 instead of resizing. i tried that and a bunch of other stuff:

  • cropping to smaller 16 multiples: didn't lose quality, but sometimes cut off important parts
  • thumbnail(): saved memory, but results were inconsistent and sometimes looked bad
  • padding to next 16 multiple: left us with those annoying black borders
  • custom cropping algorithms: either lost image info or were too complex

after all that, i went back to resize(). it's not perfect (uses more memory, since it's a copy of the img), but it looked best and worked most consistently for all kinds of images

things i tried

did a ton of testing before settling on resizing. here's what i looked at:

  • thumbnail() with different sampling methods
  • writing my own cropping code
  • various ways to add blank space
  • mixing cropping and padding
  • other image processing libraries
  • trying to make the model work with non-16 multiple sizes (didn't work)

each had its own problems. some made images look worse, some couldn't get the size right, some added weird artifacts. resizing isn't perfect (uses more memory), but it worked best overall

test results

ran some tests to make sure the fix works. here's what happened:

Running Test 1: Basic img2img
Running prediction...
Using seed: 18456
Prompt: a MNCRFTMOV film render of a handsome southasian (brown) man taking a mirror selfie
[!] Resizing input image from 628x620 to 640x624
[!] img2img mode
[!] Using dev model for img2img
Using dev model
Written output to output.0.png
Written output to output.1.png
Written output to output.2.png
Written output to output.3.png

Running Test 2: Inpainting test
Running prediction...
Using seed: 63158
Prompt: a MNCRFTMOV film render of a tiny cat on a bench
[!] Resizing input image from 512x512 to 512x512
[!] inpaint mode
[!] Using dev model for inpainting
Using dev model
Written output to output.0.png
Written output to output.1.png
Written output to output.2.png
Written output to output.3.png

********************************************************************************
***** TEST RESULTS *****
********************************************************************************
Test: Test 1: Basic img2img
Status: PASS

Input:  selfie.png (628, 620)
Output: output.0.png (640, 624)
Adjustment: Width: +12, Height: +4
(Rounded up to nearest multiple of 16)

Input:  selfie.png (628, 620)
Output: output.1.png (640, 624)
Adjustment: Width: +12, Height: +4
(Rounded up to nearest multiple of 16)

Input:  selfie.png (628, 620)
Output: output.2.png (640, 624)
Adjustment: Width: +12, Height: +4
(Rounded up to nearest multiple of 16)

Input:  selfie.png (628, 620)
Output: output.3.png (640, 624)
Adjustment: Width: +12, Height: +4
(Rounded up to nearest multiple of 16)

********************************************************************************
***** ALL TESTS PASSED *****
********************************************************************************

these tests show my resizing approach works with different input sizes. it always gives output images that are multiples of 16 in both directions. the fix works for both img2img and inpainting, and it gets rid of the black border problem

@zsxkib zsxkib merged commit 8c881c9 into main Sep 7, 2024
2 checks passed
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.

1 participant