Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added abilty to log prompts and info to one file instead of several #476

Merged
merged 8 commits into from
Sep 3, 2022
70 changes: 52 additions & 18 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def check_prompt_length(prompt, comments):
comments.append(f"Warning: too many input tokens; some ({len(overflowing_words)}) have been truncated:\n{overflowing_text}\n")

def save_sample(image, sample_path_i, filename, jpg_sample, prompts, seeds, width, height, steps, cfg_scale,
normalize_prompt_weights, use_GFPGAN, write_info_files, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
normalize_prompt_weights, use_GFPGAN, write_info_files, write_info_to_one_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, skip_metadata):
filename_i = os.path.join(sample_path_i, filename)
if not jpg_sample:
Expand All @@ -586,7 +586,7 @@ def save_sample(image, sample_path_i, filename, jpg_sample, prompts, seeds, widt
image.save(f"{filename_i}.png")
else:
image.save(f"{filename_i}.jpg", 'jpeg', quality=100, optimize=True)
if write_info_files:
if write_info_files or write_info_to_one_file:
# toggles differ for txt2img vs. img2img:
offset = 0 if init_img is None else 2
toggles = []
Expand All @@ -607,8 +607,11 @@ def save_sample(image, sample_path_i, filename, jpg_sample, prompts, seeds, widt
toggles.append(4 + offset)
if write_info_files:
toggles.append(5 + offset)
if write_info_to_one_file:
toggles.append(6+offset)
if use_GFPGAN:
toggles.append(6 + offset)
toggles.append(7 + offset)

info_dict = dict(
target="txt2img" if init_img is None else "img2img",
prompt=prompts[i], ddim_steps=steps, toggles=toggles, sampler_name=sampler_name,
Expand All @@ -621,8 +624,31 @@ def save_sample(image, sample_path_i, filename, jpg_sample, prompts, seeds, widt
#info_dict["init_mask"] = init_mask
info_dict["denoising_strength"] = denoising_strength
info_dict["resize_mode"] = resize_mode
with open(f"{filename_i}.yaml", "w", encoding="utf8") as f:
yaml.dump(info_dict, f, allow_unicode=True, width=10000)
if write_info_files:
with open(f"{filename_i}.yaml", "w", encoding="utf8") as f:
yaml.dump(info_dict, f, allow_unicode=True, width=10000)

if write_info_to_one_file:
ignore_list = ["prompt", "target", "toggles", "ddim_eta", "batch_size"]
rename_dict = {"ddim_steps": "steps", "n_iter": "number", "sampler_name": "sampler"} #changes the name of parameters to match with dynamic parameters
sample_log_path = os.path.join(sample_path_i, "log.yaml")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use the ".yaml" file ending for log files, that will confuse users trying to load settings from them using #436 .
Use a file name that ends with ".txt" or ".log" instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will change

log_dump = info_dict.get("prompt") # making sure the first item that is listed in the txt is the prompt text
for key, value in info_dict.items():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterating over a dictionary is not guaranteed to yield items in a particular order.
With this code the order of the printed parameters can change randomly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean for it to be in any particular order do you think that would be better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if that will be better.
I was merely telling you because I don't know how experienced you are.

if key in ignore_list:
continue
found_key = rename_dict.get(key)

if key == "cfg_scale": #adds zeros to to cfg_scale necessary for dynamic params
value = str(value).zfill(2)

if found_key:
key = found_key
log_dump += f" {key} {value}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be difficult to read.
I recommend using different separators between key and value and two pairs of key and value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the point is so you can copy them using #421 if I add separators it will break that


log_dump = log_dump + " \n" #space at the end for dynamic params to accept the last param
with open(sample_log_path, "a", encoding="utf8") as log_file:
log_file.write(log_dump)



def get_next_sequence_number(path, prefix=''):
Expand Down Expand Up @@ -727,7 +753,7 @@ def process_images(
n_iter, steps, cfg_scale, width, height, prompt_matrix, use_GFPGAN, use_RealESRGAN, realesrgan_model_name,
fp, ddim_eta=0.0, do_not_save_grid=False, normalize_prompt_weights=True, init_img=None, init_mask=None,
keep_mask=False, mask_blur_strength=3, denoising_strength=0.75, resize_mode=None, uses_loopback=False,
uses_random_seed_loopback=False, sort_samples=True, write_info_files=True, jpg_sample=False,
uses_random_seed_loopback=False, sort_samples=True, write_info_files=True, write_info_to_one_file=False, jpg_sample=False,
variant_amount=0.0, variant_seed=None,imgProcessorTask=True):
"""this is the main loop that both txt2img and img2img use; it calls func_init once inside all the scopes and func_sample once per batch"""
assert prompt is not None
Expand Down Expand Up @@ -894,7 +920,7 @@ def process_images(
gfpgan_image = Image.fromarray(gfpgan_sample)
gfpgan_filename = original_filename + '-gfpgan'
save_sample(gfpgan_image, sample_path_i, gfpgan_filename, jpg_sample, prompts, seeds, width, height, steps, cfg_scale,
normalize_prompt_weights, use_GFPGAN, write_info_files, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
normalize_prompt_weights, use_GFPGAN, write_info_files, write_info_to_one_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, False)
#output_images.append(gfpgan_image) #287
#if simple_templating:
Expand All @@ -910,7 +936,7 @@ def process_images(
esrgan_sample = output[:,:,::-1]
esrgan_image = Image.fromarray(esrgan_sample)
save_sample(esrgan_image, sample_path_i, esrgan_filename, jpg_sample, prompts, seeds, width, height, steps, cfg_scale,
normalize_prompt_weights, use_GFPGAN, write_info_files, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
normalize_prompt_weights, use_GFPGAN,write_info_files, write_info_to_one_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, False)
#output_images.append(esrgan_image) #287
#if simple_templating:
Expand All @@ -928,7 +954,7 @@ def process_images(
gfpgan_esrgan_sample = output[:,:,::-1]
gfpgan_esrgan_image = Image.fromarray(gfpgan_esrgan_sample)
save_sample(gfpgan_esrgan_image, sample_path_i, gfpgan_esrgan_filename, jpg_sample, prompts, seeds, width, height, steps, cfg_scale,
normalize_prompt_weights, use_GFPGAN, write_info_files, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
normalize_prompt_weights, use_GFPGAN, write_info_files, write_info_to_one_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, False)
#output_images.append(gfpgan_esrgan_image) #287
#if simple_templating:
Expand All @@ -939,7 +965,7 @@ def process_images(

if not skip_save:
save_sample(image, sample_path_i, filename, jpg_sample, prompts, seeds, width, height, steps, cfg_scale,
normalize_prompt_weights, use_GFPGAN, write_info_files, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
normalize_prompt_weights, use_GFPGAN, write_info_files, write_info_to_one_file, prompt_matrix, init_img, uses_loopback, uses_random_seed_loopback, skip_save,
skip_grid, sort_samples, sampler_name, ddim_eta, n_iter, batch_size, i, denoising_strength, resize_mode, False)
if add_original_image or not simple_templating:
output_images.append(image)
Expand Down Expand Up @@ -1019,9 +1045,10 @@ def txt2img(prompt: str, ddim_steps: int, sampler_name: str, toggles: List[int],
skip_grid = 3 not in toggles
sort_samples = 4 in toggles
write_info_files = 5 in toggles
jpg_sample = 6 in toggles
use_GFPGAN = 7 in toggles
use_RealESRGAN = 8 in toggles
write_to_one_file = 6 in toggles
jpg_sample = 7 in toggles
use_GFPGAN = 8 in toggles
use_RealESRGAN = 9 in toggles

if sampler_name == 'PLMS':
sampler = PLMSSampler(model)
Expand Down Expand Up @@ -1074,6 +1101,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
normalize_prompt_weights=normalize_prompt_weights,
sort_samples=sort_samples,
write_info_files=write_info_files,
write_info_to_one_file=write_to_one_file,
jpg_sample=jpg_sample,
variant_amount=variant_amount,
variant_seed=variant_seed,
Expand Down Expand Up @@ -1135,8 +1163,8 @@ def flag(self, flag_data, flag_option=None, flag_index=None, username=None):


def img2img(prompt: str, image_editor_mode: str, init_info, mask_mode: str, mask_blur_strength: int, ddim_steps: int, sampler_name: str,
toggles: List[int], realesrgan_model_name: str, n_iter: int, cfg_scale: float, denoising_strength: float,
seed: int, height: int, width: int, resize_mode: int, fp = None):
toggles: List[int], realesrgan_model_name: str, n_iter: int, cfg_scale: float, denoising_strength: float,
seed: int, height: int, width: int, resize_mode: int, fp=None):
outpath = opt.outdir_img2img or opt.outdir or "outputs/img2img-samples"
err = False
seed = seed_to_int(seed)
Expand All @@ -1151,9 +1179,10 @@ def img2img(prompt: str, image_editor_mode: str, init_info, mask_mode: str, mask
skip_grid = 5 not in toggles
sort_samples = 6 in toggles
write_info_files = 7 in toggles
jpg_sample = 8 in toggles
use_GFPGAN = 9 in toggles
use_RealESRGAN = 10 in toggles
write_info_to_one_file = 8 in toggles
jpg_sample = 9 in toggles
use_GFPGAN = 10 in toggles
use_RealESRGAN = 11 in toggles

if sampler_name == 'DDIM':
sampler = DDIMSampler(model)
Expand Down Expand Up @@ -1317,6 +1346,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
uses_random_seed_loopback=random_seed_loopback,
sort_samples=sort_samples,
write_info_files=write_info_files,
write_info_to_one_file=write_info_to_one_file,
jpg_sample=jpg_sample,
)

Expand Down Expand Up @@ -1372,6 +1402,7 @@ def sample(init_data, x, conditioning, unconditional_conditioning, sampler_name)
uses_loopback=loopback,
sort_samples=sort_samples,
write_info_files=write_info_files,
write_info_to_one_file=write_info_to_one_file,
jpg_sample=jpg_sample,
)

Expand Down Expand Up @@ -1655,6 +1686,7 @@ def make_mask_image(r):
uses_loopback=False,
sort_samples=True,
write_info_files=True,
write_info_to_one_file=False,
jpg_sample=False,
imgProcessorTask=True
)
Expand Down Expand Up @@ -1852,6 +1884,7 @@ def run_RealESRGAN(image, model_name: str):
'Save grid',
'Sort samples by prompt',
'Write sample info files',
'Write sample info to one file',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming is misleading.
It will make users think that if they tick both individual sample files will not be written.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funnily I wasn't really sure what to name it do you have any suggestions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your goal is a log file: "Write sample log files"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No wait, your files are explicitly not per sample.
"Write log files"?

Copy link
Contributor Author

@TingTingin TingTingin Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe
write sample info to log file?

'jpg samples',
]

Expand Down Expand Up @@ -1911,6 +1944,7 @@ def run_RealESRGAN(image, model_name: str):
'Save grid',
'Sort samples by prompt',
'Write sample info files',
'Write sample info to one file',
'jpg samples',
]
"""
Expand Down