diff --git a/CHANGELOG.md b/CHANGELOG.md index c659343df..fbc4fe616 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ against top-10 standard harmful content categories - add banned words/expressions check against prompt variations - **Other** + - **upscale**: new [asymmetric vae v2](Heasterian/AsymmetricAutoencoderKLUpscaler_v2) upscaling method + - **upscale**: new experimental support for `libvips` upscaling - add remote vae info to metadata, thanks @iDeNoh - add quantization support to **CogView-3Plus** - update `diffusers` and other requirements @@ -51,6 +53,7 @@ - guard against git returining invalid timestamp - fix hires with latent upscale - fix legacy diffusion latent upscalers + - fix upscaler selection in postprocessing - **IPEX** - add `--upgrade` to torch_command when using `--use-nightly` for *ipex* and *rocm* - add xpu to profiler diff --git a/modules/upscaler_simple.py b/modules/upscaler_simple.py index a28d540f5..bb342d5c8 100644 --- a/modules/upscaler_simple.py +++ b/modules/upscaler_simple.py @@ -98,21 +98,29 @@ class UpscalerAsymmetricVAE(Upscaler): super().__init__(False) self.name = "Asymmetric VAE" self.vae = None + self.selected = None self.scalers = [ - UpscalerData("Asymmetric VAE", None, self), + UpscalerData("Asymmetric VAE v1", None, self), + UpscalerData("Asymmetric VAE v2", None, self), ] def do_upscale(self, img: Image, selected_model=None): + if selected_model is None: + return img import torchvision.transforms.functional as F import diffusers from modules import shared, devices - - if self.vae is None: - self.vae = diffusers.AsymmetricAutoencoderKL.from_pretrained("Heasterian/AsymmetricAutoencoderKLUpscaler", cache_dir=shared.opts.hfcache_dir) + if self.vae is None or selected_model != self.selected: + if 'v1' in selected_model: + repo_id = 'Heasterian/AsymmetricAutoencoderKLUpscaler' + else: + repo_id = 'Heasterian/AsymmetricAutoencoderKLUpscaler_v2' + self.vae = diffusers.AsymmetricAutoencoderKL.from_pretrained(repo_id, cache_dir=shared.opts.hfcache_dir) + shared.log.debug(f'Upscaler load: vae="{repo_id}"') self.vae.requires_grad_(False) self.vae = self.vae.to(device=devices.device, dtype=devices.dtype) self.vae.eval() - img = img.resize((8 * (img.width // 8), 8 * (img.height // 8)), resample=Image.Resampling.BILINEAR).convert('RGB') + img = img.resize((8 * (img.width // 8), 8 * (img.height // 8)), resample=Image.Resampling.LANCZOS).convert('RGB') tensor = (F.pil_to_tensor(img).unsqueeze(0) / 255.0).to(device=devices.device, dtype=devices.dtype) self.vae = self.vae.to(device=devices.device) tensor = self.vae(tensor).sample @@ -141,3 +149,54 @@ class UpscalerDCC(Upscaler): upscaled = (255.0 * upscaled).astype(np.uint8) upscaled = Image.fromarray(upscaled) return upscaled + + +class UpscalerVIPS(Upscaler): + def __init__(self, dirname=None): # pylint: disable=unused-argument + super().__init__(False) + self.name = "VIPS" + self.scalers = [ + UpscalerData("VIPS Lanczos 2", None, self), + UpscalerData("VIPS Lanczos 3", None, self), + UpscalerData("VIPS Mitchell", None, self), + UpscalerData("VIPS MagicKernelSharp 2013", None, self), + UpscalerData("VIPS MagicKernelSharp 2021", None, self), + ] + + def do_upscale(self, img: Image, selected_model=None): + if selected_model is None: + return img + from installer import install + from modules.shared import log + install('pyvips') + try: + import pyvips + except Exception as e: + log.error(f"Upscaler: vips {e}") + return img + vips_image = pyvips.Image.new_from_array(img) + # import numpy as np + # np_image = np.array(img) + # h, w, c = np_image.shape + # np_linear = np_image.reshape(w * h * c) + # vips_image = pyvips.Image.new_from_memory(np_linear.data, w, h, c, 'uchar') + try: + if selected_model is None: + return img + elif selected_model == "VIPS Lanczos 2": + vips_image = vips_image.resize(2, kernel='lanczos2') + elif selected_model == "VIPS Lanczos 3": + vips_image = vips_image.resize(2, kernel='lanczos3') + elif selected_model == "VIPS Mitchell": + vips_image = vips_image.resize(2, kernel='mitchell') + elif selected_model == "VIPS MagicKernelSharp 2013": + vips_image = vips_image.resize(2, kernel='mks2013') + elif selected_model == "VIPS MagicKernelSharp 2021": + vips_image = vips_image.resize(2, kernel='mks2021') + else: + return img + except Exception as e: + log.error(f"Upscaler: vips {e}") + return img + upscaled = Image.fromarray(vips_image.numpy()) + return upscaled diff --git a/scripts/postprocessing_upscale.py b/scripts/postprocessing_upscale.py index 104a0fb37..cffff8ed4 100644 --- a/scripts/postprocessing_upscale.py +++ b/scripts/postprocessing_upscale.py @@ -54,7 +54,7 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): info["Postprocess upscale to"] = f"{upscale_to_width}x{upscale_to_height}" else: info["Postprocess upscale by"] = upscale_by - image = upscaler.scaler.upscale(image, upscale_by, upscaler.data_path) + image = upscaler.scaler.upscale(image, upscale_by, upscaler.name) if upscale_mode == 1 and upscale_crop: cropped = Image.new("RGB", (upscale_to_width, upscale_to_height)) cropped.paste(image, box=(upscale_to_width // 2 - image.width // 2, upscale_to_height // 2 - image.height // 2)) diff --git a/wiki b/wiki index 3676f5628..62636f56b 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 3676f5628e0a6048f21cac03ada9065a1500a354 +Subproject commit 62636f56b6ee7b623eed50f150ad2dea77976948