improve resolution handling (#396)

* improve resolution handling

* improve resolution handling

* improve resolution handling

* improve resolution handling
This commit is contained in:
lllyasviel 2023-09-16 13:21:35 -07:00 committed by GitHub
parent 6adc771888
commit ceee6dfd73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 22 deletions

View File

@ -1 +1 @@
version = '2.0.51' version = '2.0.52'

View File

@ -23,7 +23,7 @@ def worker():
from modules.sdxl_styles import apply_style, aspect_ratios, fooocus_expansion from modules.sdxl_styles import apply_style, aspect_ratios, fooocus_expansion
from modules.private_logger import log from modules.private_logger import log
from modules.expansion import safe_str from modules.expansion import safe_str
from modules.util import join_prompts, remove_empty_str, HWC3, resize_image from modules.util import join_prompts, remove_empty_str, HWC3, resize_image, image_is_generated_in_current_ui
from modules.upscaler import perform_upscale from modules.upscaler import perform_upscale
try: try:
@ -82,11 +82,12 @@ def worker():
progressbar(0, 'Image processing ...') progressbar(0, 'Image processing ...')
if uov_method != flags.disabled and uov_input_image is not None: if uov_method != flags.disabled and uov_input_image is not None:
uov_input_image = HWC3(uov_input_image) uov_input_image = HWC3(uov_input_image)
H, W, C = uov_input_image.shape
if 'vary' in uov_method: if 'vary' in uov_method:
if H * W + 8 < width * height or float(abs(H * width - W * height)) > 1.5 * float(max(H, W, width, height)): if not image_is_generated_in_current_ui(uov_input_image, ui_width=width, ui_height=height):
uov_input_image = resize_image(uov_input_image, width=width, height=height) uov_input_image = resize_image(uov_input_image, width=width, height=height)
print(f'Aspect ratio corrected - users are uploading their own images.') print(f'Resolution corrected - users are uploading their own images.')
else:
print(f'Processing images generated by Fooocus.')
if 'subtle' in uov_method: if 'subtle' in uov_method:
denoising_strength = 0.5 denoising_strength = 0.5
if 'strong' in uov_method: if 'strong' in uov_method:
@ -99,6 +100,14 @@ def worker():
height = H * 8 height = H * 8
print(f'Final resolution is {str((height, width))}.') print(f'Final resolution is {str((height, width))}.')
elif 'upscale' in uov_method: elif 'upscale' in uov_method:
H, W, C = uov_input_image.shape
progressbar(0, f'Upscaling image from {str((H, W))} ...')
uov_input_image = core.numpy_to_pytorch(uov_input_image)
uov_input_image = perform_upscale(uov_input_image)
uov_input_image = core.pytorch_to_numpy(uov_input_image)[0]
print(f'Image upscaled.')
if '1.5x' in uov_method: if '1.5x' in uov_method:
f = 1.5 f = 1.5
elif '2x' in uov_method: elif '2x' in uov_method:
@ -106,25 +115,31 @@ def worker():
else: else:
f = 1.0 f = 1.0
width = int(W * f) width_f = int(width * f)
height = int(H * f) height_f = int(height * f)
image_is_super_large = width * height > 2800 * 2800
progressbar(0, f'Upscaling image from {str((H, W))} to {str((height, width))}...')
uov_input_image = core.numpy_to_pytorch(uov_input_image) if image_is_generated_in_current_ui(uov_input_image, ui_width=width_f, ui_height=height_f):
uov_input_image = perform_upscale(uov_input_image) uov_input_image = resize_image(uov_input_image, width=int(W * f), height=int(H * f))
uov_input_image = core.pytorch_to_numpy(uov_input_image)[0] print(f'Processing images generated by Fooocus.')
uov_input_image = resize_image(uov_input_image, width=width, height=height) else:
print(f'Image upscaled.') uov_input_image = resize_image(uov_input_image, width=width_f, height=height_f)
print(f'Resolution corrected - users are uploading their own images.')
if 'fast' in uov_method or image_is_super_large: H, W, C = uov_input_image.shape
if 'fast' not in uov_method: image_is_super_large = H * W > 2800 * 2800
print('Image is too large. Directly returned the SR image. '
'Usually directly return SR image at 4K resolution ' if 'fast' in uov_method:
'yields better results than SDXL diffusion.') direct_return = True
d = [ elif image_is_super_large:
('Upscale (Fast)', '2x'), print('Image is too large. Directly returned the SR image. '
] 'Usually directly return SR image at 4K resolution '
'yields better results than SDXL diffusion.')
direct_return = True
else:
direct_return = False
if direct_return:
d = [('Upscale (Fast)', '2x')]
log(uov_input_image, d, single_line_number=1) log(uov_input_image, d, single_line_number=1)
outputs.append(['results', [uov_input_image]]) outputs.append(['results', [uov_input_image]])
return return
@ -135,6 +150,7 @@ def worker():
switch = int(steps * 0.67) switch = int(steps * 0.67)
initial_pixels = core.numpy_to_pytorch(uov_input_image) initial_pixels = core.numpy_to_pytorch(uov_input_image)
progressbar(0, 'VAE encoding ...') progressbar(0, 'VAE encoding ...')
initial_latent = core.encode_vae(vae=pipeline.xl_base_patched.vae, pixels=initial_pixels, tiled=True) initial_latent = core.encode_vae(vae=pipeline.xl_base_patched.vae, pixels=initial_pixels, tiled=True)
B, C, H, W = initial_latent['samples'].shape B, C, H, W = initial_latent['samples'].shape
width = W * 8 width = W * 8

View File

@ -6,6 +6,25 @@ import os
from PIL import Image from PIL import Image
def image_is_generated_in_current_ui(image, ui_width, ui_height):
H, W, C = image.shape
if H < ui_height:
return False
if W < ui_width:
return False
k1 = float(H) / float(W)
k2 = float(ui_height) / float(ui_width)
d = abs(k1 - k2)
if d > 0.01:
return False
return True
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS) LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)