Delete extensions-builtin/forge_preprocessor_tile
Browse files
extensions-builtin/forge_preprocessor_tile/scripts/preprocessor_tile.py
DELETED
|
@@ -1,102 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
from modules_forge.supported_preprocessor import Preprocessor, PreprocessorParameter
|
| 4 |
-
from modules_forge.shared import add_supported_preprocessor
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def blur(x, k):
|
| 8 |
-
y = torch.nn.functional.pad(x, (k, k, k, k), mode='replicate')
|
| 9 |
-
y = torch.nn.functional.avg_pool2d(y, (k * 2 + 1, k * 2 + 1), stride=(1, 1))
|
| 10 |
-
return y
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class PreprocessorTile(Preprocessor):
|
| 14 |
-
def __init__(self):
|
| 15 |
-
super().__init__()
|
| 16 |
-
self.name = 'tile_resample'
|
| 17 |
-
self.tags = ['Tile']
|
| 18 |
-
self.model_filename_filters = ['tile']
|
| 19 |
-
self.slider_resolution = PreprocessorParameter(visible=False)
|
| 20 |
-
self.latent = None
|
| 21 |
-
|
| 22 |
-
def register_latent(self, process, cond):
|
| 23 |
-
vae = process.sd_model.forge_objects.vae
|
| 24 |
-
# This is a powerful VAE with integrated memory management, bf16, and tiled fallback.
|
| 25 |
-
|
| 26 |
-
latent_image = vae.encode(cond.movedim(1, -1))
|
| 27 |
-
latent_image = process.sd_model.forge_objects.unet.model.latent_format.process_in(latent_image)
|
| 28 |
-
self.latent = latent_image
|
| 29 |
-
return self.latent
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
class PreprocessorTileColorFix(PreprocessorTile):
|
| 33 |
-
def __init__(self):
|
| 34 |
-
super().__init__()
|
| 35 |
-
self.name = 'tile_colorfix'
|
| 36 |
-
self.slider_1 = PreprocessorParameter(label='Variation', value=8.0, minimum=3.0, maximum=32.0, step=1.0, visible=True)
|
| 37 |
-
self.variation = 8
|
| 38 |
-
self.sharpness = None
|
| 39 |
-
|
| 40 |
-
def process_before_every_sampling(self, process, cond, mask, *args, **kwargs):
|
| 41 |
-
self.variation = int(kwargs['unit'].threshold_a)
|
| 42 |
-
|
| 43 |
-
latent = self.register_latent(process, cond)
|
| 44 |
-
|
| 45 |
-
unet = process.sd_model.forge_objects.unet.clone()
|
| 46 |
-
sigma_data = process.sd_model.forge_objects.unet.model.model_sampling.sigma_data
|
| 47 |
-
|
| 48 |
-
if getattr(process, 'is_hr_pass', False):
|
| 49 |
-
k = int(self.variation * 2)
|
| 50 |
-
else:
|
| 51 |
-
k = int(self.variation)
|
| 52 |
-
|
| 53 |
-
def block_proc(h, flag, transformer_options):
|
| 54 |
-
location, block_id = transformer_options['block']
|
| 55 |
-
cond_mark = transformer_options['cond_mark'][:, None, None, None] # cond is 0
|
| 56 |
-
|
| 57 |
-
if location == 'input' and block_id == 0 and flag == 'before':
|
| 58 |
-
sigma = transformer_options['sigmas'].to(h)
|
| 59 |
-
self.x_input = h[:, :4] # Inpaint fix
|
| 60 |
-
self.x_input_sigma_space = self.x_input * (sigma ** 2 + sigma_data ** 2) ** 0.5
|
| 61 |
-
|
| 62 |
-
if location == 'last' and block_id == 0 and flag == 'after':
|
| 63 |
-
sigma = transformer_options['sigmas'].to(h)
|
| 64 |
-
eps_estimation = h[:, :4]
|
| 65 |
-
denoised = self.x_input_sigma_space - eps_estimation * sigma
|
| 66 |
-
|
| 67 |
-
denoised = denoised - blur(denoised, k) + blur(latent.to(denoised), k)
|
| 68 |
-
|
| 69 |
-
if isinstance(self.sharpness, float):
|
| 70 |
-
detail_weight = float(self.sharpness) * 0.01
|
| 71 |
-
neg = detail_weight * blur(denoised, k) + (1 - detail_weight) * denoised
|
| 72 |
-
denoised = (1 - cond_mark) * denoised + cond_mark * neg
|
| 73 |
-
|
| 74 |
-
eps_modified = (self.x_input_sigma_space - denoised) / sigma
|
| 75 |
-
|
| 76 |
-
return eps_modified
|
| 77 |
-
|
| 78 |
-
return h
|
| 79 |
-
|
| 80 |
-
unet.add_block_modifier(block_proc)
|
| 81 |
-
|
| 82 |
-
process.sd_model.forge_objects.unet = unet
|
| 83 |
-
|
| 84 |
-
return cond, mask
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
class PreprocessorTileColorFixSharp(PreprocessorTileColorFix):
|
| 88 |
-
def __init__(self):
|
| 89 |
-
super().__init__()
|
| 90 |
-
self.name = 'tile_colorfix+sharp'
|
| 91 |
-
self.slider_2 = PreprocessorParameter(label='Sharpness', value=1.0, minimum=0.0, maximum=2.0, step=0.01, visible=True)
|
| 92 |
-
|
| 93 |
-
def process_before_every_sampling(self, process, cond, mask, *args, **kwargs):
|
| 94 |
-
self.sharpness = float(kwargs['unit'].threshold_b)
|
| 95 |
-
return super().process_before_every_sampling(process, cond, mask, *args, **kwargs)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
add_supported_preprocessor(PreprocessorTile())
|
| 99 |
-
|
| 100 |
-
add_supported_preprocessor(PreprocessorTileColorFix())
|
| 101 |
-
|
| 102 |
-
add_supported_preprocessor(PreprocessorTileColorFixSharp())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|