DEFAULT_SCHEDULER = 'EulerAncestralDiscreteScheduler'
-
-
-def save_path(count: int) -> str:
- n_iterations = len(args.gen_paramses) * args.quantity
- filename_count = f'_{count:08}' if n_iterations > 1 else ''
- return path_join(dir_path, f'{filename_sans_ext}{filename_count}.{ext}')
+SEED_MIN = -(2**31-1)
+SEED_MAX = 2**31
def parse_args():
prefix += 'unless calling with -H/--help or --list-schedulers, '
prefix += 'requiring '
- # Re-seed defaults from stdin, if -D.
+ # Re-seed defaults from stdin, if -D.
temp_gen_paramses = [None]
if parsed_args.defaults_from_stdin:
temp_gen_paramses = []
temp_gen_paramses += [GenParams.from_str(line)]
parsed_args.models = []
parsed_args.gen_paramses = []
- for gen_params in temp_gen_paramses:
- if gen_params:
- parser.set_defaults(**gen_params.as_dict)
+ for gen_params_ in temp_gen_paramses:
+ if gen_params_:
+ parser.set_defaults(**gen_params_.as_dict)
new_parsed_args = parser.parse_args()
# Validate input.
return parsed_args
-# guard against overwriting
-args = parse_args()
-if not args.list_schedulers:
- dir_path = dirname(args.output)
- filename = basename(args.output)
- filename_sans_ext, ext = splitext(filename)
- ext = ext[1:] if ext else 'png'
- for n in range(args.quantity * len(args.gen_paramses)):
- path = save_path(n)
- if exists(path):
- raise Exception(f'Would overwrite file: {path}')
-
-# pylint: disable=wrong-import-position
-from stable.core import ImageMaker # noqa: E402
-old_model_path = ''
-maker = None
-for i, model_name in enumerate(args.models):
- new_model_path = path_join(args.models_dir, model_name)
- if new_model_path != old_model_path:
- maker = ImageMaker(new_model_path)
- old_model_path = new_model_path
-
- # only list available schedulers, if -L
- if args.list_schedulers:
- print(f'AVAILABLE SCHEDULERS FOR MODEL {new_model_path}:\n')
- for name in maker.compatible_schedulers:
- print(name)
- continue
-
- # otherwise generate pictures
- gen_params = args.gen_paramses[i]
- start_seed = gen_params.seed
- start_seed = start_seed if start_seed != 0 else randint(-(2**31-1), 2**31)
- seed_corrector = 0
- for n in range(args.quantity):
- if 0 == start_seed + n + seed_corrector:
- seed_corrector += 1
- gen_params.seed = start_seed + n + seed_corrector
- path = save_path(i*args.quantity + n)
- maker.set_gen_params(gen_params)
- print(f'GENERATING: {path}; {gen_params.to_str}')
- maker.gen_image_to(path)
+def run():
+
+ def save_path(count: int) -> str:
+ n_iterations = len(args.gen_paramses)
+ filename_count = f'_{count:08}' if n_iterations > 1 else ''
+ # pylint: disable=possibly-used-before-assignment
+ return path_join(dir_path,
+ f'{filename_sans_ext}{filename_count}.{ext}')
+
+ # guard against overwriting
+ args = parse_args()
+ if not args.list_schedulers:
+ dir_path = dirname(args.output)
+ filename = basename(args.output)
+ filename_sans_ext, ext = splitext(filename)
+ ext = ext[1:] if ext else 'png'
+ for n in range(args.quantity * len(args.gen_paramses)):
+ path = save_path(n)
+ if exists(path):
+ raise Exception(f'Would overwrite file: {path}')
+
+ # pylint: disable=wrong-import-position, import-outside-toplevel
+ from stable.core import ImageMaker # noqa: E402
+ old_model_path = ''
+ maker = None
+ for i, model_name in enumerate(args.models):
+ new_model_path = path_join(args.models_dir, model_name)
+ if new_model_path != old_model_path:
+ maker = ImageMaker(new_model_path)
+ old_model_path = new_model_path
+
+ # only list available schedulers, if -L
+ if args.list_schedulers:
+ print(f'AVAILABLE SCHEDULERS FOR MODEL {new_model_path}:\n')
+ for name in maker.compatible_schedulers:
+ print(name)
+ continue
+
+ # otherwise generate pictures
+ gen_params = args.gen_paramses[i]
+ start_seed = gen_params.seed
+ start_seed = start_seed if start_seed != 0 else randint(SEED_MIN,
+ SEED_MAX)
+ seed_corrector = 0
+ for n in range(args.quantity):
+ if 0 == start_seed + n + seed_corrector:
+ seed_corrector += 1
+ gen_params.seed = start_seed + n + seed_corrector
+ path = save_path(i*args.quantity + n)
+ maker.set_gen_params(gen_params)
+ print(f'GENERATING: {path}; {gen_params.to_str}')
+ maker.gen_image_to(path)
+
+
+run()
from logging import (Formatter as LogFormatter, captureWarnings,
Filter as LogFilter)
-from os.path import basename
from diffusers import StableDiffusionPipeline
from diffusers.utils import logging
from torch import Generator, float16
from exiftool import ExifToolHelper # type: ignore
-from stable.gen_params import GenParams
SAFETY_CHECKER_WARNING_PATTERN = 'You have disabled the safety checker'
scheduler_selection = [s for s in self.pipe.scheduler.compatibles
if s.__name__ == gen_params.scheduler_name]
if not scheduler_selection:
- raise Exception(f'unknown scheduler: {scheduler_name}')
+ raise Exception(f'unknown scheduler: {gen_params.scheduler_name}')
self.pipe.scheduler = scheduler_selection[0].from_config(
self.pipe.scheduler.config)
self.gen_params = gen_params
class GenParams:
- def __init__(self, seed, guidance, n_steps, height, width, scheduler, model, prompt):
+ def __init__(self, seed, guidance, n_steps, height, width, scheduler,
+ model, prompt):
if '; ' in model:
raise Exception('illegal model filename (must not contain "; ")')
self.seed = seed
self.height, self.width = height, width
self.scheduler_name = scheduler
self.model_name = model
- self.prompt = prompt
+ self.prompt = prompt
@property
def to_str(self):
d = {}
first_split = string.split('; PROMPT: ', maxsplit=1)
if 2 == len(first_split):
- d['prompt'] = first_split[1]
+ d['prompt'] = first_split[1]
for section in first_split[0].split('; '):
key, val = section.split(': ', maxsplit=1)
key = key.lower()