2022-09-14 11:47:54 +00:00
|
|
|
import numpy as np
|
|
|
|
from tqdm import trange
|
|
|
|
|
|
|
|
import modules.scripts as scripts
|
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
from modules import processing, shared, sd_samplers, images
|
|
|
|
from modules.processing import Processed
|
|
|
|
from modules.sd_samplers import samplers
|
|
|
|
from modules.shared import opts, cmd_opts, state
|
|
|
|
|
|
|
|
class Script(scripts.Script):
|
|
|
|
def title(self):
|
|
|
|
return "Loopback"
|
|
|
|
|
|
|
|
def show(self, is_img2img):
|
|
|
|
return is_img2img
|
|
|
|
|
|
|
|
def ui(self, is_img2img):
|
2023-01-05 07:27:09 +00:00
|
|
|
elem_prefix = 'script_loopback_'
|
2023-01-04 21:03:32 +00:00
|
|
|
|
|
|
|
loops = gr.Slider(minimum=1, maximum=32, step=1, label='Loops', value=4, elem_id=elem_prefix + "loops")
|
|
|
|
denoising_strength_change_factor = gr.Slider(minimum=0.9, maximum=1.1, step=0.01, label='Denoising strength change factor', value=1, elem_id=elem_prefix + "denoising_strength_change_factor")
|
2022-09-14 11:47:54 +00:00
|
|
|
|
|
|
|
return [loops, denoising_strength_change_factor]
|
|
|
|
|
|
|
|
def run(self, p, loops, denoising_strength_change_factor):
|
|
|
|
processing.fix_seed(p)
|
|
|
|
batch_count = p.n_iter
|
|
|
|
p.extra_generation_params = {
|
|
|
|
"Denoising strength change factor": denoising_strength_change_factor,
|
|
|
|
}
|
|
|
|
|
|
|
|
p.batch_size = 1
|
|
|
|
p.n_iter = 1
|
|
|
|
|
|
|
|
output_images, info = None, None
|
|
|
|
initial_seed = None
|
|
|
|
initial_info = None
|
|
|
|
|
|
|
|
grids = []
|
|
|
|
all_images = []
|
2022-10-11 10:23:47 +00:00
|
|
|
original_init_image = p.init_images
|
2022-09-14 11:47:54 +00:00
|
|
|
state.job_count = loops * batch_count
|
|
|
|
|
2022-09-17 22:18:30 +00:00
|
|
|
initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
|
2022-09-16 05:33:47 +00:00
|
|
|
|
2022-09-14 11:47:54 +00:00
|
|
|
for n in range(batch_count):
|
|
|
|
history = []
|
|
|
|
|
2022-10-11 10:23:47 +00:00
|
|
|
# Reset to original init image at the start of each batch
|
|
|
|
p.init_images = original_init_image
|
|
|
|
|
2022-09-14 11:47:54 +00:00
|
|
|
for i in range(loops):
|
|
|
|
p.n_iter = 1
|
|
|
|
p.batch_size = 1
|
|
|
|
p.do_not_save_grid = True
|
2022-09-17 22:20:43 +00:00
|
|
|
|
|
|
|
if opts.img2img_color_correction:
|
|
|
|
p.color_corrections = initial_color_corrections
|
2022-09-14 11:47:54 +00:00
|
|
|
|
|
|
|
state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"
|
|
|
|
|
|
|
|
processed = processing.process_images(p)
|
|
|
|
|
|
|
|
if initial_seed is None:
|
|
|
|
initial_seed = processed.seed
|
|
|
|
initial_info = processed.info
|
|
|
|
|
|
|
|
init_img = processed.images[0]
|
|
|
|
|
|
|
|
p.init_images = [init_img]
|
|
|
|
p.seed = processed.seed + 1
|
|
|
|
p.denoising_strength = min(max(p.denoising_strength * denoising_strength_change_factor, 0.1), 1)
|
|
|
|
history.append(processed.images[0])
|
|
|
|
|
|
|
|
grid = images.image_grid(history, rows=1)
|
|
|
|
if opts.grid_save:
|
|
|
|
images.save_image(grid, p.outpath_grids, "grid", initial_seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p)
|
|
|
|
|
|
|
|
grids.append(grid)
|
|
|
|
all_images += history
|
|
|
|
|
|
|
|
if opts.return_grid:
|
|
|
|
all_images = grids + all_images
|
|
|
|
|
|
|
|
processed = Processed(p, all_images, initial_seed, initial_info)
|
|
|
|
|
|
|
|
return processed
|