1
Fork 0

Initial commit

master
MMaker 2022-11-09 19:29:36 -05:00
commit 1b9787a57d
Signed by: mmaker
GPG Key ID: CCE79B8FEDA40FB2
3 changed files with 107 additions and 0 deletions

21
LICENSE 100644
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 MMaker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

9
README.md 100644
View File

@ -0,0 +1,9 @@
# Stable Diffusion Randomize extension
An extension for [AUTOMATIC1111/stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui) that allows for random parameters during txt2img generation.
Syntax for randomization for parameters is `min,max,step`, with the exception of the randomize highres. param (a float value between `0` and `1`), and the sampler list (comma separated list of strings).
If enabled, this script is processed for *all* generations, regardless of the script selected, meaning this script will function with others as well, such as [AUTOMATIC1111/stable-diffusion-webui-wildcards](https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards).
Please note this is only for txt2img, I do not intend to add the same functionality for img2img.

View File

@ -0,0 +1,77 @@
import random
from modules import script_callbacks, scripts, shared
from modules.processing import (StableDiffusionProcessing,
StableDiffusionProcessingTxt2Img)
from scripts.xy_grid import build_samplers_dict
class RandomizeScript(scripts.Script):
def title(self):
return 'Randomize'
def show(self, is_img2img):
return scripts.AlwaysVisible
def process(self, p: StableDiffusionProcessing):
if shared.opts.randomize_enabled and isinstance(p, StableDiffusionProcessingTxt2Img):
all_opts = list(vars(shared.opts)['data'].keys())
for param in [o for o in filter(lambda x: x.startswith('randomize_param_'), all_opts)]:
if len(getattr(shared.opts, param).strip()) > 0:
param_name = param.split('randomize_param_')[1]
try:
opt = self._opt(param_name, p)
if opt is not None:
setattr(p, param_name, opt)
except TypeError:
print(f'Failed to randomize param `{param_name}` -- incorrect value?')
if random.random() < float(shared.opts.randomize_hires or None): # type: ignore
try:
setattr(p, 'width', self._opt('width', p, 'randomize_hires_'))
setattr(p, 'height', self._opt('height', p, 'randomize_hires_'))
setattr(p, 'enable_hr', True)
setattr(p, 'firstphase_width', 0)
setattr(p, 'firstphase_height', 0)
setattr(p, 'truncate_x', 0)
setattr(p, 'truncate_y', 0)
setattr(p, 'denoising_strength', float(self._opt('denoising_strength', p, 'randomize_hires_'))) # type: ignore
except TypeError:
print(f'Failed to utilize highres. fix -- incorrect value?')
else:
return
def _opt(self, opt, p, prefix='randomize_param_'):
opt_name = f'{prefix}{opt}'
option: str = getattr(shared.opts, opt_name)
split: list[str] = option.split(',')
if split[0].isdigit():
vals = [float(v) for v in split]
rand = self._rand(vals[0], vals[1], vals[2])
if rand.is_integer():
return int(rand)
else:
return float(rand)
else:
if opt == 'sampler_index':
return build_samplers_dict(p).get(random.choice(split).lower(), None)
else:
return random.choice(split)
def _rand(self, start: float, stop: float, step: float) -> float:
return random.randint(0, int((stop - start) / step)) * step + start
def on_ui_settings():
shared.opts.add_option('randomize_enabled', shared.OptionInfo(False, 'Enable Randomize extension', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_param_sampler_index', shared.OptionInfo('euler a,euler', 'Randomize Sampler', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_param_cfg_scale', shared.OptionInfo('5,15,0.5', 'Randomize CFG Scale', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_param_steps', shared.OptionInfo('10,50,2', 'Randomize Steps', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_param_width', shared.OptionInfo('256,768,64', 'Randomize Width', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_param_height', shared.OptionInfo('256,768,64', 'Randomize Height', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_hires', shared.OptionInfo('0.25', 'Randomize Highres. percentage', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_hires_denoising_strength', shared.OptionInfo('0.5,0.8,0.05', 'Randomize Highres. Denoising Strength', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_hires_width', shared.OptionInfo('768,1920,64', 'Randomize Highres. Width', section=('randomize', 'Randomize')))
shared.opts.add_option('randomize_hires_height', shared.OptionInfo('768,1920,64', 'Randomize Highres. Height', section=('randomize', 'Randomize')))
script_callbacks.on_ui_settings(on_ui_settings)