From 89c6c11214b2e801513881449a4ce597225992d7 Mon Sep 17 00:00:00 2001 From: plucked Date: Fri, 7 Oct 2022 08:32:39 +0000 Subject: [PATCH 1/4] feat: adding filename format template --- ldm/dream/args.py | 9 ++++++++- scripts/dream.py | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ldm/dream/args.py b/ldm/dream/args.py index 7a41094ded6..375509251ba 100644 --- a/ldm/dream/args.py +++ b/ldm/dream/args.py @@ -185,6 +185,7 @@ def dream_prompt_str(self,**kwargs): switches.append(f'-W {a["width"]}') switches.append(f'-H {a["height"]}') switches.append(f'-C {a["cfg_scale"]}') + switches.append(f'--fnformat {a["fnformat"]}') if a['perlin'] > 0: switches.append(f'--perlin {a["perlin"]}') if a['threshold'] > 0: @@ -559,6 +560,12 @@ def _create_dream_cmd_parser(self): type=float, help='Perlin noise scale (0.0 - 1.0) - add perlin noise to the initialization instead of the usual gaussian noise.', ) + render_group.add_argument( + '--fnformat', + default='%(prefix)s.%(seed)s.png', + type=str, + help='Specify the template of the generated files names.', + ) render_group.add_argument( '--grid', '-g', @@ -757,7 +764,7 @@ def metadata_dumps(opt, # remove any image keys not mentioned in RFC #266 rfc266_img_fields = ['type','postprocessing','sampler','prompt','seed','variations','steps', - 'cfg_scale','threshold','perlin','step_number','width','height','extra','strength'] + 'cfg_scale','threshold','perlin','fnformat','step_number','width','height','extra','strength'] rfc_dict ={} diff --git a/scripts/dream.py b/scripts/dream.py index cd0d7c33b4b..365667484b3 100644 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -458,7 +458,23 @@ def prepare_image_metadata( if postprocessed and opt.save_original: filename = choose_postprocess_name(opt,prefix,seed) else: - filename = f'{prefix}.{seed}.png' + filename = opt.fnformat % { + 'prefix': prefix, + 'seed': seed, + 'steps': opt.steps, + 'prompt': opt.prompt, + 'width': opt.width, + 'height': opt.height, + 'cfg_scale': opt.cfg_scale, + 'perlin': opt.perlin, + 'threshold': opt.threshold, + 'gfpgan_strength': opt.gfpgan_strength, + 'outcrop': opt.outcrop, + 'upscale': opt.upscale, + 'embiggen': opt.embiggen, + 'embiggen_tiles': opt.embiggen_tiles, + 'out_direction': opt.out_direction + } if opt.variation_amount > 0: first_seed = first_seed or seed From 395445e7b0a8398948912ae8f71b31121bd7a9e8 Mon Sep 17 00:00:00 2001 From: plucked Date: Fri, 7 Oct 2022 09:24:39 +0000 Subject: [PATCH 2/4] using string.format for filename formatting --- ldm/dream/args.py | 4 ++-- scripts/dream.py | 30 ++++++++++++------------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/ldm/dream/args.py b/ldm/dream/args.py index 375509251ba..0c24ef5732d 100644 --- a/ldm/dream/args.py +++ b/ldm/dream/args.py @@ -562,9 +562,9 @@ def _create_dream_cmd_parser(self): ) render_group.add_argument( '--fnformat', - default='%(prefix)s.%(seed)s.png', + default='{prefix}.{seed}.png', type=str, - help='Specify the template of the generated files names.', + help='Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix}.{seed}.png', ) render_group.add_argument( '--grid', diff --git a/scripts/dream.py b/scripts/dream.py index 365667484b3..92d3ba58456 100644 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -356,7 +356,7 @@ def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None): grid_img = make_grid(list(grid_images.values())) grid_seeds = list(grid_images.keys()) first_seed = last_results[0][1] - filename = f'{prefix}.{first_seed}.png' + filename = f'{prefix}.png' formatted_dream_prompt = opt.dream_prompt_str(seed=first_seed,grid=True,iterations=len(grid_images)) formatted_dream_prompt += f' # {grid_seeds}' metadata = metadata_dumps( @@ -458,23 +458,17 @@ def prepare_image_metadata( if postprocessed and opt.save_original: filename = choose_postprocess_name(opt,prefix,seed) else: - filename = opt.fnformat % { - 'prefix': prefix, - 'seed': seed, - 'steps': opt.steps, - 'prompt': opt.prompt, - 'width': opt.width, - 'height': opt.height, - 'cfg_scale': opt.cfg_scale, - 'perlin': opt.perlin, - 'threshold': opt.threshold, - 'gfpgan_strength': opt.gfpgan_strength, - 'outcrop': opt.outcrop, - 'upscale': opt.upscale, - 'embiggen': opt.embiggen, - 'embiggen_tiles': opt.embiggen_tiles, - 'out_direction': opt.out_direction - } + wildcards = dict(opt.__dict__) + wildcards['prefix'] = prefix + wildcards['seed'] = seed + try: + filename = opt.fnformat.format(**wildcards) + except KeyError as e: + print(f'The filename format contains an unknown key \'{e.args[0]}\'. Will use \'{{prefix}}.{{seed}}.png\' instead') + filename = f'{prefix}.{seed}.png' + except IndexError as e: + print(f'The filename format is broken or complete. Will use \'{{prefix}}.{{seed}}.png\' instead') + filename = f'{prefix}.{seed}.png' if opt.variation_amount > 0: first_seed = first_seed or seed From c6611b2ad65ade481501a4bc7a089e411b090c09 Mon Sep 17 00:00:00 2001 From: plucked Date: Fri, 7 Oct 2022 10:21:16 +0000 Subject: [PATCH 3/4] doc: described how filename format works --- docs/features/OTHER.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/features/OTHER.md b/docs/features/OTHER.md index 6aab5282756..0c478c0bfe5 100644 --- a/docs/features/OTHER.md +++ b/docs/features/OTHER.md @@ -71,6 +71,20 @@ combination of integers and floating point numbers, and they do not need to add --- +## Filename Format + +The argument `--fnformat` allows to specify the filename of the image. Supported wildcards are all arguments what can be set such as + `perlin`, `seed`, `threshold`, `height`, `width`, `gfpgan_strength`, `sampler_name`, `steps`, `model`, `upscale`, `prompt`, `cfg_scale`, `prefix`. + +The following prompt +```bash +dream> a red car --steps 25 -C 9.8 --perlin 0.1 --fnformat {prompt}_steps.{steps}_cfg.{cfg_scale}_perlin.{perlin}.png +``` + +generates a file with the name: `outputs/img-samples/a red car_steps.25_cfg.9.8_perlin.0.1.png` + +--- + ## Thresholding and Perlin Noise Initialization Options Two new options are the thresholding (`--threshold`) and the perlin noise initialization (`--perlin`) options. Thresholding limits the range of the latent values during optimization, which helps combat oversaturation with higher CFG scale values. Perlin noise initialization starts with a percentage (a value ranging from 0 to 1) of perlin noise mixed into the initial noise. Both features allow for more variations and options in the course of generating images. From 6499b99dad70e9ea58109b094347edc76b61f200 Mon Sep 17 00:00:00 2001 From: plucked Date: Fri, 7 Oct 2022 10:26:14 +0000 Subject: [PATCH 4/4] revert accidental edit --- scripts/dream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dream.py b/scripts/dream.py index 92d3ba58456..dddbbdc85ca 100644 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -356,7 +356,7 @@ def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None): grid_img = make_grid(list(grid_images.values())) grid_seeds = list(grid_images.keys()) first_seed = last_results[0][1] - filename = f'{prefix}.png' + filename = f'{prefix}.{first_seed}.png' formatted_dream_prompt = opt.dream_prompt_str(seed=first_seed,grid=True,iterations=len(grid_images)) formatted_dream_prompt += f' # {grid_seeds}' metadata = metadata_dumps(