From ed943bd6c785d968d1e31fbbb814fea8688fc8bb Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Sat, 12 Nov 2022 16:05:45 +0000 Subject: [PATCH] outcrop improvements, hand-added --- docs/features/OUTPAINTING.md | 2 +- ldm/generate.py | 19 ++++++------------- ldm/invoke/generator/base.py | 2 +- ldm/invoke/generator/inpaint.py | 2 +- ldm/invoke/restoration/outcrop.py | 6 +++--- scripts/invoke.py | 5 ++++- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/docs/features/OUTPAINTING.md b/docs/features/OUTPAINTING.md index 823077edcb..b53ff545f3 100644 --- a/docs/features/OUTPAINTING.md +++ b/docs/features/OUTPAINTING.md @@ -105,7 +105,7 @@ invoke> !fix images/vacation.png --outcrop top 128 --new_prompt "family vacation ``` You may also provide a different seed for outcropping to use by passing -`-S`. A seed of "0" will generate a new random seed. +`-S`. A negative seed will generate a new random seed. A number of caveats: diff --git a/ldm/generate.py b/ldm/generate.py index ca9fd7691c..9aebe314f3 100644 --- a/ldm/generate.py +++ b/ldm/generate.py @@ -563,19 +563,12 @@ class Generate: seed = None prompt = None - args = metadata_from_png(image_path) - if opt.seed is not None: - seed = opt.seed - elif args.seed >= 0: - seed = args.seed - else: + args = metadata_from_png(image_path) + seed = opt.seed or args.seed + if seed is None or seed < 0: seed = random.randrange(0, np.iinfo(np.uint32).max) - - if opt.prompt is not None: - prompt = opt.prompt - else: - prompt = args.prompt - + + prompt = opt.prompt or args.prompt or '' print(f'>> using seed {seed} and prompt "{prompt}" for {image_path}') # try to reuse the same filename prefix as the original file. @@ -627,7 +620,7 @@ class Generate: opt.seed = seed opt.prompt = prompt - if len(extend_instructions)>0: + if len(extend_instructions) > 0: restorer = Outcrop(image,self,) return restorer.process ( extend_instructions, diff --git a/ldm/invoke/generator/base.py b/ldm/invoke/generator/base.py index 719d08c7c0..003ac1b466 100644 --- a/ldm/invoke/generator/base.py +++ b/ldm/invoke/generator/base.py @@ -63,7 +63,7 @@ class Generator(): **kwargs ) results = [] - seed = seed if seed is not None and seed > 0 else self.new_seed() + seed = seed if seed is not None and seed >= 0 else self.new_seed() first_seed = seed seed, initial_noise = self.generate_initial_noise(seed, width, height) diff --git a/ldm/invoke/generator/inpaint.py b/ldm/invoke/generator/inpaint.py index d8a471350f..792a01b3b1 100644 --- a/ldm/invoke/generator/inpaint.py +++ b/ldm/invoke/generator/inpaint.py @@ -169,7 +169,7 @@ class Inpaint(Img2Img): # Fill missing areas of original image init_filled = self.tile_fill_missing( self.pil_image.copy(), - seed = self.seed, + seed = self.seed if self.seed >= 0 else self.new_seed(), tile_size = tile_size ) init_filled.paste(init_image, (0,0), init_image.split()[-1]) diff --git a/ldm/invoke/restoration/outcrop.py b/ldm/invoke/restoration/outcrop.py index 1a0aaf2c8f..2587bb5776 100644 --- a/ldm/invoke/restoration/outcrop.py +++ b/ldm/invoke/restoration/outcrop.py @@ -28,12 +28,12 @@ class Outcrop(object): self.generate._set_sampler() def wrapped_callback(img,seed,**kwargs): - preferred_seed = orig_opt.seed if orig_opt.seed> 0 else seed + preferred_seed = orig_opt.seed if orig_opt.seed >= 0 else seed image_callback(img,preferred_seed,use_prefix=prefix,**kwargs) result= self.generate.prompt2image( - orig_opt.prompt, - seed = orig_opt.seed if orig_opt.seed>0 else opt.seed, + opt.prompt, + seed = opt.seed or orig_opt.seed, sampler = self.generate.sampler, steps = opt.steps, cfg_scale = opt.cfg_scale, diff --git a/scripts/invoke.py b/scripts/invoke.py index 1c1d44dd16..d7ea508fd8 100755 --- a/scripts/invoke.py +++ b/scripts/invoke.py @@ -213,7 +213,10 @@ def main_loop(gen, opt): setattr(opt,attr,path) # retrieve previous value of seed if requested - if opt.seed is not None and opt.seed < 0: + # Exception: for postprocess operations negative seed values + # mean "discard the original seed and generate a new one" + # (this is a non-obvious hack and needs to be reworked) + if opt.seed is not None and opt.seed < 0 and operation != 'postprocess': try: opt.seed = last_results[opt.seed][1] print(f'>> Reusing previous seed {opt.seed}')