sharpfin don't use antialias with area mode

This commit is contained in:
Disty0
2026-06-15 23:41:58 +03:00
parent d227a46406
commit 78243b70a4
2 changed files with 11 additions and 10 deletions
+2 -2
View File
@@ -184,14 +184,14 @@ def resize_tensor(tensor: torch.Tensor, target_size: tuple[int, int], *, kernel=
mode = 'bilinear' if (target_size[0] * target_size[1]) > (tensor.shape[-2] * tensor.shape[-1]) else 'area'
log.debug(f'Resize tensor: method=torch mode={mode} shape={tensor.shape} target={target_size} fn={fn}')
inp = tensor if tensor.dim() == 4 else tensor.unsqueeze(0)
result = torch.nn.functional.interpolate(inp, size=target_size, mode=mode, antialias=True)
result = torch.nn.functional.interpolate(inp, size=target_size, mode=mode, antialias=(mode != 'area'))
return result.squeeze(0) if tensor.dim() == 3 else result
rk = get_kernel(kernel)
if rk is None:
mode = 'bilinear' if (target_size[0] * target_size[1]) > (tensor.shape[-2] * tensor.shape[-1]) else 'area'
log.debug(f'Resize tensor: method=torch mode={mode} shape={tensor.shape} target={target_size} kernel=None fn={fn}')
inp = tensor if tensor.dim() == 4 else tensor.unsqueeze(0)
result = torch.nn.functional.interpolate(inp, size=target_size, mode=mode, antialias=True)
result = torch.nn.functional.interpolate(inp, size=target_size, mode=mode, antialias=(mode != 'area'))
return result.squeeze(0) if tensor.dim() == 3 else result
from modules.sharpfin.functional import scale
+9 -8
View File
@@ -47,7 +47,7 @@ def img_to_pixelart(image: PipelineImageInput, sharpen: float = 0, block_size: i
@devices.inference_context()
def edge_detect_for_pixelart(image: PipelineImageInput, image_weight: float = 1.0, block_size: int = 8, device: torch.device = "cpu") -> torch.Tensor:
block_size_sq = block_size * block_size
new_image = process_image_input(image).to(device).to(dtype=torch.float32) / 255
new_image = process_image_input(image).to(device).to(dtype=torch.float32).div(255)
new_image = new_image.permute(0,3,1,2)
batch_size, _channels, height, width = new_image.shape
block_height = height // block_size
@@ -58,15 +58,16 @@ def edge_detect_for_pixelart(image: PipelineImageInput, image_weight: float = 1.
greyscale = (new_image[:,0,:,:] * 0.299).add_(new_image[:,1,:,:], alpha=0.587).add_(new_image[:,2,:,:], alpha=0.114)
greyscale = greyscale[:, :(new_image.shape[-2]//block_size)*block_size, :(new_image.shape[-1]//block_size)*block_size] # crop to a multiple of block_size
greyscale_reshaped = greyscale.reshape(batch_size, block_size, block_height, block_size, block_width)
greyscale_reshaped = greyscale_reshaped.permute(0,1,3,2,4)
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 = sharpfin.resize_tensor(greyscale_range, (height, width), linearize=False)
range_weight = greyscale.reshape(batch_size, block_size, block_height, block_size, block_width)
range_weight = range_weight.permute(0,1,3,2,4).reshape(batch_size, block_size_sq, block_height, block_width)
range_weight = range_weight.amax(dim=1, keepdim=True).sub_(range_weight.amin(dim=1, keepdim=True))
range_weight = sharpfin.resize_tensor(range_weight, (height, width), linearize=False)
range_weight = range_weight.div_(range_weight.max())
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)
weight_map = (greyscale > greyscale.median()).unsqueeze(1).to(dtype=torch.float32)
weight_map = sharpfin.resize_tensor(weight_map, (height, width), linearize=False)
weight_map = weight_map.add_(range_weight).mul_(image_weight / 2)
new_image = new_image.mul_(weight_map).addcmul_(min_pool, (1-weight_map))
new_image = new_image.permute(0,2,3,1).mul_(255).clamp_(0, 255)