stable-diffusion-webui-rand.../scripts/randomize.py

148 lines
6.6 KiB
Python
Raw Normal View History

2022-11-10 00:29:36 +00:00
import random
import gradio as gr
2022-11-10 00:29:36 +00:00
2022-11-17 23:15:43 +00:00
from modules import scripts, sd_models, shared
2022-11-10 00:29:36 +00:00
from modules.processing import (StableDiffusionProcessing,
StableDiffusionProcessingTxt2Img)
2022-11-10 01:56:14 +00:00
from modules.shared import opts
2022-11-10 00:29:36 +00:00
from scripts.xy_grid import build_samplers_dict
class RandomizeScript(scripts.Script):
def title(self):
return 'Randomize'
def show(self, is_img2img):
if not is_img2img:
return scripts.AlwaysVisible
2022-11-10 00:29:36 +00:00
def ui(self, is_img2img):
2022-11-17 23:15:43 +00:00
randomize_enabled, randomize_param_sampler_index, randomize_param_cfg_scale, randomize_param_steps, randomize_param_width, randomize_param_height, randomize_hires, randomize_hires_denoising_strength, randomize_hires_width, randomize_hires_height, randomize_other_CLIP_stop_at_last_layers, randomize_other_sd_model_checkpoint = self._create_ui()
2022-11-17 23:15:43 +00:00
return [randomize_enabled, randomize_param_sampler_index, randomize_param_cfg_scale, randomize_param_steps, randomize_param_width, randomize_param_height, randomize_hires, randomize_hires_denoising_strength, randomize_hires_width, randomize_hires_height, randomize_other_CLIP_stop_at_last_layers, randomize_other_sd_model_checkpoint]
def process_batch(
self,
p: StableDiffusionProcessing,
randomize_enabled: bool,
# randomize_param_seed: str,
randomize_param_sampler_index: str,
randomize_param_cfg_scale: str,
randomize_param_steps: str,
randomize_param_width: str,
randomize_param_height: str,
randomize_hires: str,
randomize_hires_denoising_strength: str,
randomize_hires_width: str,
randomize_hires_height: str,
randomize_other_CLIP_stop_at_last_layers: str,
2022-11-17 23:15:43 +00:00
randomize_other_sd_model_checkpoint: str,
**kwargs
):
if randomize_enabled and isinstance(p, StableDiffusionProcessingTxt2Img):
# TODO (mmaker): Fix this jank. Don't do this.
all_opts = {k: v for k, v in locals().items() if k not in ['self', 'p', 'randomize_enabled', 'batch_number', 'prompts', 'seeds', 'subseeds']}
2022-11-10 01:56:14 +00:00
# Base params
for param, val in self._list_params(all_opts):
2022-11-10 01:56:14 +00:00
try:
opt = self._opt({param: val}, p)
2022-11-10 01:56:14 +00:00
if opt is not None:
if param in ['seed']:
setattr(p, 'all_seeds', [self._opt({param: val}, p) for _ in range(0, len(getattr(p, 'all_seeds')))]) # NOTE (mmaker): Is this correct?
else:
setattr(p, param, opt)
2022-11-11 15:49:08 +00:00
else:
print(f'Skipping randomizing param `{param}` -- incorrect value')
except (TypeError, IndexError) as exception:
print(f'Failed to randomize param `{param}` -- incorrect value?', exception)
2022-11-10 01:56:14 +00:00
# Other params
for param, val in self._list_params(all_opts, prefix='randomize_other_'):
2022-11-10 01:56:14 +00:00
if param == 'CLIP_stop_at_last_layers':
opts.data[param] = int(self._opt({param: val}, p)) # type: ignore
2022-11-17 23:15:43 +00:00
if param == 'sd_model_checkpoint':
sd_model_checkpoint = self._opt({param: val}, p)
if sd_model_checkpoint:
sd_models.reload_model_weights(shared.sd_model, sd_model_checkpoint)
p.sd_model = shared.sd_model
2022-11-10 01:56:14 +00:00
2022-11-10 01:59:02 +00:00
# Highres. fix params
if random.random() < float(randomize_hires or 0):
2022-11-10 00:29:36 +00:00
try:
2022-11-12 16:46:08 +00:00
setattr(p, 'enable_hr', True)
2022-11-10 00:29:36 +00:00
setattr(p, 'firstphase_width', 0)
setattr(p, 'firstphase_height', 0)
setattr(p, 'denoising_strength', self._opt({'denoising_strength': randomize_hires_denoising_strength}, p))
2022-11-12 16:46:08 +00:00
setattr(p, 'width', self._opt({'width': randomize_hires_width}, p))
setattr(p, 'height', self._opt({'height': randomize_hires_height}, p))
# Set up highres. fix related stuff by re-running init function
p.init(p.all_prompts, p.all_seeds, p.all_subseeds)
except (TypeError, IndexError) as exception:
print(f'Failed to utilize highres. fix -- incorrect value?', exception)
2022-11-10 00:29:36 +00:00
else:
return
2022-11-10 01:56:14 +00:00
def _list_params(self, opts, prefix='randomize_param_'):
for k, v in opts.items():
if k.startswith(prefix) and v is not None and len(v) > 0:
yield k.replace(prefix,''), v
def _opt(self, opt, p):
opt_name = list(opt.keys())[0]
opt_val = list(opt.values())[0]
2022-11-10 01:56:14 +00:00
2022-11-11 15:29:37 +00:00
opt_arr: list[str] = [x.strip() for x in opt_val.split(',')]
if self._is_num(opt_arr[0]) and len(opt_arr) == 3 and opt_name not in ['seed']:
2022-11-10 00:41:55 +00:00
vals = [float(v) for v in opt_arr]
2022-11-10 00:29:36 +00:00
rand = self._rand(vals[0], vals[1], vals[2])
if rand.is_integer():
return int(rand)
else:
2022-11-10 01:14:11 +00:00
return round(float(rand), max(0, int(opt_arr[2][::-1].find('.'))))
2022-11-10 00:29:36 +00:00
else:
if opt_name == 'sampler_index':
2022-11-10 00:41:55 +00:00
return build_samplers_dict(p).get(random.choice(opt_arr).lower(), None)
elif opt_name == 'seed':
return int(random.choice(opt_arr))
2022-11-17 23:15:43 +00:00
elif opt_name == 'sd_model_checkpoint':
return sd_models.get_closet_checkpoint_match(random.choice(opt_arr))
2022-11-10 00:29:36 +00:00
else:
2022-11-11 15:49:08 +00:00
return None
2022-11-10 00:29:36 +00:00
def _rand(self, start: float, stop: float, step: float) -> float:
return random.randint(0, int((stop - start) / step)) * step + start
def _is_num(self, val: str):
if val.isdigit():
return True
else:
try:
float(val)
return True
except ValueError:
return False
2022-11-10 00:29:36 +00:00
def _create_ui(self):
2022-11-12 14:58:29 +00:00
hint_minmax = 'Range of stepped values (min, max, step)'
hint_list = 'Comma separated list'
with gr.Group():
with gr.Accordion('Randomize', open=False):
randomize_enabled = gr.Checkbox(label='Enable', value=False)
# randomize_param_seed = gr.Textbox(label='Seed', value='', placeholder=hint_list)
2022-11-12 14:58:29 +00:00
randomize_param_sampler_index = gr.Textbox(label='Sampler', value='', placeholder=hint_list)
randomize_param_cfg_scale = gr.Textbox(label='CFG Scale', value='', placeholder=hint_minmax)
randomize_param_steps = gr.Textbox(label='Steps', value='', placeholder=hint_minmax)
randomize_param_width = gr.Textbox(label='Width', value='', placeholder=hint_minmax)
randomize_param_height = gr.Textbox(label='Height', value='', placeholder=hint_minmax)
randomize_hires = gr.Textbox(label='Highres. percentage chance', value='0', placeholder='Float value from 0 to 1')
randomize_hires_denoising_strength = gr.Textbox(label='Highres. Denoising Strength', value='', placeholder=hint_minmax)
randomize_hires_width = gr.Textbox(label='Highres. Width', value='', placeholder=hint_minmax)
randomize_hires_height = gr.Textbox(label='Highres. Height', value='', placeholder=hint_minmax)
randomize_other_CLIP_stop_at_last_layers = gr.Textbox(label='Stop at CLIP layers', value='', placeholder=hint_minmax)
2022-11-17 23:15:43 +00:00
randomize_other_sd_model_checkpoint = gr.Textbox(label='Checkpoint name', value='', placeholder=hint_list)
2022-11-17 23:15:43 +00:00
return randomize_enabled, randomize_param_sampler_index, randomize_param_cfg_scale, randomize_param_steps, randomize_param_width, randomize_param_height, randomize_hires, randomize_hires_denoising_strength, randomize_hires_width, randomize_hires_height, randomize_other_CLIP_stop_at_last_layers, randomize_other_sd_model_checkpoint