refactor: integrate sharpfin for high-quality image resize

Vendor sharpfin library (Apache 2.0) and add centralized wrapper
module (images_sharpfin.py) replacing torchvision tensor/PIL
conversion and resize operations throughout the codebase.

- Add modules/sharpfin/ vendored library with MKS2021, Lanczos3,
  Mitchell, Catmull-Rom kernels and optional Triton sparse acceleration
- Add modules/images_sharpfin.py wrapper with to_tensor(), to_pil(),
  pil_to_tensor(), normalize(), resize(), resize_tensor()
- Add resize_quality and resize_linearize_srgb settings
- Add MKS2021 and Lanczos3 upscaler entries
- Replace torchvision.transforms.functional imports across 18 files
- to_pil() auto-detects HWC/BHWC layout, adds .round() before uint8
- Sparse Triton path falls back to dense GPU on compilation failure
- Mixed-axis resize splits into two single-axis scale() calls
- Masks and non-sRGB data always use linearize=False
This commit is contained in:
CalamitousFelicitousness
2026-02-10 00:13:35 +00:00
committed by vladmandic
parent 2c4d0751d9
commit 76aa949a26
30 changed files with 2878 additions and 78 deletions
+6 -6
View File
@@ -18,7 +18,6 @@ import cv2
import numpy as np
from PIL import Image, ImageFilter
import torch
import torchvision
from torchvision import transforms
from transformers import (
CLIPImageProcessor,
@@ -1323,7 +1322,8 @@ class StableDiffusionXLSoftFillPipeline(
image.save("noised_image.png")
image = transforms.CenterCrop((image.size[1] // 64 * 64, image.size[0] // 64 * 64))(image)
image = transforms.ToTensor()(image)
from modules import images_sharpfin
image = images_sharpfin.to_tensor(image)
image = image * 2 - 1 # Normalize to [-1, 1]
return image.unsqueeze(0)
@@ -1334,7 +1334,8 @@ class StableDiffusionXLSoftFillPipeline(
"""
map = map.convert("L")
map = transforms.CenterCrop((map.size[1] // 64 * 64, map.size[0] // 64 * 64))(map)
map = transforms.ToTensor()(map)
from modules import images_sharpfin
map = images_sharpfin.to_tensor(map)
map = (map - 0.05) / (0.95 - 0.05)
map = torch.clamp(map, 0.0, 1.0)
return 1.0 - map
@@ -1349,9 +1350,8 @@ class StableDiffusionXLSoftFillPipeline(
# Prepare mask as rescaled tensor map
map = preprocess_map(mask).to(device)
map = torchvision.transforms.Resize(
tuple(s // self.vae_scale_factor for s in original_image_tensor.shape[2:]), antialias=None
)(map)
from modules import images_sharpfin
map = images_sharpfin.resize_tensor(map, tuple(s // self.vae_scale_factor for s in original_image_tensor.shape[2:]), linearize=False)
# Generate latent tensor with noise
original_with_noise = self.prepare_latents(