stable-diffusion-webui
140 строк · 5.2 Кб
1import math
2
3import gradio as gr
4import modules.scripts as scripts
5from modules import deepbooru, images, processing, shared
6from modules.processing import Processed
7from modules.shared import opts, state
8
9
10class Script(scripts.Script):
11def title(self):
12return "Loopback"
13
14def show(self, is_img2img):
15return is_img2img
16
17def ui(self, is_img2img):
18loops = gr.Slider(minimum=1, maximum=32, step=1, label='Loops', value=4, elem_id=self.elem_id("loops"))
19final_denoising_strength = gr.Slider(minimum=0, maximum=1, step=0.01, label='Final denoising strength', value=0.5, elem_id=self.elem_id("final_denoising_strength"))
20denoising_curve = gr.Dropdown(label="Denoising strength curve", choices=["Aggressive", "Linear", "Lazy"], value="Linear")
21append_interrogation = gr.Dropdown(label="Append interrogated prompt at each iteration", choices=["None", "CLIP", "DeepBooru"], value="None")
22
23return [loops, final_denoising_strength, denoising_curve, append_interrogation]
24
25def run(self, p, loops, final_denoising_strength, denoising_curve, append_interrogation):
26processing.fix_seed(p)
27batch_count = p.n_iter
28p.extra_generation_params = {
29"Final denoising strength": final_denoising_strength,
30"Denoising curve": denoising_curve
31}
32
33p.batch_size = 1
34p.n_iter = 1
35
36info = None
37initial_seed = None
38initial_info = None
39initial_denoising_strength = p.denoising_strength
40
41grids = []
42all_images = []
43original_init_image = p.init_images
44original_prompt = p.prompt
45original_inpainting_fill = p.inpainting_fill
46state.job_count = loops * batch_count
47
48initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
49
50def calculate_denoising_strength(loop):
51strength = initial_denoising_strength
52
53if loops == 1:
54return strength
55
56progress = loop / (loops - 1)
57if denoising_curve == "Aggressive":
58strength = math.sin((progress) * math.pi * 0.5)
59elif denoising_curve == "Lazy":
60strength = 1 - math.cos((progress) * math.pi * 0.5)
61else:
62strength = progress
63
64change = (final_denoising_strength - initial_denoising_strength) * strength
65return initial_denoising_strength + change
66
67history = []
68
69for n in range(batch_count):
70# Reset to original init image at the start of each batch
71p.init_images = original_init_image
72
73# Reset to original denoising strength
74p.denoising_strength = initial_denoising_strength
75
76last_image = None
77
78for i in range(loops):
79p.n_iter = 1
80p.batch_size = 1
81p.do_not_save_grid = True
82
83if opts.img2img_color_correction:
84p.color_corrections = initial_color_corrections
85
86if append_interrogation != "None":
87p.prompt = f"{original_prompt}, " if original_prompt else ""
88if append_interrogation == "CLIP":
89p.prompt += shared.interrogator.interrogate(p.init_images[0])
90elif append_interrogation == "DeepBooru":
91p.prompt += deepbooru.model.tag(p.init_images[0])
92
93state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"
94
95processed = processing.process_images(p)
96
97# Generation cancelled.
98if state.interrupted or state.stopping_generation:
99break
100
101if initial_seed is None:
102initial_seed = processed.seed
103initial_info = processed.info
104
105p.seed = processed.seed + 1
106p.denoising_strength = calculate_denoising_strength(i + 1)
107
108if state.skipped:
109break
110
111last_image = processed.images[0]
112p.init_images = [last_image]
113p.inpainting_fill = 1 # Set "masked content" to "original" for next loop.
114
115if batch_count == 1:
116history.append(last_image)
117all_images.append(last_image)
118
119if batch_count > 1 and not state.skipped and not state.interrupted:
120history.append(last_image)
121all_images.append(last_image)
122
123p.inpainting_fill = original_inpainting_fill
124
125if state.interrupted or state.stopping_generation:
126break
127
128if len(history) > 1:
129grid = images.image_grid(history, rows=1)
130if opts.grid_save:
131images.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)
132
133if opts.return_grid:
134grids.append(grid)
135
136all_images = grids + all_images
137
138processed = Processed(p, all_images, initial_seed, initial_info)
139
140return processed
141