From 0f2866380517e67256b13b7d85c2d252c94f0b9a Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 12:43:13 +0200 Subject: [PATCH 1/7] remove redundant None check (`if var` does the same thing) --- scripts/dream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dream.py b/scripts/dream.py index f6feb10adc..8175221cb3 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -66,7 +66,7 @@ def main(): infile = None try: - if opt.infile is not None: + if opt.infile: infile = open(opt.infile, 'r') except FileNotFoundError as e: print(e) From cf750f62dbcb21ce5350caf032d63d7ab1364bb4 Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 13:10:37 +0200 Subject: [PATCH 2/7] refactored infile handling --- scripts/dream.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/scripts/dream.py b/scripts/dream.py index 8175221cb3..ef7f6f3c56 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -64,13 +64,16 @@ def main(): # gets rid of annoying messages about random seed logging.getLogger('pytorch_lightning').setLevel(logging.ERROR) + # load the infile as a list of lines infile = None - try: - if opt.infile: - infile = open(opt.infile, 'r') - except FileNotFoundError as e: - print(e) - exit(-1) + if opt.infile: + if os.path.isfile(opt.infile): + with open(opt.infile, "r") as file: + infile = file.read() + infile = infile.split("\n") + else: + print(f"WARNING: '{opt.infile}' not found. Aborting.") + sys.exit(-1) # exit does not work on every os, sys.exit does afaik # preload the model t2i.load_model() @@ -119,8 +122,6 @@ def main(): cmd_parser = create_cmd_parser() main_loop(t2i, opt.outdir, cmd_parser, log, infile) log.close() - if infile: - infile.close() def main_loop(t2i, outdir, parser, log, infile): @@ -129,15 +130,19 @@ def main_loop(t2i, outdir, parser, log, infile): last_seeds = [] while not done: - try: - command = infile.readline() if infile else input('dream> ') - except EOFError: - done = True - break + if not infile: + command = input("dream> ") + else: + try: + # get the next line of the infile + command = infile.pop(0) + except IndexError: + done = True + break - if infile and len(command) == 0: - done = True - break + # skip empty lines + if not command.strip(): + continue if command.startswith(('#', '//')): continue @@ -152,9 +157,6 @@ def main_loop(t2i, outdir, parser, log, infile): print(str(e)) continue - if len(elements) == 0: - continue - if elements[0] == 'q': done = True break From 12f59e1daab94bed9d204a0200dc0a5ee79126fd Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 13:12:56 +0200 Subject: [PATCH 3/7] removed log.close(); 'with open' automatically closes the file --- scripts/dream.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/dream.py b/scripts/dream.py index ef7f6f3c56..88848da210 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -121,7 +121,6 @@ def main(): with open(log_path, 'a') as log: cmd_parser = create_cmd_parser() main_loop(t2i, opt.outdir, cmd_parser, log, infile) - log.close() def main_loop(t2i, outdir, parser, log, infile): From e00397f9cafbc84302596e280d955c4a83bfdd21 Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 13:22:53 +0200 Subject: [PATCH 4/7] refactored logfile handling; minimised time spent in context managers (with open) --- scripts/dream.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/scripts/dream.py b/scripts/dream.py index 88848da210..d937a4ac95 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -118,12 +118,11 @@ def main(): ) log_path = os.path.join(opt.outdir, 'dream_log.txt') - with open(log_path, 'a') as log: - cmd_parser = create_cmd_parser() - main_loop(t2i, opt.outdir, cmd_parser, log, infile) + cmd_parser = create_cmd_parser() + main_loop(t2i, opt.outdir, cmd_parser, log_path, infile) -def main_loop(t2i, outdir, parser, log, infile): +def main_loop(t2i, outdir, parser, log_path, infile): """prompt/read/execute loop""" done = False last_seeds = [] @@ -240,7 +239,7 @@ def main_loop(t2i, outdir, parser, log, infile): continue print('Outputs:') - write_log_message(t2i, normalized_prompt, results, log) + write_log_message(t2i, normalized_prompt, results, log_path) print('goodbye!') @@ -310,19 +309,14 @@ def load_gfpgan_bg_upsampler(bg_upsampler, bg_tile=400): # return variants -def write_log_message(t2i, prompt, results, logfile): +### the results variable doesn't seem to be necessary. maybe remove it? +def write_log_message(t2i, prompt, results, log_path): """logs the name of the output image, its prompt and seed to the terminal, log file, and a Dream text chunk in the PNG metadata""" - last_seed = None - img_num = 1 - seenit = {} + log_lines = [f"{r[0]}: {prompt} -S{seed}\n" for r in results] + print(*log_lines, sep="") - for r in results: - seed = r[1] - log_message = f'{r[0]}: {prompt} -S{seed}' - - print(log_message) - logfile.write(log_message + '\n') - logfile.flush() + with open(log_path, "a") as file: + file.writelines(log_lines) def create_argv_parser(): From 89805a5239bf46dec99e97f0adcfe41baa0b6402 Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 13:25:12 +0200 Subject: [PATCH 5/7] fixed mistake in comment --- scripts/dream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dream.py b/scripts/dream.py index d937a4ac95..89e6977d8c 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -309,7 +309,7 @@ def load_gfpgan_bg_upsampler(bg_upsampler, bg_tile=400): # return variants -### the results variable doesn't seem to be necessary. maybe remove it? +### the t2i variable doesn't seem to be necessary here. maybe remove it? def write_log_message(t2i, prompt, results, log_path): """logs the name of the output image, its prompt and seed to the terminal, log file, and a Dream text chunk in the PNG metadata""" log_lines = [f"{r[0]}: {prompt} -S{seed}\n" for r in results] From 5129f256a3ef4c702ba96ffebba55368abaea63e Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 14:13:16 +0200 Subject: [PATCH 6/7] simplet2i: changed image file handling to work as stated in the [docs](https://pillow.readthedocs.io/en/stable/reference/open_files.html) --- ldm/simplet2i.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ldm/simplet2i.py b/ldm/simplet2i.py index 8aad3557af..e592e34f83 100644 --- a/ldm/simplet2i.py +++ b/ldm/simplet2i.py @@ -545,7 +545,9 @@ class T2I: return model def _load_img(self, path): - image = Image.open(path).convert('RGB') + with Image.open(path) as img: + image = img.convert("RGB") + w, h = image.size print(f'loaded input image of size ({w}, {h}) from {path}') w, h = map( From 7040995cebcad8c2bbbac1aed8d7224875e9afe8 Mon Sep 17 00:00:00 2001 From: yun saki Date: Fri, 26 Aug 2022 14:25:49 +0200 Subject: [PATCH 7/7] fixed variable name error --- scripts/dream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dream.py b/scripts/dream.py index 89e6977d8c..32e31dd7a2 100755 --- a/scripts/dream.py +++ b/scripts/dream.py @@ -312,7 +312,7 @@ def load_gfpgan_bg_upsampler(bg_upsampler, bg_tile=400): ### the t2i variable doesn't seem to be necessary here. maybe remove it? def write_log_message(t2i, prompt, results, log_path): """logs the name of the output image, its prompt and seed to the terminal, log file, and a Dream text chunk in the PNG metadata""" - log_lines = [f"{r[0]}: {prompt} -S{seed}\n" for r in results] + log_lines = [f"{r[0]}: {prompt} -S{r[1]}\n" for r in results] print(*log_lines, sep="") with open(log_path, "a") as file: