From 318650e01e4e26ed07a5d9821d069cf148fd97ff Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Tue, 20 Sep 2022 00:09:33 +0200 Subject: [PATCH 1/5] Fixed fused_memory_opt error --- ldm/modules/diffusionmodules/model.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ldm/modules/diffusionmodules/model.py b/ldm/modules/diffusionmodules/model.py index 4940e7b9a..f2606a97e 100644 --- a/ldm/modules/diffusionmodules/model.py +++ b/ldm/modules/diffusionmodules/model.py @@ -184,12 +184,12 @@ def __init__(self, in_channels): @torch.jit.script -def fused_t(x, c): +def fused_t(x, c: int): return x * (int(c) ** (-0.5)) @torch.jit.script -def fused_memory_opt(mem_reserved, mem_active, mem_free_cuda, b, h, w, c): +def fused_memory_opt(mem_reserved: int, mem_active: int, mem_free_cuda: int, b: int, h: int, w: int, c: int): mem_free_torch = mem_reserved - mem_active mem_free_total = mem_free_cuda + mem_free_torch @@ -198,7 +198,7 @@ def fused_memory_opt(mem_reserved, mem_active, mem_free_cuda, b, h, w, c): # s3 = (b * c * h * w * 3) * 2 # zeros_like, empty_like, empty_strided # s = 2 * (s1 + s2 + s3) # 2 because of small allocations which don't really matter s = 16 * b * ((h * w) ** 2) + 12 * b * c * h * w - s = (s // mem_free_total) + 1 if s > mem_free_total else torch.tensor(1) + s = int((s // mem_free_total) + 1) if s > mem_free_total else 1 return s From 5f7c6c47f9a554dc77565b95c9bfe5b74ac11c66 Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Tue, 20 Sep 2022 10:58:33 +0200 Subject: [PATCH 2/5] Fixed requirements and save as single image with time stamp --- optimizedSD/optimized_txt2img.py | 15 ++++++++++++++- requirements.txt | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/optimizedSD/optimized_txt2img.py b/optimizedSD/optimized_txt2img.py index 34c251205..a9ba9f187 100644 --- a/optimizedSD/optimized_txt2img.py +++ b/optimizedSD/optimized_txt2img.py @@ -1,4 +1,5 @@ import argparse +from datetime import datetime import os import random import re @@ -182,6 +183,15 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): opt.seed += 1 base_count += 1 + count = 0 + for x_sample in all_samples: + x_sample = 255. * rearrange(x_sample[0].numpy(), 'c h w -> h w c') + now = datetime.now() + dt_string = now.strftime(" %d-%m-%Y %H_%M_%S ") + Image.fromarray(x_sample.astype(np.uint8)).save( + os.path.join(sample_path, f"{dt_string+str(count)}.png")) + count += 1 + if opt.device != "cpu": mem = torch.cuda.memory_allocated() / 1e6 modelFS.to("cpu") @@ -422,7 +432,10 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): grid = torch.cat(all_samples, 0) grid = make_grid(grid, nrow=opt.n_iter) grid = 255.0 * rearrange(grid, "c h w -> h w c").cpu().numpy() + + now = datetime.now() + dt_string = now.strftime(" %d-%m-%Y %H_%M_%S ") Image.fromarray(grid.astype(np.uint8)).save( - os.path.join(outpath + "/" + str(opt.prompt).replace("/", "")[:100] + f".{opt.format}") + os.path.join(outpath + "/" + str(opt.prompt).replace("/", "")[:100] + dt_string + f".{opt.format}") ) print("exported to", outpath + "/" + str(opt.prompt).replace("/", "")[:100] + f".{opt.format}") diff --git a/requirements.txt b/requirements.txt index 9af0e8223..da6298f7a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,6 @@ kornia yapf lpips tb-nightly --e git+https://github.com/openai/CLIP.git@main#egg=clip git+https://github.com/crowsonkb/k-diffusion -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers -e . From dcb6ec7afc6ef0930304c01134f7d777d39bdd33 Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Tue, 20 Sep 2022 18:45:50 +0200 Subject: [PATCH 3/5] Added pngs to git ignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..087a8040e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +*.png +*.pyc +*.csv From 1a4f43ad97d64bc77e498be4d2414965d1b19c47 Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Tue, 20 Sep 2022 18:47:38 +0200 Subject: [PATCH 4/5] Test --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 087a8040e..489e48483 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.png *.pyc *.csv +CodeFormer/ +src/clip/ +src/taming-transformers/ From da7c316839ed876a6a6f88eee49428b876733494 Mon Sep 17 00:00:00 2001 From: Maximilian Winter Date: Tue, 20 Sep 2022 20:10:09 +0200 Subject: [PATCH 5/5] Test2 --- .gitignore | 9 +++++++++ optimizedSD/optimized_txt2img.py | 13 +++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 489e48483..00c591c03 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,12 @@ CodeFormer/ src/clip/ src/taming-transformers/ +latent_diffusion.egg-info/dependency_links.txt +latent_diffusion.egg-info/PKG-INFO +latent_diffusion.egg-info/requires.txt +latent_diffusion.egg-info/SOURCES.txt +latent_diffusion.egg-info/top_level.txt +log.txt +models/ldm/stable-diffusion-v1/model.ckpt +.vscode/settings.json +tqdm.txt diff --git a/optimizedSD/optimized_txt2img.py b/optimizedSD/optimized_txt2img.py index a9ba9f187..a7d388313 100644 --- a/optimizedSD/optimized_txt2img.py +++ b/optimizedSD/optimized_txt2img.py @@ -114,6 +114,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): seeds = "" with torch.no_grad(): all_samples = list() + count_iter = 0 for _ in trange(opt.n_iter, desc="Sampling"): for prompts in tqdm(data, desc="data"): sample_path = os.path.join(opt.outpath, "_".join(re.split(":| ", prompts[0])))[:150] @@ -142,7 +143,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): c = modelCS.get_learned_conditioning(prompts) shape = [opt.num_images, opt.C, opt.height // opt.f, opt.width // opt.f] - + seed_at_begin = opt.seed if opt.device != "cpu": mem = torch.cuda.memory_allocated() / 1e6 modelCS.to("cpu") @@ -184,12 +185,12 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): base_count += 1 count = 0 - for x_sample in all_samples: - x_sample = 255. * rearrange(x_sample[0].numpy(), 'c h w -> h w c') + for x_index in range(count_iter * batch_size, len(all_samples)): + x_sample = 255. * rearrange(all_samples[x_index][0].numpy(), 'c h w -> h w c') now = datetime.now() - dt_string = now.strftime(" %d-%m-%Y %H_%M_%S ") + dt_string = now.strftime("%d-%m-%Y %H_%M_%S ") Image.fromarray(x_sample.astype(np.uint8)).save( - os.path.join(sample_path, f"{dt_string+str(count)}.png")) + os.path.join(sample_path, f"{dt_string}s{seed_at_begin}d{opt.ddim_steps}-{str(count)}.png")) count += 1 if opt.device != "cpu": @@ -199,7 +200,7 @@ def get_image(opt, model, modelCS, modelFS, prompt=None): time.sleep(1) del samples_ddim print("memory_final = ", torch.cuda.memory_allocated() / 1e6) - + count_iter += 1 toc = time.time() time_taken = (toc - tic) / 60.0