refactor image methods

Signed-off-by: vladmandic <mandic00@live.com>
This commit is contained in:
vladmandic
2026-02-11 12:29:00 +01:00
parent 0ed64ec195
commit da1cf2f996
35 changed files with 682 additions and 648 deletions
+6 -6
View File
@@ -9,8 +9,8 @@ from diffusers.image_processor import PipelineImageInput
from diffusers.configuration_utils import ConfigMixin, register_to_config
from transformers import ImageProcessingMixin
from modules import images_sharpfin
from modules import devices
from modules.image import sharpfin
@devices.inference_context()
@@ -64,9 +64,9 @@ def edge_detect_for_pixelart(image: PipelineImageInput, image_weight: float = 1.
greyscale_reshaped = greyscale_reshaped.reshape(batch_size, block_size_sq, block_height, block_width)
greyscale_range = greyscale_reshaped.amax(dim=1, keepdim=True).sub_(greyscale_reshaped.amin(dim=1, keepdim=True))
range_weight = images_sharpfin.resize_tensor(greyscale_range, (height, width), linearize=False)
range_weight = sharpfin.resize_tensor(greyscale_range, (height, width), linearize=False)
range_weight = range_weight.div_(range_weight.max())
weight_map = images_sharpfin.resize_tensor((greyscale > greyscale.median()).to(dtype=torch.float32), (height, width), linearize=False)
weight_map = sharpfin.resize_tensor((greyscale > greyscale.median()).to(dtype=torch.float32), (height, width), linearize=False)
weight_map = weight_map.unsqueeze(0).add_(range_weight).mul_(image_weight / 2)
new_image = new_image.mul_(weight_map).addcmul_(min_pool, (1-weight_map))
@@ -158,7 +158,7 @@ def encode_jpeg_tensor(img: torch.FloatTensor, block_size: int=16, cbcr_downscal
img = img[:, :, :(img.shape[-2]//block_size)*block_size, :(img.shape[-1]//block_size)*block_size] # crop to a multiply of block_size
cbcr_block_size = block_size//cbcr_downscale
_, _, height, width = img.shape
down_img = images_sharpfin.resize_tensor(img[:, 1:,:,:], (height//cbcr_downscale, width//cbcr_downscale), linearize=False)
down_img = sharpfin.resize_tensor(img[:, 1:,:,:], (height//cbcr_downscale, width//cbcr_downscale), linearize=False)
y = encode_single_channel_dct_2d(img[:, 0, :,:], block_size=block_size, norm=norm)
cb = encode_single_channel_dct_2d(down_img[:, 0, :,:], block_size=cbcr_block_size, norm=norm)
cr = encode_single_channel_dct_2d(down_img[:, 1, :,:], block_size=cbcr_block_size, norm=norm)
@@ -176,8 +176,8 @@ def decode_jpeg_tensor(jpeg_img: torch.FloatTensor, block_size: int=16, cbcr_dow
y = decode_single_channel_dct_2d(y, norm=norm)
cb = decode_single_channel_dct_2d(cb, norm=norm)
cr = decode_single_channel_dct_2d(cr, norm=norm)
cb = images_sharpfin.resize_tensor(cb, (h_blocks*block_size, w_blocks*block_size), linearize=False)
cr = images_sharpfin.resize_tensor(cr, (h_blocks*block_size, w_blocks*block_size), linearize=False)
cb = sharpfin.resize_tensor(cb, (h_blocks*block_size, w_blocks*block_size), linearize=False)
cr = sharpfin.resize_tensor(cr, (h_blocks*block_size, w_blocks*block_size), linearize=False)
return torch.stack([y,cb,cr], dim=1)
+3 -3
View File
@@ -3,9 +3,10 @@ import random
import numpy as np
import torch
from PIL import Image
from modules import devices, images_sharpfin
from modules import devices
from modules.shared import opts, log
from modules.upscaler import Upscaler, UpscalerData
from modules.image import convert
MODELS_MAP = {
@@ -13,7 +14,6 @@ MODELS_MAP = {
"SeedVR2 7B": "seedvr2_ema_7b_fp16.safetensors",
"SeedVR2 7B Sharp": "seedvr2_ema_7b_sharp_fp16.safetensors",
}
to_pil = images_sharpfin.to_pil
class UpscalerSeedVR(Upscaler):
@@ -158,7 +158,7 @@ class UpscalerSeedVR(Upscaler):
)
t1 = time.time()
log.info(f'Upscaler: type="{self.name}" model="{selected_file}" scale={self.scale} cfg={opts.seedvt_cfg_scale} seed={seed} time={t1 - t0:.2f}')
img = to_pil(result_tensor.squeeze())
img = convert.to_pil(result_tensor.squeeze())
if opts.upscaler_unload:
self.model.dit = None