asymmetric vae v2 and libvips support

Signed-off-by: Vladimir Mandic <mandic00@live.com>
This commit is contained in:
Vladimir Mandic
2025-03-16 19:51:38 -04:00
parent 942553a504
commit d4dff967b3
4 changed files with 69 additions and 7 deletions
+3
View File
@@ -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
+64 -5
View File
@@ -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
+1 -1
View File
@@ -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))
+1 -1
Submodule wiki updated: 3676f5628e...62636f56b6