mirror of
https://github.com/vladmandic/automatic
synced 2026-09-19 17:24:32 +02:00
control add inpaint masked only
This commit is contained in:
Submodule extensions-builtin/sd-webui-controlnet updated: 1b7ae7dbec...00cd2ca525
+14
-16
@@ -135,6 +135,9 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
denoising_strength = denoising_strength,
|
||||
n_iter = batch_count,
|
||||
batch_size = batch_size,
|
||||
inpaint_full_res = masking.opts.mask_only,
|
||||
inpaint_full_res_padding = masking.opts.mask_padding,
|
||||
inpainting_fill = 1,
|
||||
outpath_samples=shared.opts.outdir_samples or shared.opts.outdir_control_samples,
|
||||
outpath_grids=shared.opts.outdir_grids or shared.opts.outdir_control_grids,
|
||||
)
|
||||
@@ -366,7 +369,8 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
p.image = []
|
||||
debug(f'Control: process=None image={p.image} mask={mask}')
|
||||
elif len(active_process) == 0:
|
||||
p.image = [masking.run_mask(input_image=input_image, input_mask=mask, return_type='Masked') if mask is not None else input_image]
|
||||
# p.image = [masking.run_mask(input_image=input_image, input_mask=mask, return_type='Masked') if mask is not None else input_image]
|
||||
pass
|
||||
elif len(active_process) > 0:
|
||||
p.image = []
|
||||
masked_image = masking.run_mask(input_image=input_image, input_mask=mask, return_type='Masked') if mask is not None else input_image
|
||||
@@ -375,8 +379,8 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
debug(f'Control: process={[process.processor_id for p in active_process]} i={i} image={p.image}')
|
||||
p.image.append(process(masked_image, image_mode))
|
||||
|
||||
if len(p.image) > 0:
|
||||
p.task_args['image'] = p.image
|
||||
if p.image is not None and len(p.image) > 0:
|
||||
p.init_images = p.image
|
||||
p.extra_generation_params["Control process"] = [p.processor_id for p in active_process]
|
||||
if any(img is None for img in p.image):
|
||||
msg = 'Control: attempting process but output is none'
|
||||
@@ -400,7 +404,7 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
restore_pipeline()
|
||||
return msg
|
||||
elif unit_type == 'controlnet' and input_type == 1: # Init image same as control
|
||||
p.task_args['image'] = input_image
|
||||
p.init_images = input_image
|
||||
p.task_args['control_image'] = p.image
|
||||
p.task_args['strength'] = p.denoising_strength
|
||||
elif unit_type == 'controlnet' and input_type == 2: # Separate init image
|
||||
@@ -408,9 +412,9 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
p.task_args['strength'] = p.denoising_strength
|
||||
if init_image is None:
|
||||
shared.log.warning('Control: separate init image not provided')
|
||||
p.task_args['image'] = input_image
|
||||
p.init_images = input_image
|
||||
else:
|
||||
p.task_args['image'] = init_image
|
||||
p.init_images = init_image
|
||||
|
||||
if is_generator:
|
||||
image_txt = f'{processed_image.width}x{processed_image.height}' if processed_image is not None else 'None'
|
||||
@@ -420,18 +424,12 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
t2 += time.time() - t2
|
||||
|
||||
# prepare pipeline
|
||||
if hasattr(p, 'init_images'):
|
||||
del p.init_images # control never uses init_image as-is
|
||||
if pipe is not None:
|
||||
if not has_models and (unit_type == 'controlnet' or unit_type == 'adapter' or unit_type == 'xs' or unit_type == 'lite'): # run in txt2img/img2img/inpaint mode
|
||||
if mask is not None:
|
||||
p.task_args['strength'] = denoising_strength
|
||||
p.image_mask = mask
|
||||
p.inpaint_full_res = False
|
||||
p.init_images = [input_image]
|
||||
# TODO implement mask_overlap enable once fixed in diffusers
|
||||
# if mask_overlap > 0:
|
||||
# p.task_args['padding_mask_crop'] = mask_overlap
|
||||
shared.sd_model = sd_models.set_diffuser_pipe(shared.sd_model, sd_models.DiffusersTaskType.INPAINTING)
|
||||
elif processed_image is not None:
|
||||
p.init_images = [processed_image]
|
||||
@@ -447,17 +445,17 @@ def control_run(units: List[unit.Unit], inputs, inits, mask, unit_type: str, is_
|
||||
if mask is not None:
|
||||
p.task_args['strength'] = denoising_strength
|
||||
p.image_mask = mask
|
||||
p.inpaint_full_res = False
|
||||
# TODO implement mask_overlap enable once fixed in diffusers
|
||||
# if mask_overlap > 0:
|
||||
# p.task_args['padding_mask_crop'] = mask_overlap
|
||||
shared.sd_model = sd_models.set_diffuser_pipe(shared.sd_model, sd_models.DiffusersTaskType.INPAINTING) # only controlnet supports inpaint
|
||||
elif 'control_image' in p.task_args:
|
||||
shared.sd_model = sd_models.set_diffuser_pipe(shared.sd_model, sd_models.DiffusersTaskType.IMAGE_2_IMAGE) # only controlnet supports img2img
|
||||
else:
|
||||
shared.sd_model = sd_models.set_diffuser_pipe(shared.sd_model, sd_models.DiffusersTaskType.TEXT_2_IMAGE)
|
||||
if p.init_images is not None:
|
||||
p.task_args['image'] = p.init_images # need to set explicitly for txt2img
|
||||
if unit_type == 'lite':
|
||||
instance.apply(selected_models, p.image, use_conditioning)
|
||||
if p.init_images is None:
|
||||
del p.init_images
|
||||
|
||||
# ip adapter
|
||||
if ipadapter.apply(shared.sd_model, p, ip_adapter, ip_scale, ip_image or input_image):
|
||||
|
||||
+9
-7
@@ -210,7 +210,7 @@ def draw_prompt_matrix(im, width, height, all_prompts, margin=0):
|
||||
|
||||
|
||||
def resize_image(resize_mode, im, width, height, upscaler_name=None, output_type='image'):
|
||||
shared.log.debug(f'Image resize: mode={resize_mode} resolution={width}x{height} upscaler={upscaler_name} function={sys._getframe(1).f_code.co_name}') # pylint: disable=protected-access
|
||||
shared.log.debug(f'Image resize: input={im} mode={resize_mode} target={width}x{height} upscaler={upscaler_name} function={sys._getframe(1).f_code.co_name}') # pylint: disable=protected-access
|
||||
"""
|
||||
Resizes an image with the specified resize_mode, width, and height.
|
||||
Args:
|
||||
@@ -266,8 +266,8 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None, output_type
|
||||
res = Image.new(im.mode, (width, height))
|
||||
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
|
||||
else:
|
||||
ratio = width / height
|
||||
src_ratio = im.width / im.height
|
||||
ratio = round(width / height, 1)
|
||||
src_ratio = round(im.width / im.height, 1)
|
||||
src_w = width if ratio < src_ratio else im.width * height // im.height
|
||||
src_h = height if ratio >= src_ratio else im.height * width // im.width
|
||||
resized = resize(im, src_w, src_h)
|
||||
@@ -275,12 +275,14 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None, output_type
|
||||
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
|
||||
if ratio < src_ratio:
|
||||
fill_height = height // 2 - src_h // 2
|
||||
res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
|
||||
res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
|
||||
if width > 0 and fill_height > 0:
|
||||
res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
|
||||
res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
|
||||
elif ratio > src_ratio:
|
||||
fill_width = width // 2 - src_w // 2
|
||||
res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
|
||||
res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
|
||||
if height > 0 and fill_width > 0:
|
||||
res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
|
||||
res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
|
||||
if output_type == 'np':
|
||||
return np.array(res)
|
||||
return res
|
||||
|
||||
+17
-9
@@ -129,7 +129,9 @@ btn_segment = None
|
||||
controls = []
|
||||
opts = SimpleNamespace(**{
|
||||
'auto_mask': 'None',
|
||||
'mask_only': False,
|
||||
'mask_blur': 0.01,
|
||||
'mask_padding': 0,
|
||||
'mask_erode': 0.01,
|
||||
'mask_dilate': 0.01,
|
||||
'seg_iou_thresh': 0.5,
|
||||
@@ -324,6 +326,9 @@ def run_mask(input_image: gr.Image, input_mask: gr.Image = None, return_type: st
|
||||
opts.mask_blur = mask_blur / min(input_image.width, input_image.height)
|
||||
if mask_padding is not None:
|
||||
opts.mask_dilate = mask_padding / min(input_image.width, input_image.height)
|
||||
opts.mask_padding = mask_padding
|
||||
else:
|
||||
opts.mask_padding = int(opts.mask_dilate * input_image.height / 4) + 1
|
||||
|
||||
if loaded_model is None or not segment_enable:
|
||||
mask = input_mask
|
||||
@@ -407,15 +412,16 @@ def run_mask_live(input_image: gr.Image):
|
||||
def create_segment_ui():
|
||||
def update_opts(*args):
|
||||
opts.seg_live = args[0]
|
||||
opts.mask_blur = args[1]
|
||||
opts.mask_erode = args[2]
|
||||
opts.mask_dilate = args[3]
|
||||
opts.auto_mask = args[4]
|
||||
opts.seg_score_thresh = args[5]
|
||||
opts.seg_iou_thresh = args[6]
|
||||
opts.seg_nms_thresh = args[7]
|
||||
opts.preview_type = args[8]
|
||||
opts.seg_colormap = args[9]
|
||||
opts.mask_only = args[1]
|
||||
opts.mask_blur = args[2]
|
||||
opts.mask_erode = args[3]
|
||||
opts.mask_dilate = args[4]
|
||||
opts.auto_mask = args[5]
|
||||
opts.seg_score_thresh = args[6]
|
||||
opts.seg_iou_thresh = args[7]
|
||||
opts.seg_nms_thresh = args[8]
|
||||
opts.preview_type = args[9]
|
||||
opts.seg_colormap = args[10]
|
||||
|
||||
global btn_segment # pylint: disable=global-statement
|
||||
with gr.Accordion(open=False, label="Mask", elem_id="control_mask", elem_classes=["small-accordion"]):
|
||||
@@ -423,6 +429,8 @@ def create_segment_ui():
|
||||
with gr.Row():
|
||||
controls.append(gr.Checkbox(label="Live update", value=True))
|
||||
btn_segment = ui_components.ToolButton(value=ui_symbols.refresh, visible=True)
|
||||
with gr.Row():
|
||||
controls.append(gr.Checkbox(label="Inpaint masked only", value=False))
|
||||
with gr.Row():
|
||||
controls.append(gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Blur', value=0.01, elem_id="control_mask_blur"))
|
||||
controls.append(gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Erode', value=0.01, elem_id="control_mask_erode"))
|
||||
|
||||
@@ -1235,7 +1235,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
|
||||
|
||||
class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
|
||||
|
||||
def __init__(self, init_images: list = None, resize_mode: int = 0, resize_name: str = 'None', denoising_strength: float = 0.3, image_cfg_scale: float = None, mask: Any = None, mask_blur: int = 4, inpainting_fill: int = 0, inpaint_full_res: bool = True, inpaint_full_res_padding: int = 0, inpainting_mask_invert: int = 0, initial_noise_multiplier: float = None, scale_by: float = 1, refiner_steps: int = 5, refiner_start: float = 0, refiner_prompt: str = '', refiner_negative: str = '', **kwargs):
|
||||
def __init__(self, init_images: list = None, resize_mode: int = 0, resize_name: str = 'None', denoising_strength: float = 0.3, image_cfg_scale: float = None, mask: Any = None, mask_blur: int = 4, inpainting_fill: int = 0, inpaint_full_res: bool = False, inpaint_full_res_padding: int = 0, inpainting_mask_invert: int = 0, initial_noise_multiplier: float = None, scale_by: float = 1, refiner_steps: int = 5, refiner_start: float = 0, refiner_prompt: str = '', refiner_negative: str = '', **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.init_images = init_images
|
||||
self.resize_mode: int = resize_mode
|
||||
@@ -1289,7 +1289,7 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
|
||||
if self.image_mask is not None:
|
||||
if type(self.image_mask) == list:
|
||||
self.image_mask = self.image_mask[0]
|
||||
if shared.backend == shared.Backend.ORIGINAL:
|
||||
if shared.backend == shared.Backend.ORIGINAL: # original way of processing mask
|
||||
self.image_mask = create_binary_mask(self.image_mask)
|
||||
if self.inpainting_mask_invert:
|
||||
self.image_mask = ImageOps.invert(self.image_mask)
|
||||
@@ -1302,16 +1302,16 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
|
||||
else:
|
||||
if hasattr(self, 'init_images'):
|
||||
self.image_mask = modules.masking.run_mask(input_image=self.init_images, input_mask=self.image_mask, return_type='Grayscale', mask_blur=self.mask_blur, mask_padding=self.inpaint_full_res_padding, segment_enable=False)
|
||||
if self.inpaint_full_res:
|
||||
if self.inpaint_full_res: # mask only inpaint
|
||||
self.mask_for_overlay = self.image_mask
|
||||
mask = self.image_mask.convert('L')
|
||||
crop_region = modules.masking.get_crop_region(np.array(mask), self.inpaint_full_res_padding)
|
||||
crop_region = modules.masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
|
||||
x1, y1, x2, y2 = crop_region
|
||||
mask = mask.crop(crop_region)
|
||||
self.image_mask = images.resize_image(2, mask, self.width, self.height)
|
||||
crop_mask = mask.crop(crop_region)
|
||||
self.image_mask = images.resize_image(2, crop_mask, self.width, self.height)
|
||||
self.paste_to = (x1, y1, x2-x1, y2-y1)
|
||||
else:
|
||||
else: # full image inpaint
|
||||
self.image_mask = images.resize_image(self.resize_mode, self.image_mask, self.width, self.height)
|
||||
np_mask = np.array(self.image_mask)
|
||||
np_mask = np.clip((np_mask.astype(np.float32)) * 2, 0, 255).astype(np.uint8)
|
||||
@@ -1338,6 +1338,8 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
|
||||
if shared.opts.save_init_img:
|
||||
images.save_image(img, path=shared.opts.outdir_init_images, basename=None, forced_filename=self.init_img_hash, suffix="-init-image")
|
||||
image = images.flatten(img, shared.opts.img2img_background_color)
|
||||
if self.width is None or self.height is None:
|
||||
self.width, self.height = image.width, image.height
|
||||
if crop_region is None and self.resize_mode != 4 and self.resize_mode > 0:
|
||||
if image.width != self.width or image.height != self.height:
|
||||
image = images.resize_image(self.resize_mode, image, self.width, self.height, self.resize_name)
|
||||
|
||||
@@ -6,18 +6,7 @@ import typing
|
||||
import torch
|
||||
import torchvision.transforms.functional as TF
|
||||
import diffusers
|
||||
import modules.devices as devices
|
||||
import modules.shared as shared
|
||||
import modules.sd_samplers as sd_samplers
|
||||
import modules.sd_models as sd_models
|
||||
import modules.images as images
|
||||
import modules.errors as errors
|
||||
from modules.processing import StableDiffusionProcessing, create_random_tensors
|
||||
import modules.prompt_parser_diffusers as prompt_parser_diffusers
|
||||
from modules.sd_hijack_hypertile import hypertile_set
|
||||
from modules.processing_correction import correction_callback
|
||||
from modules.processing_vae import vae_encode, vae_decode
|
||||
from modules.masking import run_mask
|
||||
from modules import shared, devices, processing, sd_samplers, sd_models, images, errors, masking, prompt_parser_diffusers, sd_hijack_hypertile, processing_correction, processing_vae
|
||||
|
||||
|
||||
debug = shared.log.trace if os.environ.get('SD_DIFFUSERS_DEBUG', None) is not None else lambda *args, **kwargs: None
|
||||
@@ -26,7 +15,7 @@ debug_steps = shared.log.trace if os.environ.get('SD_STEPS_DEBUG', None) is not
|
||||
debug_steps('Trace: STEPS')
|
||||
|
||||
|
||||
def process_diffusers(p: StableDiffusionProcessing):
|
||||
def process_diffusers(p: processing.StableDiffusionProcessing):
|
||||
debug(f'Process diffusers args: {vars(p)}')
|
||||
results = []
|
||||
|
||||
@@ -37,6 +26,8 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
return p.enable_hr and p.refiner_steps > 0 and p.refiner_start > 0 and p.refiner_start < 1 and shared.sd_refiner is not None
|
||||
|
||||
def resize_images():
|
||||
if getattr(p, 'image', None) is not None and getattr(p, 'init_images', None) is None:
|
||||
p.init_images = [p.image]
|
||||
if getattr(p, 'init_images', None) is not None and len(p.init_images) > 0:
|
||||
tgt_width, tgt_height = 8 * math.ceil(p.init_images[0].width / 8), 8 * math.ceil(p.init_images[0].height / 8)
|
||||
if p.init_images[0].size != (tgt_width, tgt_height):
|
||||
@@ -44,9 +35,11 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
p.init_images = [images.resize_image(1, image, tgt_width, tgt_height, upscaler_name=None) for image in p.init_images]
|
||||
p.height = tgt_height
|
||||
p.width = tgt_width
|
||||
hypertile_set(p)
|
||||
sd_hijack_hypertile.hypertile_set(p)
|
||||
if getattr(p, 'mask', None) is not None and p.mask.size != (tgt_width, tgt_height):
|
||||
p.mask = images.resize_image(1, p.mask, tgt_width, tgt_height, upscaler_name=None)
|
||||
if getattr(p, 'init_mask', None) is not None and p.init_mask.size != (tgt_width, tgt_height):
|
||||
p.init_mask = images.resize_image(1, p.init_mask, tgt_width, tgt_height, upscaler_name=None)
|
||||
if getattr(p, 'mask_for_overlay', None) is not None and p.mask_for_overlay.size != (tgt_width, tgt_height):
|
||||
p.mask_for_overlay = images.resize_image(1, p.mask_for_overlay, tgt_width, tgt_height, upscaler_name=None)
|
||||
return tgt_width, tgt_height
|
||||
@@ -55,13 +48,13 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
def hires_resize(latents): # input=latents output=pil
|
||||
if not torch.is_tensor(latents):
|
||||
shared.log.warning('Hires: input is not tensor')
|
||||
first_pass_images = vae_decode(latents=latents, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
first_pass_images = processing_vae.vae_decode(latents=latents, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
return first_pass_images
|
||||
latent_upscaler = shared.latent_upscale_modes.get(p.hr_upscaler, None)
|
||||
shared.log.info(f'Hires: upscaler={p.hr_upscaler} width={p.hr_upscale_to_x} height={p.hr_upscale_to_y} images={latents.shape[0]}')
|
||||
if latent_upscaler is not None:
|
||||
latents = torch.nn.functional.interpolate(latents, size=(p.hr_upscale_to_y // 8, p.hr_upscale_to_x // 8), mode=latent_upscaler["mode"], antialias=latent_upscaler["antialias"])
|
||||
first_pass_images = vae_decode(latents=latents, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
first_pass_images = processing_vae.vae_decode(latents=latents, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
resized_images = []
|
||||
for img in first_pass_images:
|
||||
if latent_upscaler is None:
|
||||
@@ -75,14 +68,14 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
for i in range(len(latents)):
|
||||
from modules.processing import create_infotext
|
||||
info=create_infotext(p, p.all_prompts, p.all_seeds, p.all_subseeds, [], iteration=p.iteration, position_in_batch=i)
|
||||
decoded = vae_decode(latents=latents, model=shared.sd_model, output_type='pil', full_quality=p.full_quality)
|
||||
decoded = processing_vae.vae_decode(latents=latents, model=shared.sd_model, output_type='pil', full_quality=p.full_quality)
|
||||
for j in range(len(decoded)):
|
||||
images.save_image(decoded[j], path=p.outpath_samples, basename="", seed=p.seeds[i], prompt=p.prompts[i], extension=shared.opts.samples_format, info=info, p=p, suffix=suffix)
|
||||
|
||||
def diffusers_callback_legacy(step: int, timestep: int, latents: torch.FloatTensor):
|
||||
shared.state.sampling_step = step
|
||||
shared.state.current_latent = latents
|
||||
latents = correction_callback(p, timestep, {'latents': latents})
|
||||
latents = processing_correction.correction_callback(p, timestep, {'latents': latents})
|
||||
if shared.state.interrupted or shared.state.skipped:
|
||||
raise AssertionError('Interrupted...')
|
||||
if shared.state.paused:
|
||||
@@ -104,7 +97,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
time.sleep(0.1)
|
||||
if kwargs.get('latents', None) is None:
|
||||
return kwargs
|
||||
kwargs = correction_callback(p, timestep, kwargs)
|
||||
kwargs = processing_correction.correction_callback(p, timestep, kwargs)
|
||||
if p.scheduled_prompt and 'prompt_embeds' in kwargs and 'negative_prompt_embeds' in kwargs:
|
||||
try:
|
||||
i = (step + 1) % len(p.prompt_embeds)
|
||||
@@ -168,10 +161,11 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
elif (sd_models.get_diffusers_task(model) == sd_models.DiffusersTaskType.INPAINTING or is_img2img_model) and len(getattr(p, 'init_images' ,[])) > 0:
|
||||
p.ops.append('inpaint')
|
||||
if p.task_args.get('mask_image', None) is not None: # provided as override by a module
|
||||
# p.mask = shared.sd_model.mask_processor.blur(p.task_args['mask_image'], blur_factor=p.mask_blur) if p.mask_blur > 0 else p.task_args['mask_image']
|
||||
p.mask = run_mask(input_image=p.init_images, input_mask=p.task_args['mask_image'], return_type='Grayscale')
|
||||
p.mask = masking.run_mask(input_image=p.init_images, input_mask=p.task_args['mask_image'], return_type='Grayscale')
|
||||
if p.task_args.get('mask_image', None) is not None: # provided as override by a module
|
||||
p.mask = masking.run_mask(input_image=p.init_images, input_mask=p.task_args['mask_image'], return_type='Grayscale')
|
||||
elif getattr(p, 'image_mask', None) is not None: # standard
|
||||
p.mask = run_mask(input_image=p.init_images, input_mask=p.image_mask, return_type='Grayscale')
|
||||
p.mask = masking.run_mask(input_image=p.init_images, input_mask=p.image_mask, return_type='Grayscale')
|
||||
elif getattr(p, 'mask', None) is not None: # backward compatibility
|
||||
pass
|
||||
else: # fallback
|
||||
@@ -183,13 +177,12 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
'strength': p.denoising_strength,
|
||||
'height': height,
|
||||
'width': width,
|
||||
# 'padding_mask_crop': p.inpaint_full_res_padding # done back in main processing method
|
||||
}
|
||||
if model.__class__.__name__ == 'LatentConsistencyModelPipeline' and hasattr(p, 'init_images') and len(p.init_images) > 0:
|
||||
p.ops.append('lcm')
|
||||
init_latents = [vae_encode(image, model=shared.sd_model, full_quality=p.full_quality).squeeze(dim=0) for image in p.init_images]
|
||||
init_latents = [processing_vae.vae_encode(image, model=shared.sd_model, full_quality=p.full_quality).squeeze(dim=0) for image in p.init_images]
|
||||
init_latent = torch.stack(init_latents, dim=0).to(shared.device)
|
||||
init_noise = p.denoising_strength * create_random_tensors(init_latent.shape[1:], seeds=p.all_seeds, subseeds=p.all_subseeds, subseed_strength=p.subseed_strength, p=p)
|
||||
init_noise = p.denoising_strength * processing.create_random_tensors(init_latent.shape[1:], seeds=p.all_seeds, subseeds=p.all_subseeds, subseed_strength=p.subseed_strength, p=p)
|
||||
init_latent = (1 - p.denoising_strength) * init_latent + init_noise
|
||||
task_args = {
|
||||
'latents': init_latent.to(model.dtype),
|
||||
@@ -276,7 +269,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
else:
|
||||
debug(f'Diffusers unknown task args: {k}={v}')
|
||||
|
||||
hypertile_set(p, hr=len(getattr(p, 'init_images', [])) > 0)
|
||||
sd_hijack_hypertile.hypertile_set(p, hr=len(getattr(p, 'init_images', [])) > 0)
|
||||
clean = args.copy()
|
||||
clean.pop('callback', None)
|
||||
clean.pop('callback_steps', None)
|
||||
@@ -387,7 +380,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
sampler_options.append('low order')
|
||||
p.extra_generation_params['Sampler options'] = '/'.join(sampler_options)
|
||||
|
||||
def update_pipeline(sd_model, p: StableDiffusionProcessing):
|
||||
def update_pipeline(sd_model, p: processing.StableDiffusionProcessing):
|
||||
if p.sag_scale > 0 and is_txt2img():
|
||||
update_sampler(shared.sd_model)
|
||||
supported = ['DDIMScheduler', 'PNDMScheduler', 'DDPMScheduler', 'DEISMultistepScheduler', 'UniPCMultistepScheduler', 'DPMSolverMultistepScheduler', 'DPMSolverSinlgestepScheduler']
|
||||
@@ -591,7 +584,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
noise_level = round(350 * p.denoising_strength)
|
||||
output_type='latent' if hasattr(shared.sd_refiner, 'vae') else 'np'
|
||||
if shared.sd_refiner.__class__.__name__ == 'StableDiffusionUpscalePipeline':
|
||||
image = vae_decode(latents=image, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
image = processing_vae.vae_decode(latents=image, model=shared.sd_model, full_quality=p.full_quality, output_type='pil')
|
||||
p.extra_generation_params['Noise level'] = noise_level
|
||||
output_type = 'np'
|
||||
refiner_args = set_pipeline_args(
|
||||
@@ -620,7 +613,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
shared.log.info(e)
|
||||
|
||||
if not shared.state.interrupted and not shared.state.skipped:
|
||||
refiner_images = vae_decode(latents=refiner_output.images, model=shared.sd_refiner, full_quality=True)
|
||||
refiner_images = processing_vae.vae_decode(latents=refiner_output.images, model=shared.sd_refiner, full_quality=True)
|
||||
for refiner_image in refiner_images:
|
||||
results.append(refiner_image)
|
||||
|
||||
@@ -639,7 +632,7 @@ def process_diffusers(p: StableDiffusionProcessing):
|
||||
shared.log.debug(f'Generated: frames={len(output.frames[0])}')
|
||||
output.images = output.frames[0]
|
||||
if output.images is not None and len(output.images) > 0:
|
||||
results = vae_decode(latents=output.images, model=shared.sd_model, full_quality=p.full_quality)
|
||||
results = processing_vae.vae_decode(latents=output.images, model=shared.sd_model, full_quality=p.full_quality)
|
||||
else:
|
||||
shared.log.warning('Processing returned no results')
|
||||
results = []
|
||||
|
||||
@@ -82,7 +82,7 @@ class IFNet(nn.Module):
|
||||
# self.contextnet = Contextnet()
|
||||
# self.unet = Unet()
|
||||
|
||||
def forward( self, x, timestep=0.5, scale_list=[8, 4, 2, 1], training=False, fastmode=True, ensemble=False): # pylint: disable=dangerous-default-value # noqa: B006
|
||||
def forward( self, x, timestep=0.5, scale_list=[8, 4, 2, 1], training=False, fastmode=True, ensemble=False): # pylint: disable=dangerous-default-value, unused-argument # noqa: B006
|
||||
if training is False:
|
||||
channel = x.shape[1] // 2
|
||||
img0 = x[:, :channel]
|
||||
@@ -122,13 +122,4 @@ class IFNet(nn.Module):
|
||||
merged.append((warped_img0, warped_img1))
|
||||
mask_list[3] = torch.sigmoid(mask_list[3])
|
||||
merged[3] = merged[3][0] * mask_list[3] + merged[3][1] * (1 - mask_list[3])
|
||||
if not fastmode:
|
||||
print('contextnet is removed')
|
||||
'''
|
||||
c0 = self.contextnet(img0, flow[:, :2])
|
||||
c1 = self.contextnet(img1, flow[:, 2:4])
|
||||
tmp = self.unet(img0, img1, warped_img0, warped_img1, mask, flow, c0, c1)
|
||||
res = tmp[:, :3] * 2 - 1
|
||||
merged[3] = torch.clamp(merged[3] + res, 0, 1)
|
||||
'''
|
||||
return flow_list, mask_list[3], merged
|
||||
|
||||
@@ -715,6 +715,7 @@ class Options:
|
||||
self.data_labels[key].onchange()
|
||||
except Exception as e:
|
||||
log.error(f'Error in onchange callback: {key} {value} {e}')
|
||||
errors.display(e, 'Error in onchange callback')
|
||||
setattr(self, key, oldval)
|
||||
return False
|
||||
return True
|
||||
|
||||
@@ -234,7 +234,6 @@ def create_resize_inputs(tab, images, scale_visible=True, mode=None, accordion=T
|
||||
res_switch_btn = ToolButton(value=ui_symbols.switch, elem_id=f"{tab}_res_switch_btn")
|
||||
res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
|
||||
detect_image_size_btn = ToolButton(value=ui_symbols.detect, elem_id=f"{tab}_detect_image_size_btn")
|
||||
print('HERE', tab)
|
||||
detect_image_size_btn.click(fn=lambda w, h, _: (w or gr.update(), h or gr.update()), _js=f'currentImageResolution{tab}', inputs=[dummy_component, dummy_component, dummy_component], outputs=[width, height], show_progress=False)
|
||||
|
||||
with gr.Tab(label="Scale") as tab_scale_by:
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import math
|
||||
import time
|
||||
from rich.progress import Progress, TextColumn, BarColumn, TaskProgressColumn, TimeRemainingColumn, TimeElapsedColumn
|
||||
@@ -565,7 +564,6 @@ class UniPC:
|
||||
A_p = C_inv_p
|
||||
|
||||
if use_corrector:
|
||||
#print('using corrector')
|
||||
C_inv = torch.linalg.inv(C)
|
||||
A_c = C_inv
|
||||
|
||||
@@ -692,7 +690,6 @@ class UniPC:
|
||||
D1s = None
|
||||
|
||||
if use_corrector:
|
||||
#print('using corrector')
|
||||
# for order 1, we use a simplified version
|
||||
if order == 1:
|
||||
rhos_c = torch.tensor([0.5], device=b.device)
|
||||
@@ -782,9 +779,7 @@ class UniPC:
|
||||
step_order = min(order, steps + 1 - step)
|
||||
else:
|
||||
step_order = order
|
||||
#print('this step order:', step_order)
|
||||
if step == steps:
|
||||
#print('do not run corrector at the last step')
|
||||
use_corrector = False
|
||||
else:
|
||||
use_corrector = True
|
||||
|
||||
Reference in New Issue
Block a user