Skip to content

Commit e98fe9c

Browse files
committed
fix noisy images at high step counts
At step counts greater than ~75, the ksamplers start producing noisy images when using the Karras noise schedule. This PR reverts to using the model's own noise schedule, which eliminates the problem at the cost of slowing convergence at lower step counts. This PR also introduces a new CLI `--save_intermediates <n>' argument, which will save every nth intermediate image into a subdirectory named `intermediates/<image_prefix>'. Addresses issue #1083.
1 parent 7f491fd commit e98fe9c

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

ldm/invoke/args.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ def _create_dream_cmd_parser(self):
636636
dest='hires_fix',
637637
help='Create hires image using img2img to prevent duplicated objects'
638638
)
639+
render_group.add_argument(
640+
'--save_intermediates',
641+
type=int,
642+
default=0,
643+
dest='save_intermediates',
644+
help='Save every nth intermediate image into an "intermediates" directory within the output directory'
645+
)
639646
img2img_group.add_argument(
640647
'-I',
641648
'--init_img',

ldm/invoke/readline.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'--perlin',
3232
'--grid','-g',
3333
'--individual','-i',
34+
'--save_intermediates',
3435
'--init_img','-I',
3536
'--init_mask','-M',
3637
'--init_color',

ldm/models/diffusion/ksampler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def make_schedule(
9898
rho=7.,
9999
device=self.device,
100100
)
101-
self.sigmas = self.karras_sigmas
101+
self.sigmas = self.model_sigmas
102+
#self.sigmas = self.karras_sigmas
102103

103104
# ALERT: We are completely overriding the sample() method in the base class, which
104105
# means that inpainting will not work. To get this to work we need to be able to

ldm/models/diffusion/sampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def sample(
140140
conditioning=None,
141141
callback=None,
142142
normals_sequence=None,
143-
img_callback=None,
143+
img_callback=None, # TODO: this is very confusing because it is called "step_callback" elsewhere. Change.
144144
quantize_x0=False,
145145
eta=0.0,
146146
mask=None,

scripts/invoke.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ def main_loop(gen, opt, infile):
289289
grid_images = dict() # seed -> Image, only used if `opt.grid`
290290
prior_variations = opt.with_variations or []
291291
prefix = file_writer.unique_prefix()
292+
step_callback = make_step_callback(gen, opt, prefix) if opt.save_intermediates > 0 else None
292293

293294
def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None):
294295
# note the seed is the seed of the current image
@@ -350,6 +351,7 @@ def image_writer(image, seed, upscaled=False, first_seed=None, use_prefix=None):
350351
opt.last_operation='generate'
351352
gen.prompt2image(
352353
image_callback=image_writer,
354+
step_callback=step_callback,
353355
catch_interrupts=catch_ctrl_c,
354356
**vars(opt)
355357
)
@@ -547,6 +549,17 @@ def split_variations(variations_string) -> list:
547549
else:
548550
return parts
549551

552+
def make_step_callback(gen, opt, prefix):
553+
destination = os.path.join(opt.outdir,'intermediates',prefix)
554+
os.makedirs(destination,exist_ok=True)
555+
print(f'>> Intermediate images will be written into {destination}')
556+
def callback(img, step):
557+
if step % opt.save_intermediates == 0 or step == opt.steps-1:
558+
filename = os.path.join(destination,f'{step:04}.png')
559+
image = gen.sample_to_image(img)
560+
image.save(filename,'PNG')
561+
return callback
562+
550563
def retrieve_dream_command(opt,file_path,completer):
551564
'''
552565
Given a full or partial path to a previously-generated image file,

0 commit comments

Comments
 (0)