xyz_grid.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. from collections import namedtuple
  2. from copy import copy
  3. from itertools import permutations, chain
  4. import random
  5. import csv
  6. from io import StringIO
  7. from PIL import Image
  8. import numpy as np
  9. import modules.scripts as scripts
  10. import gradio as gr
  11. from modules import images, paths, sd_samplers, processing, sd_models, sd_vae
  12. from modules.processing import process_images, Processed, StableDiffusionProcessingTxt2Img
  13. from modules.shared import opts, cmd_opts, state
  14. import modules.shared as shared
  15. import modules.sd_samplers
  16. import modules.sd_models
  17. import modules.sd_vae
  18. import glob
  19. import os
  20. import re
  21. from modules.ui_components import ToolButton
  22. fill_values_symbol = "\U0001f4d2" # 📒
  23. AxisInfo = namedtuple('AxisInfo', ['axis', 'values'])
  24. def apply_field(field):
  25. def fun(p, x, xs):
  26. setattr(p, field, x)
  27. return fun
  28. def apply_prompt(p, x, xs):
  29. if xs[0] not in p.prompt and xs[0] not in p.negative_prompt:
  30. raise RuntimeError(f"Prompt S/R did not find {xs[0]} in prompt or negative prompt.")
  31. p.prompt = p.prompt.replace(xs[0], x)
  32. p.negative_prompt = p.negative_prompt.replace(xs[0], x)
  33. def apply_order(p, x, xs):
  34. token_order = []
  35. # Initally grab the tokens from the prompt, so they can be replaced in order of earliest seen
  36. for token in x:
  37. token_order.append((p.prompt.find(token), token))
  38. token_order.sort(key=lambda t: t[0])
  39. prompt_parts = []
  40. # Split the prompt up, taking out the tokens
  41. for _, token in token_order:
  42. n = p.prompt.find(token)
  43. prompt_parts.append(p.prompt[0:n])
  44. p.prompt = p.prompt[n + len(token):]
  45. # Rebuild the prompt with the tokens in the order we want
  46. prompt_tmp = ""
  47. for idx, part in enumerate(prompt_parts):
  48. prompt_tmp += part
  49. prompt_tmp += x[idx]
  50. p.prompt = prompt_tmp + p.prompt
  51. def apply_sampler(p, x, xs):
  52. sampler_name = sd_samplers.samplers_map.get(x.lower(), None)
  53. if sampler_name is None:
  54. raise RuntimeError(f"Unknown sampler: {x}")
  55. p.sampler_name = sampler_name
  56. def confirm_samplers(p, xs):
  57. for x in xs:
  58. if x.lower() not in sd_samplers.samplers_map:
  59. raise RuntimeError(f"Unknown sampler: {x}")
  60. def apply_checkpoint(p, x, xs):
  61. info = modules.sd_models.get_closet_checkpoint_match(x)
  62. if info is None:
  63. raise RuntimeError(f"Unknown checkpoint: {x}")
  64. modules.sd_models.reload_model_weights(shared.sd_model, info)
  65. def confirm_checkpoints(p, xs):
  66. for x in xs:
  67. if modules.sd_models.get_closet_checkpoint_match(x) is None:
  68. raise RuntimeError(f"Unknown checkpoint: {x}")
  69. def apply_clip_skip(p, x, xs):
  70. opts.data["CLIP_stop_at_last_layers"] = x
  71. def apply_upscale_latent_space(p, x, xs):
  72. if x.lower().strip() != '0':
  73. opts.data["use_scale_latent_for_hires_fix"] = True
  74. else:
  75. opts.data["use_scale_latent_for_hires_fix"] = False
  76. def find_vae(name: str):
  77. if name.lower() in ['auto', 'automatic']:
  78. return modules.sd_vae.unspecified
  79. if name.lower() == 'none':
  80. return None
  81. else:
  82. choices = [x for x in sorted(modules.sd_vae.vae_dict, key=lambda x: len(x)) if name.lower().strip() in x.lower()]
  83. if len(choices) == 0:
  84. print(f"No VAE found for {name}; using automatic")
  85. return modules.sd_vae.unspecified
  86. else:
  87. return modules.sd_vae.vae_dict[choices[0]]
  88. def apply_vae(p, x, xs):
  89. modules.sd_vae.reload_vae_weights(shared.sd_model, vae_file=find_vae(x))
  90. def apply_styles(p: StableDiffusionProcessingTxt2Img, x: str, _):
  91. p.styles.extend(x.split(','))
  92. def apply_uni_pc_order(p, x, xs):
  93. opts.data["uni_pc_order"] = min(x, p.steps - 1)
  94. def apply_face_restore(p, opt, x):
  95. opt = opt.lower()
  96. if opt == 'codeformer':
  97. is_active = True
  98. p.face_restoration_model = 'CodeFormer'
  99. elif opt == 'gfpgan':
  100. is_active = True
  101. p.face_restoration_model = 'GFPGAN'
  102. else:
  103. is_active = opt in ('true', 'yes', 'y', '1')
  104. p.restore_faces = is_active
  105. def format_value_add_label(p, opt, x):
  106. if type(x) == float:
  107. x = round(x, 8)
  108. return f"{opt.label}: {x}"
  109. def format_value(p, opt, x):
  110. if type(x) == float:
  111. x = round(x, 8)
  112. return x
  113. def format_value_join_list(p, opt, x):
  114. return ", ".join(x)
  115. def do_nothing(p, x, xs):
  116. pass
  117. def format_nothing(p, opt, x):
  118. return ""
  119. def str_permutations(x):
  120. """dummy function for specifying it in AxisOption's type when you want to get a list of permutations"""
  121. return x
  122. class AxisOption:
  123. def __init__(self, label, type, apply, format_value=format_value_add_label, confirm=None, cost=0.0, choices=None):
  124. self.label = label
  125. self.type = type
  126. self.apply = apply
  127. self.format_value = format_value
  128. self.confirm = confirm
  129. self.cost = cost
  130. self.choices = choices
  131. class AxisOptionImg2Img(AxisOption):
  132. def __init__(self, *args, **kwargs):
  133. super().__init__(*args, **kwargs)
  134. self.is_img2img = True
  135. class AxisOptionTxt2Img(AxisOption):
  136. def __init__(self, *args, **kwargs):
  137. super().__init__(*args, **kwargs)
  138. self.is_img2img = False
  139. axis_options = [
  140. AxisOption("Nothing", str, do_nothing, format_value=format_nothing),
  141. AxisOption("Seed", int, apply_field("seed")),
  142. AxisOption("Var. seed", int, apply_field("subseed")),
  143. AxisOption("Var. strength", float, apply_field("subseed_strength")),
  144. AxisOption("Steps", int, apply_field("steps")),
  145. AxisOptionTxt2Img("Hires steps", int, apply_field("hr_second_pass_steps")),
  146. AxisOption("CFG Scale", float, apply_field("cfg_scale")),
  147. AxisOptionImg2Img("Image CFG Scale", float, apply_field("image_cfg_scale")),
  148. AxisOption("Prompt S/R", str, apply_prompt, format_value=format_value),
  149. AxisOption("Prompt order", str_permutations, apply_order, format_value=format_value_join_list),
  150. AxisOptionTxt2Img("Sampler", str, apply_sampler, format_value=format_value, confirm=confirm_samplers, choices=lambda: [x.name for x in sd_samplers.samplers]),
  151. AxisOptionImg2Img("Sampler", str, apply_sampler, format_value=format_value, confirm=confirm_samplers, choices=lambda: [x.name for x in sd_samplers.samplers_for_img2img]),
  152. AxisOption("Checkpoint name", str, apply_checkpoint, format_value=format_value, confirm=confirm_checkpoints, cost=1.0, choices=lambda: list(sd_models.checkpoints_list)),
  153. AxisOption("Sigma Churn", float, apply_field("s_churn")),
  154. AxisOption("Sigma min", float, apply_field("s_tmin")),
  155. AxisOption("Sigma max", float, apply_field("s_tmax")),
  156. AxisOption("Sigma noise", float, apply_field("s_noise")),
  157. AxisOption("Eta", float, apply_field("eta")),
  158. AxisOption("Clip skip", int, apply_clip_skip),
  159. AxisOption("Denoising", float, apply_field("denoising_strength")),
  160. AxisOptionTxt2Img("Hires upscaler", str, apply_field("hr_upscaler"), choices=lambda: [*shared.latent_upscale_modes, *[x.name for x in shared.sd_upscalers]]),
  161. AxisOptionImg2Img("Cond. Image Mask Weight", float, apply_field("inpainting_mask_weight")),
  162. AxisOption("VAE", str, apply_vae, cost=0.7, choices=lambda: list(sd_vae.vae_dict)),
  163. AxisOption("Styles", str, apply_styles, choices=lambda: list(shared.prompt_styles.styles)),
  164. AxisOption("UniPC Order", int, apply_uni_pc_order, cost=0.5),
  165. AxisOption("Face restore", str, apply_face_restore, format_value=format_value),
  166. ]
  167. def draw_xyz_grid(p, xs, ys, zs, x_labels, y_labels, z_labels, cell, draw_legend, include_lone_images, include_sub_grids, first_axes_processed, second_axes_processed, margin_size):
  168. hor_texts = [[images.GridAnnotation(x)] for x in x_labels]
  169. ver_texts = [[images.GridAnnotation(y)] for y in y_labels]
  170. title_texts = [[images.GridAnnotation(z)] for z in z_labels]
  171. list_size = (len(xs) * len(ys) * len(zs))
  172. processed_result = None
  173. state.job_count = list_size * p.n_iter
  174. def process_cell(x, y, z, ix, iy, iz):
  175. nonlocal processed_result
  176. def index(ix, iy, iz):
  177. return ix + iy * len(xs) + iz * len(xs) * len(ys)
  178. state.job = f"{index(ix, iy, iz) + 1} out of {list_size}"
  179. processed: Processed = cell(x, y, z, ix, iy, iz)
  180. if processed_result is None:
  181. # Use our first processed result object as a template container to hold our full results
  182. processed_result = copy(processed)
  183. processed_result.images = [None] * list_size
  184. processed_result.all_prompts = [None] * list_size
  185. processed_result.all_seeds = [None] * list_size
  186. processed_result.infotexts = [None] * list_size
  187. processed_result.index_of_first_image = 1
  188. idx = index(ix, iy, iz)
  189. if processed.images:
  190. # Non-empty list indicates some degree of success.
  191. processed_result.images[idx] = processed.images[0]
  192. processed_result.all_prompts[idx] = processed.prompt
  193. processed_result.all_seeds[idx] = processed.seed
  194. processed_result.infotexts[idx] = processed.infotexts[0]
  195. else:
  196. cell_mode = "P"
  197. cell_size = (processed_result.width, processed_result.height)
  198. if processed_result.images[0] is not None:
  199. cell_mode = processed_result.images[0].mode
  200. #This corrects size in case of batches:
  201. cell_size = processed_result.images[0].size
  202. processed_result.images[idx] = Image.new(cell_mode, cell_size)
  203. if first_axes_processed == 'x':
  204. for ix, x in enumerate(xs):
  205. if second_axes_processed == 'y':
  206. for iy, y in enumerate(ys):
  207. for iz, z in enumerate(zs):
  208. process_cell(x, y, z, ix, iy, iz)
  209. else:
  210. for iz, z in enumerate(zs):
  211. for iy, y in enumerate(ys):
  212. process_cell(x, y, z, ix, iy, iz)
  213. elif first_axes_processed == 'y':
  214. for iy, y in enumerate(ys):
  215. if second_axes_processed == 'x':
  216. for ix, x in enumerate(xs):
  217. for iz, z in enumerate(zs):
  218. process_cell(x, y, z, ix, iy, iz)
  219. else:
  220. for iz, z in enumerate(zs):
  221. for ix, x in enumerate(xs):
  222. process_cell(x, y, z, ix, iy, iz)
  223. elif first_axes_processed == 'z':
  224. for iz, z in enumerate(zs):
  225. if second_axes_processed == 'x':
  226. for ix, x in enumerate(xs):
  227. for iy, y in enumerate(ys):
  228. process_cell(x, y, z, ix, iy, iz)
  229. else:
  230. for iy, y in enumerate(ys):
  231. for ix, x in enumerate(xs):
  232. process_cell(x, y, z, ix, iy, iz)
  233. if not processed_result:
  234. # Should never happen, I've only seen it on one of four open tabs and it needed to refresh.
  235. print("Unexpected error: Processing could not begin, you may need to refresh the tab or restart the service.")
  236. return Processed(p, [])
  237. elif not any(processed_result.images):
  238. print("Unexpected error: draw_xyz_grid failed to return even a single processed image")
  239. return Processed(p, [])
  240. z_count = len(zs)
  241. sub_grids = [None] * z_count
  242. for i in range(z_count):
  243. start_index = (i * len(xs) * len(ys)) + i
  244. end_index = start_index + len(xs) * len(ys)
  245. grid = images.image_grid(processed_result.images[start_index:end_index], rows=len(ys))
  246. if draw_legend:
  247. grid = images.draw_grid_annotations(grid, processed_result.images[start_index].size[0], processed_result.images[start_index].size[1], hor_texts, ver_texts, margin_size)
  248. processed_result.images.insert(i, grid)
  249. processed_result.all_prompts.insert(i, processed_result.all_prompts[start_index])
  250. processed_result.all_seeds.insert(i, processed_result.all_seeds[start_index])
  251. processed_result.infotexts.insert(i, processed_result.infotexts[start_index])
  252. sub_grid_size = processed_result.images[0].size
  253. z_grid = images.image_grid(processed_result.images[:z_count], rows=1)
  254. if draw_legend:
  255. z_grid = images.draw_grid_annotations(z_grid, sub_grid_size[0], sub_grid_size[1], title_texts, [[images.GridAnnotation()]])
  256. processed_result.images.insert(0, z_grid)
  257. #TODO: Deeper aspects of the program rely on grid info being misaligned between metadata arrays, which is not ideal.
  258. #processed_result.all_prompts.insert(0, processed_result.all_prompts[0])
  259. #processed_result.all_seeds.insert(0, processed_result.all_seeds[0])
  260. processed_result.infotexts.insert(0, processed_result.infotexts[0])
  261. return processed_result
  262. class SharedSettingsStackHelper(object):
  263. def __enter__(self):
  264. self.CLIP_stop_at_last_layers = opts.CLIP_stop_at_last_layers
  265. self.vae = opts.sd_vae
  266. self.uni_pc_order = opts.uni_pc_order
  267. def __exit__(self, exc_type, exc_value, tb):
  268. opts.data["sd_vae"] = self.vae
  269. opts.data["uni_pc_order"] = self.uni_pc_order
  270. modules.sd_models.reload_model_weights()
  271. modules.sd_vae.reload_vae_weights()
  272. opts.data["CLIP_stop_at_last_layers"] = self.CLIP_stop_at_last_layers
  273. re_range = re.compile(r"\s*([+-]?\s*\d+)\s*-\s*([+-]?\s*\d+)(?:\s*\(([+-]\d+)\s*\))?\s*")
  274. re_range_float = re.compile(r"\s*([+-]?\s*\d+(?:.\d*)?)\s*-\s*([+-]?\s*\d+(?:.\d*)?)(?:\s*\(([+-]\d+(?:.\d*)?)\s*\))?\s*")
  275. re_range_count = re.compile(r"\s*([+-]?\s*\d+)\s*-\s*([+-]?\s*\d+)(?:\s*\[(\d+)\s*\])?\s*")
  276. re_range_count_float = re.compile(r"\s*([+-]?\s*\d+(?:.\d*)?)\s*-\s*([+-]?\s*\d+(?:.\d*)?)(?:\s*\[(\d+(?:.\d*)?)\s*\])?\s*")
  277. class Script(scripts.Script):
  278. def title(self):
  279. return "X/Y/Z plot"
  280. def ui(self, is_img2img):
  281. self.current_axis_options = [x for x in axis_options if type(x) == AxisOption or x.is_img2img == is_img2img]
  282. with gr.Row():
  283. with gr.Column(scale=19):
  284. with gr.Row():
  285. x_type = gr.Dropdown(label="X type", choices=[x.label for x in self.current_axis_options], value=self.current_axis_options[1].label, type="index", elem_id=self.elem_id("x_type"))
  286. x_values = gr.Textbox(label="X values", lines=1, elem_id=self.elem_id("x_values"))
  287. fill_x_button = ToolButton(value=fill_values_symbol, elem_id="xyz_grid_fill_x_tool_button", visible=False)
  288. with gr.Row():
  289. y_type = gr.Dropdown(label="Y type", choices=[x.label for x in self.current_axis_options], value=self.current_axis_options[0].label, type="index", elem_id=self.elem_id("y_type"))
  290. y_values = gr.Textbox(label="Y values", lines=1, elem_id=self.elem_id("y_values"))
  291. fill_y_button = ToolButton(value=fill_values_symbol, elem_id="xyz_grid_fill_y_tool_button", visible=False)
  292. with gr.Row():
  293. z_type = gr.Dropdown(label="Z type", choices=[x.label for x in self.current_axis_options], value=self.current_axis_options[0].label, type="index", elem_id=self.elem_id("z_type"))
  294. z_values = gr.Textbox(label="Z values", lines=1, elem_id=self.elem_id("z_values"))
  295. fill_z_button = ToolButton(value=fill_values_symbol, elem_id="xyz_grid_fill_z_tool_button", visible=False)
  296. with gr.Row(variant="compact", elem_id="axis_options"):
  297. with gr.Column():
  298. draw_legend = gr.Checkbox(label='Draw legend', value=True, elem_id=self.elem_id("draw_legend"))
  299. no_fixed_seeds = gr.Checkbox(label='Keep -1 for seeds', value=False, elem_id=self.elem_id("no_fixed_seeds"))
  300. with gr.Column():
  301. include_lone_images = gr.Checkbox(label='Include Sub Images', value=False, elem_id=self.elem_id("include_lone_images"))
  302. include_sub_grids = gr.Checkbox(label='Include Sub Grids', value=False, elem_id=self.elem_id("include_sub_grids"))
  303. with gr.Column():
  304. margin_size = gr.Slider(label="Grid margins (px)", minimum=0, maximum=500, value=0, step=2, elem_id=self.elem_id("margin_size"))
  305. with gr.Row(variant="compact", elem_id="swap_axes"):
  306. swap_xy_axes_button = gr.Button(value="Swap X/Y axes", elem_id="xy_grid_swap_axes_button")
  307. swap_yz_axes_button = gr.Button(value="Swap Y/Z axes", elem_id="yz_grid_swap_axes_button")
  308. swap_xz_axes_button = gr.Button(value="Swap X/Z axes", elem_id="xz_grid_swap_axes_button")
  309. def swap_axes(axis1_type, axis1_values, axis2_type, axis2_values):
  310. return self.current_axis_options[axis2_type].label, axis2_values, self.current_axis_options[axis1_type].label, axis1_values
  311. xy_swap_args = [x_type, x_values, y_type, y_values]
  312. swap_xy_axes_button.click(swap_axes, inputs=xy_swap_args, outputs=xy_swap_args)
  313. yz_swap_args = [y_type, y_values, z_type, z_values]
  314. swap_yz_axes_button.click(swap_axes, inputs=yz_swap_args, outputs=yz_swap_args)
  315. xz_swap_args = [x_type, x_values, z_type, z_values]
  316. swap_xz_axes_button.click(swap_axes, inputs=xz_swap_args, outputs=xz_swap_args)
  317. def fill(x_type):
  318. axis = self.current_axis_options[x_type]
  319. return ", ".join(axis.choices()) if axis.choices else gr.update()
  320. fill_x_button.click(fn=fill, inputs=[x_type], outputs=[x_values])
  321. fill_y_button.click(fn=fill, inputs=[y_type], outputs=[y_values])
  322. fill_z_button.click(fn=fill, inputs=[z_type], outputs=[z_values])
  323. def select_axis(x_type):
  324. return gr.Button.update(visible=self.current_axis_options[x_type].choices is not None)
  325. x_type.change(fn=select_axis, inputs=[x_type], outputs=[fill_x_button])
  326. y_type.change(fn=select_axis, inputs=[y_type], outputs=[fill_y_button])
  327. z_type.change(fn=select_axis, inputs=[z_type], outputs=[fill_z_button])
  328. self.infotext_fields = (
  329. (x_type, "X Type"),
  330. (x_values, "X Values"),
  331. (y_type, "Y Type"),
  332. (y_values, "Y Values"),
  333. (z_type, "Z Type"),
  334. (z_values, "Z Values"),
  335. )
  336. return [x_type, x_values, y_type, y_values, z_type, z_values, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, margin_size]
  337. def run(self, p, x_type, x_values, y_type, y_values, z_type, z_values, draw_legend, include_lone_images, include_sub_grids, no_fixed_seeds, margin_size):
  338. if not no_fixed_seeds:
  339. modules.processing.fix_seed(p)
  340. if not opts.return_grid:
  341. p.batch_size = 1
  342. def process_axis(opt, vals):
  343. if opt.label == 'Nothing':
  344. return [0]
  345. valslist = [x.strip() for x in chain.from_iterable(csv.reader(StringIO(vals))) if x]
  346. if opt.type == int:
  347. valslist_ext = []
  348. for val in valslist:
  349. m = re_range.fullmatch(val)
  350. mc = re_range_count.fullmatch(val)
  351. if m is not None:
  352. start = int(m.group(1))
  353. end = int(m.group(2))+1
  354. step = int(m.group(3)) if m.group(3) is not None else 1
  355. valslist_ext += list(range(start, end, step))
  356. elif mc is not None:
  357. start = int(mc.group(1))
  358. end = int(mc.group(2))
  359. num = int(mc.group(3)) if mc.group(3) is not None else 1
  360. valslist_ext += [int(x) for x in np.linspace(start=start, stop=end, num=num).tolist()]
  361. else:
  362. valslist_ext.append(val)
  363. valslist = valslist_ext
  364. elif opt.type == float:
  365. valslist_ext = []
  366. for val in valslist:
  367. m = re_range_float.fullmatch(val)
  368. mc = re_range_count_float.fullmatch(val)
  369. if m is not None:
  370. start = float(m.group(1))
  371. end = float(m.group(2))
  372. step = float(m.group(3)) if m.group(3) is not None else 1
  373. valslist_ext += np.arange(start, end + step, step).tolist()
  374. elif mc is not None:
  375. start = float(mc.group(1))
  376. end = float(mc.group(2))
  377. num = int(mc.group(3)) if mc.group(3) is not None else 1
  378. valslist_ext += np.linspace(start=start, stop=end, num=num).tolist()
  379. else:
  380. valslist_ext.append(val)
  381. valslist = valslist_ext
  382. elif opt.type == str_permutations:
  383. valslist = list(permutations(valslist))
  384. valslist = [opt.type(x) for x in valslist]
  385. # Confirm options are valid before starting
  386. if opt.confirm:
  387. opt.confirm(p, valslist)
  388. return valslist
  389. x_opt = self.current_axis_options[x_type]
  390. xs = process_axis(x_opt, x_values)
  391. y_opt = self.current_axis_options[y_type]
  392. ys = process_axis(y_opt, y_values)
  393. z_opt = self.current_axis_options[z_type]
  394. zs = process_axis(z_opt, z_values)
  395. # this could be moved to common code, but unlikely to be ever triggered anywhere else
  396. Image.MAX_IMAGE_PIXELS = None # disable check in Pillow and rely on check below to allow large custom image sizes
  397. grid_mp = round(len(xs) * len(ys) * len(zs) * p.width * p.height / 1000000)
  398. assert grid_mp < opts.img_max_size_mp, f'Error: Resulting grid would be too large ({grid_mp} MPixels) (max configured size is {opts.img_max_size_mp} MPixels)'
  399. def fix_axis_seeds(axis_opt, axis_list):
  400. if axis_opt.label in ['Seed', 'Var. seed']:
  401. return [int(random.randrange(4294967294)) if val is None or val == '' or val == -1 else val for val in axis_list]
  402. else:
  403. return axis_list
  404. if not no_fixed_seeds:
  405. xs = fix_axis_seeds(x_opt, xs)
  406. ys = fix_axis_seeds(y_opt, ys)
  407. zs = fix_axis_seeds(z_opt, zs)
  408. if x_opt.label == 'Steps':
  409. total_steps = sum(xs) * len(ys) * len(zs)
  410. elif y_opt.label == 'Steps':
  411. total_steps = sum(ys) * len(xs) * len(zs)
  412. elif z_opt.label == 'Steps':
  413. total_steps = sum(zs) * len(xs) * len(ys)
  414. else:
  415. total_steps = p.steps * len(xs) * len(ys) * len(zs)
  416. if isinstance(p, StableDiffusionProcessingTxt2Img) and p.enable_hr:
  417. if x_opt.label == "Hires steps":
  418. total_steps += sum(xs) * len(ys) * len(zs)
  419. elif y_opt.label == "Hires steps":
  420. total_steps += sum(ys) * len(xs) * len(zs)
  421. elif z_opt.label == "Hires steps":
  422. total_steps += sum(zs) * len(xs) * len(ys)
  423. elif p.hr_second_pass_steps:
  424. total_steps += p.hr_second_pass_steps * len(xs) * len(ys) * len(zs)
  425. else:
  426. total_steps *= 2
  427. total_steps *= p.n_iter
  428. image_cell_count = p.n_iter * p.batch_size
  429. cell_console_text = f"; {image_cell_count} images per cell" if image_cell_count > 1 else ""
  430. plural_s = 's' if len(zs) > 1 else ''
  431. print(f"X/Y/Z plot will create {len(xs) * len(ys) * len(zs) * image_cell_count} images on {len(zs)} {len(xs)}x{len(ys)} grid{plural_s}{cell_console_text}. (Total steps to process: {total_steps})")
  432. shared.total_tqdm.updateTotal(total_steps)
  433. state.xyz_plot_x = AxisInfo(x_opt, xs)
  434. state.xyz_plot_y = AxisInfo(y_opt, ys)
  435. state.xyz_plot_z = AxisInfo(z_opt, zs)
  436. # If one of the axes is very slow to change between (like SD model
  437. # checkpoint), then make sure it is in the outer iteration of the nested
  438. # `for` loop.
  439. first_axes_processed = 'z'
  440. second_axes_processed = 'y'
  441. if x_opt.cost > y_opt.cost and x_opt.cost > z_opt.cost:
  442. first_axes_processed = 'x'
  443. if y_opt.cost > z_opt.cost:
  444. second_axes_processed = 'y'
  445. else:
  446. second_axes_processed = 'z'
  447. elif y_opt.cost > x_opt.cost and y_opt.cost > z_opt.cost:
  448. first_axes_processed = 'y'
  449. if x_opt.cost > z_opt.cost:
  450. second_axes_processed = 'x'
  451. else:
  452. second_axes_processed = 'z'
  453. elif z_opt.cost > x_opt.cost and z_opt.cost > y_opt.cost:
  454. first_axes_processed = 'z'
  455. if x_opt.cost > y_opt.cost:
  456. second_axes_processed = 'x'
  457. else:
  458. second_axes_processed = 'y'
  459. grid_infotext = [None] * (1 + len(zs))
  460. def cell(x, y, z, ix, iy, iz):
  461. if shared.state.interrupted:
  462. return Processed(p, [], p.seed, "")
  463. pc = copy(p)
  464. pc.styles = pc.styles[:]
  465. x_opt.apply(pc, x, xs)
  466. y_opt.apply(pc, y, ys)
  467. z_opt.apply(pc, z, zs)
  468. res = process_images(pc)
  469. # Sets subgrid infotexts
  470. subgrid_index = 1 + iz
  471. if grid_infotext[subgrid_index] is None and ix == 0 and iy == 0:
  472. pc.extra_generation_params = copy(pc.extra_generation_params)
  473. pc.extra_generation_params['Script'] = self.title()
  474. if x_opt.label != 'Nothing':
  475. pc.extra_generation_params["X Type"] = x_opt.label
  476. pc.extra_generation_params["X Values"] = x_values
  477. if x_opt.label in ["Seed", "Var. seed"] and not no_fixed_seeds:
  478. pc.extra_generation_params["Fixed X Values"] = ", ".join([str(x) for x in xs])
  479. if y_opt.label != 'Nothing':
  480. pc.extra_generation_params["Y Type"] = y_opt.label
  481. pc.extra_generation_params["Y Values"] = y_values
  482. if y_opt.label in ["Seed", "Var. seed"] and not no_fixed_seeds:
  483. pc.extra_generation_params["Fixed Y Values"] = ", ".join([str(y) for y in ys])
  484. grid_infotext[subgrid_index] = processing.create_infotext(pc, pc.all_prompts, pc.all_seeds, pc.all_subseeds)
  485. # Sets main grid infotext
  486. if grid_infotext[0] is None and ix == 0 and iy == 0 and iz == 0:
  487. pc.extra_generation_params = copy(pc.extra_generation_params)
  488. if z_opt.label != 'Nothing':
  489. pc.extra_generation_params["Z Type"] = z_opt.label
  490. pc.extra_generation_params["Z Values"] = z_values
  491. if z_opt.label in ["Seed", "Var. seed"] and not no_fixed_seeds:
  492. pc.extra_generation_params["Fixed Z Values"] = ", ".join([str(z) for z in zs])
  493. grid_infotext[0] = processing.create_infotext(pc, pc.all_prompts, pc.all_seeds, pc.all_subseeds)
  494. return res
  495. with SharedSettingsStackHelper():
  496. processed = draw_xyz_grid(
  497. p,
  498. xs=xs,
  499. ys=ys,
  500. zs=zs,
  501. x_labels=[x_opt.format_value(p, x_opt, x) for x in xs],
  502. y_labels=[y_opt.format_value(p, y_opt, y) for y in ys],
  503. z_labels=[z_opt.format_value(p, z_opt, z) for z in zs],
  504. cell=cell,
  505. draw_legend=draw_legend,
  506. include_lone_images=include_lone_images,
  507. include_sub_grids=include_sub_grids,
  508. first_axes_processed=first_axes_processed,
  509. second_axes_processed=second_axes_processed,
  510. margin_size=margin_size
  511. )
  512. if not processed.images:
  513. # It broke, no further handling needed.
  514. return processed
  515. z_count = len(zs)
  516. # Set the grid infotexts to the real ones with extra_generation_params (1 main grid + z_count sub-grids)
  517. processed.infotexts[:1+z_count] = grid_infotext[:1+z_count]
  518. if not include_lone_images:
  519. # Don't need sub-images anymore, drop from list:
  520. processed.images = processed.images[:z_count+1]
  521. if opts.grid_save:
  522. # Auto-save main and sub-grids:
  523. grid_count = z_count + 1 if z_count > 1 else 1
  524. for g in range(grid_count):
  525. #TODO: See previous comment about intentional data misalignment.
  526. adj_g = g-1 if g > 0 else g
  527. images.save_image(processed.images[g], p.outpath_grids, "xyz_grid", info=processed.infotexts[g], extension=opts.grid_format, prompt=processed.all_prompts[adj_g], seed=processed.all_seeds[adj_g], grid=True, p=processed)
  528. if not include_sub_grids:
  529. # Done with sub-grids, drop all related information:
  530. for sg in range(z_count):
  531. del processed.images[1]
  532. del processed.all_prompts[1]
  533. del processed.all_seeds[1]
  534. del processed.infotexts[1]
  535. return processed